您的位置:首页 > 其它

继承,转型

2016-03-08 13:46 99 查看
package juxing;

public class TestJiCheng {

public static void main(String[] args)
{

Father f=new Father("父亲");
//f.setName("父亲");

f.setAge(50);
f.work();
System.out.println();

Son s=new Son();

//s.setName("儿子");

s.setAge(20);

System.out.println("儿子"+s.getAge()+"岁");
System.out.println();
s.work();

s.sing();

System.out.println();
System.out.println();
//转型
//向上转型,子类到父类

Father f1=new Son();
System.out.println("名字"+s.getName());
f1.work();//调用子类的work

System.out.println();
System.out.println("向下转型,父类转子类");
//向下转型,父类转子类
//Son s1=(Son) new Father("父亲");子类有的父类不一定有

Son s1=(Son)f1;

s1.work();
System.out.println();

//调用,
System.out.println("类型不确定,用Object,先向上全部转成父类,再向下转型。");
Father f2=(Father)Son.getDate(2);
f2.work();

}

}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: