class Student11 {
private String name;
private int age;
Student11(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class DouTaiDemo5 {
public static void main(String[] args) {
Object obj = new Student11("zhangsan", 30);
Student11 s = (Student11) obj;
System.out.println(s.name + "----" + s.age); // 这里访问Studnet类中的name和age时为什么要调用getName和getAge方法,而不能直接用s.name和s.age
}
}
|