静态属性和静态方法能否被继承

静态属性和静态方法是可以被继承的,子类可以调用父类所有的静态属性和静态方法
如果子类有和父类同名的静态属性和静态方法
没有向上转型的时候,子类只可以调用定义在自己类中的静态属性和静态方法,就会把同名的父类的静态属性和静态方法隐藏
如果向上转型Father father = new Son(),这时候father只会调用父类自己的静态属性和静态方法,不会调用子类的静态属性和静态方法

子类不能重写父类的静态方法
属性不能被重写,不管是静态还是非静态

 

public class Pen {
    private String color;
    private int size;
    public static String staticProperty= "father static property";
    public String normalProperty = "father normal property";

    public static void staticMethod() {
        Log.d("wang_bo", "father static method");
    }

    public void normalMethod() {
        Log.d("wang_bo", "father normal method");
    }

}

public class PenSon1 extends Pen {
    public static String staticProperty = "PenSon1 static property";
    public static String normalProperty = "PenSon1 normal property";

    public static void staticMethod() {
        Log.d("wang_bo", "PenSon1 static method");
    }

    public void normalMethod() {
        Log.d("wang_bo", "PenSon1 normal method");
    }
}
public class PenSon2 extends Pen {
}
 
Pen pen =  new Pen();
Log.d("wang_bo", "Father Pen " + pen.staticProperty);
Log.d("wang_bo", "Father Pen " + pen.normalProperty);
pen.staticMethod();
pen.normalMethod();

Log.d("wang_bo", "---------------------------------------");
PenSon2 pen2 = new PenSon2();
Log.d("wang_bo", "Father Pen " + pen2.staticProperty);
Log.d("wang_bo", "Father Pen " + pen2.normalProperty);
pen2.staticMethod();
pen2.normalMethod();

Log.d("wang_bo", "---------------------------------------");
Pen pen22 = new PenSon2();
Log.d("wang_bo", "Father Pen " + pen22.staticProperty);
Log.d("wang_bo", "Father Pen " + pen22.normalProperty);
pen22.staticMethod();
pen22.normalMethod();

Log.d("wang_bo", "---------------------------------------");
PenSon1 penSon1 = new PenSon1();
Log.d("wang_bo", "Father Pen " + penSon1.staticProperty);
Log.d("wang_bo", "Father Pen " + penSon1.normalProperty);
penSon1.staticMethod();
penSon1.normalMethod();

Log.d("wang_bo", "---------------------------------------");
Pen penSon11 = new PenSon1();
Log.d("wang_bo", "Father Pen " + penSon11.staticProperty);
Log.d("wang_bo", "Father Pen " + penSon11.normalProperty);
penSon11.staticMethod();
penSon11.normalMethod();

 

10-23 05:04:01.907  1345  1345 D wang_bo : Father Pen father static property
10-23 05:04:01.907  1345  1345 D wang_bo : Father Pen father normal property
10-23 05:04:01.907  1345  1345 D wang_bo : father static method
10-23 05:04:01.907  1345  1345 D wang_bo : father normal method
10-23 05:04:01.907  1345  1345 D wang_bo : ---------------------------------------
10-23 05:04:01.907  1345  1345 D wang_bo : Father Pen father static property
10-23 05:04:01.907  1345  1345 D wang_bo : Father Pen father normal property
10-23 05:04:01.907  1345  1345 D wang_bo : father static method
10-23 05:04:01.907  1345  1345 D wang_bo : father normal method
10-23 05:04:01.907  1345  1345 D wang_bo : ---------------------------------------
10-23 05:04:01.907  1345  1345 D wang_bo : Father Pen father static property
10-23 05:04:01.907  1345  1345 D wang_bo : Father Pen father normal property
10-23 05:04:01.907  1345  1345 D wang_bo : father static method
10-23 05:04:01.907  1345  1345 D wang_bo : father normal method
10-23 05:04:01.907  1345  1345 D wang_bo : ---------------------------------------
10-23 05:04:01.907  1345  1345 D wang_bo : Father Pen PenSon1 static property
10-23 05:04:01.907  1345  1345 D wang_bo : Father Pen PenSon1 normal property
10-23 05:04:01.907  1345  1345 D wang_bo : PenSon1 static method
10-23 05:04:01.907  1345  1345 D wang_bo : PenSon1 normal method
10-23 05:04:01.907  1345  1345 D wang_bo : ---------------------------------------
10-23 05:04:01.907  1345  1345 D wang_bo : Father Pen father static property
10-23 05:04:01.907  1345  1345 D wang_bo : Father Pen father normal property
10-23 05:04:01.907  1345  1345 D wang_bo : father static method
10-23 05:04:01.907  1345  1345 D wang_bo : PenSon1 normal method
 

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值