Java多态理解

 1 class Father
 2 {
 3     String father = "father";
 4     public void show(){
 5             System.out.println("Father is show()");
 6     }
 7     public void show1(){
 8             System.out.println("Father  show1111()");
 9     }
10 }
11 class Son extends Father
12 {
13     String father = "son";
14     String son = "子类特有的";
15     //重写了父类方法
16     public void show(){
17             System.out.println("Son is show()");
18     }
19     //重载父类的方法,但对于父类,是一个新增加的功能
20     public void show(int i){
21         System.out.println("Son is show() again");
22     }
23 }
24 //    测试多态性
25 class ExtendsDemo
26 {
27     public static void main(String []args){
28             
29         Father f = new Father();//
30         Father s = new Son();//创建一个父类引用指向子类对象
31         Son son = new Son();//
32         f.show();//父亲访问自己的方法
33         s.show();//由于父类引用指向了子类对象,子类又对父类的show()方法进行了重写,故访问子类的方法
34         //s.show(1);//show(int i)由于是子类新增加的功能,父类并不知道,所以不能调用,会编译不过
35         s.show1();//子类并没有重写父类show1()方法,故会去调用父类的方法show1()
36         System.out.println("Father f :"+f.father);//调用父类属性
37         System.out.println("Father s :"+s.father);//调用父类属性
38          System.out.println("Father s:"+f.son);//为子类Son新增加的属性,父类不能调用,编译不过
39     }
40 }
41 /*运行结果:
42         Father is show()
43         Son is show()
44         Father  show1111()
45         Father f :father
46         Father s :father
47 */
48     
49 /*
50     多态的三个必要条件:
51          1.继承   2.重写   3.父类引用指向子类对象。
52     总结:
53     
54         一、使用父类类型的引用指向子类的对象;
55 
56         二、该引用只能调用父类中已经定义的方法和变量;
57 
58         三、如果子类中重写了父类中的一个方法,那么在调用这个方法的时候,将会调用子类中的这个方法;(动态连接、动态调用) 
59                 动态绑定是指”在执行期间(而非编译期间)“判断所引用对象的实际类型,根据实际的类型调用其相应的方法。
60 
61         四、变量不会被重写(覆盖),”重写“的概念只针对方法,如果在子类中”重写“了父类中的变量,那么在调用时,还是会调用父类的变量
62         
63 */

   接口中的多态:

    

 1 interface Inter
 2 {
 3         public void show();
 4         String inter = "inter";
 5 }
 6 class ClassA implements Inter
 7 {
 8         public void show(){
 9             System.out.println("ClassA is show() ");
10         }
11         public void run(){
12             System.out.println("Class A running ,,,");
13         }
14         String inter = "classA";
15         String classA = "aaaaa";
16 }
17 class ClassB extends ClassA
18 {
19         
20     public void show(){
21         System.out.println("ClassB is show()");
22     }
23     String inter = "bbbbb";
24 }
25 public class InterfaceDemo 
26 {
27     
28     public static void main(String[] args){
29 
30         Inter interA = new ClassA();
31         Inter interB = new ClassB();
32 
33         interA.show();
34         interB.show();
35         //interA.run();//报错,访问不到实现类新增加的方法;
36         System.out.println(interA.inter);
37         System.out.println(interB.inter);
38         //System.out.println(interA.classA);//报错,访问不到实现类新增加的成员变量;
39     }
40 }
41 /*
42     运行结果:
43         ClassA is show()
44         ClassB is show()
45         inter
46         inter
47 */

 

posted @ 2013-04-09 17:08  hacket520  阅读(157)  评论(0)    收藏  举报