转型

父类代码:

 1 package org.hanqi.pn0120;
 2 
 3 public class Son extends Father {
 4     //Object a;  所有类的父类
 5     public Son()
 6     {
 7         //super 表示父类
 8         super("儿子");
 9         System.out.println("子类的构造方法");
10     }
11     public void sing()
12     {
13         System.out.println("我喜欢唱歌");
14     }
15     //覆盖(重写)
16     public void work()
17     {
18         //调用父类方法
19         //super.work();
20         //System.out.println("我不喜欢上班,我要去参加海选");
21         System.out.println("我边上班边练歌");
22     }
23     public static Object getData(int i)
24     {
25         Object rtn=null;
26         //获取数据
27         if(i==1)
28         {
29         //1.father
30         Father f=new Father("向上转型的父类");
31         //向上转型
32         rtn=f;
33         }
34         else
35         {
36         //2.Son
37         Son s=new Son();
38         rtn=s;
39         }
40         return rtn;
41     }
42 }
View Code

子类代码:

 1 package org.hanqi.pn0120;
 2 
 3 public class Son extends Father {
 4     //Object a;  所有类的父类
 5     public Son()
 6     {
 7         //super 表示父类
 8         super("儿子");
 9         System.out.println("子类的构造方法");
10     }
11     public void sing()
12     {
13         System.out.println("我喜欢唱歌");
14     }
15     //覆盖(重写)
16     public void work()
17     {
18         //调用父类方法
19         //super.work();
20         //System.out.println("我不喜欢上班,我要去参加海选");
21         System.out.println("我边上班边练歌");
22     }
23     public static Object getData(int i)
24     {
25         Object rtn=null;
26         //获取数据
27         if(i==1)
28         {
29         //1.father
30         Father f=new Father("向上转型的父类");
31         //向上转型
32         rtn=f;
33         }
34         else
35         {
36         //2.Son
37         Son s=new Son();
38         rtn=s;
39         }
40         return rtn;
41     }
42 }
View Code

Test类代码:

 1 package org.hanqi.pn0120;
 2 
 3 public class Testjicheng {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7 
 8         Father f=new Father("父亲");
 9         //f.setName("父亲");
10         f.setAge(50);
11         System.out.println("名字是:"+f.getName()+" 年龄是:"+f.getAge());
12         f.work();
13         System.out.println();
14         Son s=new Son();
15         //s.setName("儿子");
16         s.setAge(20);
17         System.out.println("名字是:"+s.getName()+" 年龄是:"+s.getAge());
18         s.work();
19         s.sing();
20         System.out.println();
21         //转型
22         //向上转型   由子类转向父类     即用父类对象实现子类方法
23         Father f1=new Son();
24         System.out.println("名字是"+s.getName());
25         f1.work();//如果被子类重写了,就会调用子类的方法
26         System.out.println("向下转型");
27         //向下转型   由父类转向子类     即将父类方法转换成子类对象并实现子类方法,必须先做向上转型
28         Son s1=(Son)f1;
29         s1.work();
30         System.out.println();
31         //向上转型常用方式先向上转型Object再向下转型
32         Father f2=(Father)Son.getData(1);//想调用子类方法就调用子类想调用父类方法就调用父类
33         f2.work();
34         
35         
36     }
37 
38 }
View Code

posted on 2016-03-09 08:52  beens  阅读(181)  评论(0编辑  收藏  举报

导航