课堂代码
~
package edu.xcdq;
public class Demo01 {
public static void main(String[]args){
UncleOne dajiu = new UncleOne();
dajiu.fahongbao();
//多态
Uncle dajiu1 = new UncleOne();
dajiu1.faHongbao();
// 子类独有的方法在发生向上转型的时候会无法在父类当中去使用
//dajiu1.songYan(); //会报错
UncleOne temp = (UncleOne)dajiu1;
temp.songYan();
Uncle erjiu = new UncleTow();
erjiu.faHongbao();
/*//向下转型
UncleOne uncle1 = (UncleOne) new Uncle();
UncleTow temp = (UncleTow) uncleTow;
uncle1.fahongbao();*/
}
}
~
~
package edu.xcdq;
public class Uncle {
private String name;
private int age;
public Uncle(){
}
public Uncle(String name,int age){
this.name = name;
this.age = age;
}
public void setName(String name){
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
@Override
public String toString(){
return "Uncle{"+
"name'"+name + '\''+
",age = " + age +
'}';
}
public void faHongbao(){
System.out.println("舅舅发红包");
}
}
~
~
package edu.xcdq;
public class UncleOne extends Uncle {
public void fahongbao(){
System.out.println("大舅发红包");
}
public void songYan(){
System.out.println("大舅喜欢送烟");
}
}
~
~
package edu.xcdq;
public class UncleTow extends Uncle {
public void fahongbao(){
System.out.println("二舅发红包");
}
}
~