java

static final 修饰的属性和函数在子类中继承与访问

2018-06-11  本文已影响3人  n油炸小朋友

父类:

public class fatherClass {
    public final int FinalField=1;
    public static int staticFields=2;
    public static final int staticFinalFields=3;
    public final void finalMethod() {
        System.out.println("final Method");
        
    }
    public static  void staticMethod() {
        System.out.println("static Method");
        System.out.println("staticFields:"+staticFields);
    }
    public static final void staticFinalMethod(){
        System.out.println("staticFinalMethod");
    }
}

子类

public class sonClass extends fatherClass {
    
public static void main(String[] args) {
    System.out.println("~~~~~~~~staticFinalFields~~~~~");
    System.out.println("fatherClass.staticFinalFields:"+fatherClass.staticFinalFields);
    //错误:staticFinalShuxing=8;
    System.out.println("子类中用staticFinalFields:"+staticFinalFields);//static final属性可以被继承 但不可以被更改
    
    System.out.println("~~~~~~~~staticFields~~~~~~~~~~~~~~");
    System.out.println("fatherClass.staticFields:"+fatherClass.staticFields);
    System.out.println("子类中用staticFields:"+staticFields);
    System.out.println("更改staticFields=6");
    staticFields=6;
    System.out.println("fatherClass.staticFields:"+fatherClass.staticFields);
    System.out.println("子类中用staticFields:"+staticFields);
    
    System.out.println("~~~~~~FinalField~~~~~~~");
    System.out.println("final修饰的属性没有被继承");
    fatherClass fa =new fatherClass();
    System.out.println("用父类实例才能访问final属性");
    System.out.println(fa.FinalField);
    
    System.out.println("~~~~~~~~~~finalMethod~~~~~~~~~~`");
    System.out.println("final修饰的函数没有被继承");
    System.out.println("用父类实例才能访问final函数");
    fa.finalMethod();
    
    System.out.println("~~~~~~~~~~staticFinalMethod~~~~~~~~~~`");
    System.out.println("fatherClass.staticFinalMethod():");
    fatherClass.staticFinalMethod();
    System.out.println("子类用:");
    staticFinalMethod();
    
    System.out.println("~~~~~~~~~~staticMethod~~~~~~~~~~`");
    System.out.println("fatherClass.staticMethod():");
    fatherClass.staticMethod();
    System.out.println("子类用:");
    staticMethod();

}

结果:

~~~~~~~~staticFinalFields~~~~~
fatherClass.staticFinalFields:3
子类中用staticFinalFields:3
~~~~~~~~staticFields~~~~~~~~~~~~~~
fatherClass.staticFields:2
子类中用staticFields:2
更改staticFields=6
fatherClass.staticFields:6
子类中用staticFields:6
~~~~~~FinalField~~~~~~~
final修饰的属性没有被继承
用父类实例才能访问final属性
1
~~~~~~~~~~finalMethod~~~~~~~~~~`
final修饰的函数没有被继承
用父类实例才能访问final函数
final Method
~~~~~~~~~~staticFinalMethod~~~~~~~~~~`
fatherClass.staticFinalMethod():
staticFinalMethod
子类用:
staticFinalMethod
~~~~~~~~~~staticMethod~~~~~~~~~~`
fatherClass.staticMethod():
static Method
staticFields:6
子类用:
static Method
staticFields:6

要是在子类中加入同名的属性:

public static int staticFields=7;



那么结果:

~~~~~~~~staticFields~~~~~~~~~~~~~~
fatherClass.staticFields:2
子类中用staticFields:7
更改staticFields=6
fatherClass.staticFields:2
子类中用staticFields:6

并且

fatherClass c=new sonClass();
    
System.out.println(c.staticFields);//得到的是父类中的值2,这就是覆盖:保留两套,用谁声明就用谁的

总结:

属性:

关于继承:
static和static final修饰的被子类继承,final修饰的属性没被子类继承
关于修改:
static修饰的属性被修改,没有同名属性的情况是直接修改了父类中的属性的值,static final修饰的不可被修改

函数

关于继承:
static和static final修饰的被子类继承,final修饰的函数没被子类继承
关于重写:
static修饰的函数可以重写,static final和final修饰的函数不能被重写

上一篇下一篇

猜你喜欢

热点阅读