zoukankan      html  css  js  c++  java
  • extends && implements

     final声明的类不能被继承

    方法的重写(@Override):  两同两小一大原则:

          方法名相同,参数类型相同

          子类返回类型小于等于父类方法返回类型(java里无论怎样都对)

          子类抛出异常小于等于父类方法抛出异常(不能抛出新的异常或比父类范围宽泛的异常)

          子类访问权限大于等于父类方法访问权限

          不能重写final声明方法,

          重写static方法只能是static的

    继承中方法的重写

    1
    package test; 2 3 public class ExtendTest { 4 5 static class A{ 6 public A() { 7 System.out.println("A 被创建!"); 8 } 9 public void t(){ 10 System.out.println("输出 A "); 11 } 12 } 13 static class B extends A{ 14 public B() { 15 System.out.println("B 被创建!"); 16 } 17 @Override 18 public void t() { 19 System.out.println("输出 B"); 20 } 21 public void k() { 22 System.out.println(" 方法 K"); 23 } 24 } 25 26 public static void main(String[] args) { 27 /** 28 * 1. 29 */ 30 A a1 = new A(); 31 B b1 = new B(); 32 a1.t(); 33 b1.t(); 34 35 // result: A 被创建! 36 // A 被创建! 37 // B 被创建! 38 // 输出 A 39 // 输出 B 40 41 42 43 /** 44 * 2. 45 */ 46 A a2 = new A(); 47 A b2 = new B(); 48 a2.t(); 49 b2.t(); 50 51 // result: A 被创建! 52 // A 被创建! 53 // B 被创建! 54 // 输出 A 55 // 输出 B 56 57 // /** 58 // * 3. 59 // */ 60 // 61 // A a3 = new A(); 62 // A b3 = new B(); 63 // b3.k(); 64 65 // result: 编译错误 66 67 68 69 } 70 71
     继承类加载顺序

    1
    import java.util.ArrayList; 2 import java.util.List; 3 import javax.swing.plaf.synth.SynthScrollBarUI; 4 public class Dome { 5 static class a{ 6 static { System.out.println(" static a");} 7 {System.out.println(" class a");} 8 public a() {System.out.println(" construct a ");} 9 public void t() {}; 10 } 11 static class b extends a{ 12 static { System.out.println(" static b");} 13 {System.out.println(" class b");} 14 public b() {System.out.println("construct b");} 15 public b(int i){this()} 16 public void t() { 17 System.out.println(super.getClass().getName()); 18 } 19 } 20 21 22 public static void main(String[] args) { 23 new b(); 24 25 } 26 } 27

     

     
    实现接口中的方法

    1
    package test; 2 3 import test.ExtendTest.A; 4 5 public class ImplementTest { 6 static interface A { 7 public void t(); 8 } 9 10 static class B implements A { 11 public B() { 12 System.out.println("B 被创建!"); 13 } 14 15 @Override 16 public void t() { 17 System.out.println("输出 B"); 18 } 19 20 public void k() { 21 System.out.println(" 方法 K"); 22 } 23 } 24 25 public static void main(String[] args) { 26 27 28 A b = new B(); 29 // 1. B 被创建! 30 31 b.t(); 32 // 2. 输出 B 33 34 // b.k(); 35 // 3. 编译错误 36 } 37 38 }
  • 相关阅读:
    自制游戏Zombie代码
    HNOI2020总结
    20200615题解:继续扮演
    20200611题解:树网的核
    历次考试总结
    寒假总结和省选大体规划
    每日总结
    有一种感动叫ACM(记WJMZBMR在成都赛区开幕式上的讲话)
    递推求欧拉函数的最简单的详解
    总结一些好用的C++小技巧
  • 原文地址:https://www.cnblogs.com/the-wang/p/8636398.html
Copyright © 2011-2022 走看看