java,为啥用 Student.getName(); 就报错,用 s.getName();就正确呢???
publicclassStudent{publicStringname;//名字publicintage;publicstaticStringnickName;//别名p...
public class Student {
public String name; //名字
public int age;
public static String nickName; //别名
public Student(String name, int i) {
super();
this.name = name;
this.age = i;
}
public String getName(){
return this.name;
}
public static String getNickName(){
return nickName;
}
public static void main(String[] args) throws Exception {
Student s = new Student("张三", 10);
Student.getNickName();
s.getName(); ////正确
Student.getNickName(); //类调用类
Student.getName(); //类调用实例 错误
}
}
Student是静态类吗? 展开
public String name; //名字
public int age;
public static String nickName; //别名
public Student(String name, int i) {
super();
this.name = name;
this.age = i;
}
public String getName(){
return this.name;
}
public static String getNickName(){
return nickName;
}
public static void main(String[] args) throws Exception {
Student s = new Student("张三", 10);
Student.getNickName();
s.getName(); ////正确
Student.getNickName(); //类调用类
Student.getName(); //类调用实例 错误
}
}
Student是静态类吗? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询