/*多态
创建一个父类的对象,用子类去初始化
如果要使用子类的方法,则需要把对象强制转换为子类的(xx)xxx
*/
public class Learn01{
public static void main(String[] aa){
Person p1=new Student(18);
p1.changeName(\"Remoo\");
System.out.println(\"p1\'s name:\"+p1.getName());
System.out.println(((Student)p1).getScore());//18
((Student)p1).changeScore(666);
System.out.println(((Student)p1).getScore());//666
}
}
class Person{
private String name;
//Construction method
public Person(){}
public Person (String name){
this.name = name;
}
//getter&setter
public void changeName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
class Student extends Person{
private int score;
//Construction method
public Student(int score){
this.score = score;
}
//getter&setter
public void changeScore(int score){
this.score = score;
}
public int getScore(){
return this.score;
}
}
来源:https://www.cnblogs.com/remyuu/p/15987799.html
本站部分图文来源于网络,如有侵权请联系删除。