哪些方法是不能被重写的

方法的重写:

< 不能被重写的方法: 私有方法 静态方法 构造方法 final 修饰的方法
< 被 static 修饰的方法为什么不能被重写 : 因为static 方法属于类方法,在进行执行的时候,只会去找被申明的引用的那个类,不会去管他new 了什么. 不会向普通方法一样层层向去寻找.

package InstaceOf;

/**
 * @author ct_start
 * @create 2021 -07 -18
 **/
public class A {
    public static void main(String[] args) {
        
        // 测试 一下静态方法为什么不能被重写
        Parent parent=new son();
        parent.say();
        parent.code();
        // 测试 一下 Instanceof 关键字的使用 

        Parent son = new son();
        Parent daughter = new daughter();
//        
//        if (son instanceof  daughter)
//        System.out.println(" 是");
    }
}
class Parent {
    public   static void say(){
        System.out.println("我是parent里面的静态方法");
    }

    public void code(){
        System.out.println("父类里面的code");
    }
}

class son  extends Parent
{


      public  static  void say(){
          System.out.println("我是子类的里面的静态方法");
      }

    @Override
    public void code() {
        System.out.println("子类里面的code");
    }
}

class daughter extends  Parent{
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值