继承中方法的重写

继承中方法的重写

  • 重写只跟非静态有关

  • 重写时,输出结果决定于右边

  • 重写只能是public,不能是private

  • 需要有继承关系,子类重写父类的方法

  • 方法名必须相同,参数列表必须相同,只有方法体不同

  • 修饰符:范围可以扩大,但不可以缩小:public>protected>default>private

  • 抛出异常:范围,可以被缩小,但不能扩大;

    如:

    public class HelloWorld {
    ​
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            test10 a = new test10();
            a.nice();
            test10 b = new test11();//子类重写方法后调用。
            b.nice();
        }
    ​
    ​
    public class test10 {
    ​
        public void nice() {
            System.out.println("大帅哥");
        }
    ​
    }
    public class test11 extends test10{
        public void nice(){
            System.out.println("w");
        }
    }
    }

输出结果:

大帅哥

w

猜你喜欢

转载自blog.csdn.net/weixin_54194783/article/details/117266978