方法和变量在继承时的覆盖和隐藏

package com.jdk7.chapter2.converhide;

public class Parent {
    public static String kind = "com.jdk7.chapter2.Parent";    //类变量
    public static int age = 100;            //类变量
    public String name = "Parent";            //实例变量
    
    public static String getKind() {        //静态方法
        System.out.println("Parent获取kind值");
        return kind;
    }
    public static int getAge() {            //静态方法
        System.out.println("Parent获取age值");
        return age;
    }
    public String getName() {                //实例方法
        System.out.println("Parent获取name值");
        return name;
    }
    public final int getNextAge(){
        return ++age;
    }
}
 1 package com.jdk7.chapter2.converhide;
 2 
 3 public class Child extends Parent {
 4     public static String kind = "com.jdk7.chapter2.Child";        //类变量
 5     public int age = 50;                //实例变量
 6     public String name = "Child";        //实例变量
 7     public static String getKind() {    //静态方法覆盖静态静态方法
 8         System.out.println("Child获取age值");
 9         return kind;
10     }
11     public String getName() {            //实例方法覆盖实例方法
12         System.out.println("Child获取name值");
13         return name;
14     }
15     public static String getParentKind(){        //访问父类的类变量要使用 父类名+变量名
16         System.out.println("在Child中获取Parent的kind");
17         return Parent.kind;
18     }
19     public static int getParentAge(){
20         System.out.println("在Child中获取Parent的age");
21         return Parent.age;
22     }
23     public String getParentName(){                //访问父类的实例变量要使用 super/对象名+变量名
24         System.out.println("在Child中获取Parent的name");
25         return super.name;                        
26     }
27     //静态方法无法被实例方法覆盖
28 //    public int getAge() {
29 //        return age;
30 //    }
31     
32     //final方法无法被覆盖
33 //    public int getNextAge(){
34 //        return ++age;
35 //    }
36     
37 }
 1 package com.jdk7.chapter2.converhide;
 2 
 3 public class ConverHideTest {
 4     public static void main(String[] args) {
 5         Child child = new Child();
 6         System.out.println("child kind: "+child.kind+"; child age: "+child.age+"; child name: "+child.name);
 7         Parent parent = child;
 8         System.out.println("父类的静态方法被子类隐藏,可以再调用:"+parent.getKind());
 9         System.out.println("父类的实例方法被子类覆盖,无法再调用:"+parent.getName());
10         System.out.println("父类的变量被子类隐藏, parent kind: "+parent.kind+"; parent age: "+parent.age+"; parent name: "+parent.name);
11         System.out.println("子类中访问父类实例变量name: "+child.getParentName());
12         System.out.println("子类中访问父类静态变量kind: "+child.getParentKind());
13     }
14 }
15 
16 执行结果:
17 child kind: com.jdk7.chapter2.Child; child age: 50; child name: Child
18 Parent获取kind值
19 父类的静态方法被子类隐藏,可以再调用:com.jdk7.chapter2.Parent
20 Child获取name值
21 父类的实例方法被子类覆盖,无法再调用:Child
22 父类的变量被子类隐藏, parent kind: com.jdk7.chapter2.Parent; parent age: 100; parent name: Parent
23 在Child中获取Parent的name
24 子类中访问父类实例变量name: Parent
25 在Child中获取Parent的kind
26 子类中访问父类静态变量kind: com.jdk7.chapter2.Parent

总结:

(1) 类变量,又叫静态变量,这种变量属于类,通过类名称就可以访问类变量。

例:

Child ->getParentKind->return Parent.kind;

(2) 实例变量,属于类的实例,即对象,通过对象可以访问实例变量,但不能通过类名访问实例变量。

例:

1.Child ->getParentName->return super.name;
2.ConverHideTest->child.name

(3) 静态方法,该方法属于类,通过类名称就可以访问静态方法。

例:

1.Parent.getKind()

(4) 实例方法,属于类的实例,即对象,通过对象可以访问实例方法,但不能通过类名访问实例方法。

例:

Child child = new Child();
Parent parent = child;
parent.getName();

(5) 隐藏:B隐藏了A的方法或者变量,那么B不能访问A被隐藏的方法或变量,但是将B转换为A后,可以访问A被隐藏的方法或变量。

例:

Child child = new Child();
Parent parent = child;
parent.getKind();

(6) 覆盖:B覆盖了A的方法或者变量,那么B不能访问A被覆盖的方法或变量,但是将B转换为A后,同样不能访问A被隐藏的方法或变量。

例:

Child child = new Child();
Parent parent = child;
parent.getName();

在java中,变量和方法在继承时的隐藏和覆盖的原则如下:
1. 父类的静态变量和实例变量能被子类同名的变量隐藏;
2. 父类的静态方法可以被子类同名的静态方法隐藏;
3. 父类的实例方法可以被子类同名的实例方法覆盖;
4. 不能用子类同名的静态方法隐藏父类中的实例方法,否则编译会出错;
5. 不能用子类同名的实例方法覆盖父类中的静态方法,否则编译会出错;
6. 最终方法(带有final的方法)不能被覆盖;
7. 变量只会被隐藏,不会被覆盖,子类的静态变量可以隐藏父类的实例变量,子类的实例变量也可以隐藏父类的静态变量。


posted @ 2018-01-14 17:04  celineluo  阅读(320)  评论(0编辑  收藏  举报