class fu{
int age = 70;
//成员方法
void teach(){
System.out.println("孔爹讲Java");
}
}
class zi extends fu{
int age = 40;
//重写teach方法
void teach(){
System.out.println("讲C++");
}
//特有的方法
void playGame(){
System.out.println("玩儿斗地主......");
}
}
class Demo
{
public static void main(String[] args)
{
fu f = new zi();//多态
System.out.println("大家好,我的年龄是:" + f.age);//70
f.teach();//讲C++;
zi z = (zi)f;
z.playGame();//向下转型后,可以访问子类特有成员;
}
}
|
|