class Student{
private String name;
private int StuNum;
private int score;
Student(String name,int StuNum,int score){
this.name=name;
this.StuNum=StuNum;
this.score=score;
}
public String getName(){
return name;
}
public String setName(){
return name;
}
public int getStunum(){
return StuNum;
}
public int setStunmu(){
return StuNum;
}
public int getScore(){
return socre;
}
public int setScore(){
return score;
}
}
import java.util.*;
public class java4_1 {
public static void main(String[]args){
ArrayList aa1=new ArrayList();
Student Stu1=new Student(“悟空”,1,100);
aa1.add(Stu1);
System.out.println(“大小为”+aa1.size());
}
}
问一下这段代码哪里错的?
private String name;
private int StuNum;
private int score;
Student(String name,int StuNum,int score){
this.name=name;
this.StuNum=StuNum;
this.score=score;
}
public String getName(){
return name;
}
public String setName(){
return name;
}
public int getStunum(){
return StuNum;
}
public int setStunmu(){
return StuNum;
}
public int getScore(){
return socre;
}
public int setScore(){
return score;
}
}
import java.util.*;
public class java4_1 {
public static void main(String[]args){
ArrayList aa1=new ArrayList();
Student Stu1=new Student(“悟空”,1,100);
aa1.add(Stu1);
System.out.println(“大小为”+aa1.size());
}
}
问一下这段代码哪里错的?
解决方案
30
1、错误1:假如你用的是一个非记事本的编辑器,会出现
需要class, interface或enum的错误提示
原因是http://zhidao.baidu.com/question/1381624013526783580.html对于这个问题的解决方法:第一、可以将导入包语句import java.util.*;放在首行;第二、就是链接上说的,里面就是原因。
2、错误2:
public int getScore(){ return socre;}
写错了一个参数,应该是score
3、ArrayList带泛型参数,在代码中最好加上,不然会有警告。或是用它的父接口申明,并用它的子类进行实例化:
java.util Class ArrayList<E> java.lang.Object java.util.AbstractCollection<E> java.util.AbstractList<E> java.util.ArrayList<E>
4、建议:最好把出现的问题(编译出错的信息)贴出来,。
10
import java.util.*; class Student{ private String name; private int StuNum; private int score; Student(String name,int StuNum,int score){ this.name=name; this.StuNum=StuNum; this.score=score; } public String getName(){ return name; } public String setName(){ return name; } public int getStunum(){ return StuNum; } public int setStunmu(){ return StuNum; } public int getScore(){ return score; } public int setScore(){ return score; } } public class Test { public static void main(String[]args){ //实例化方法 ArrayList<Student> aa1=new ArrayList<Student>(); // List<Student> aa1 = ArrayList<Student>();//这样也可以 Student Stu1=new Student("悟空",1,100); aa1.add(Stu1); System.out.println("大小为"+aa1.size()); } }