【Java SE】super 关键字详解
super 关键字详解super 的本质与作用super 的三种用法a) 调用父类构造方法b) 访问父类属性b) 调用父类方法深入理解 super 的查找机制查找路径规则super 与 this 的完整对比常见陷阱陷阱1在静态方法中使用 super陷阱2super 与多态的误解陷阱3构造方法中 this 和 super 的冲突super 的本质与作用super是 Java 中的一个引用变量用于直接引用父类对象。它不是一个独立的对象而是当前对象中对父类部分的引用。核心作用解决子类与父类中成员属性、方法的命名冲突显式调用父类的构造方法实现父类功能的复用与扩展super 的三种用法用法示例说明调用父类构造方法super(参数)必须在子类构造方法第一行访问父类属性super.属性名当子类属性隐藏父类属性时使用调用父类方法super.方法名()当子类重写父类方法时使用a) 调用父类构造方法子类构造方法需要初始化继承自父类的属性或调用父类特定的构造逻辑。语法规则必须写在子类构造方法的第一行如果子类构造方法中没有显式调用super(...)编译器会自动插入super()调用父类的无参构造如果父类没有无参构造子类必须显式调用父类的带参构造classAnimal{Stringname;intage;// 父类带参构造publicAnimal(Stringname,intage){this.namename;this.ageage;System.out.println(父类构造被调用);}publicAnimal(){System.out.println(父类无参构造被调用);}}classDogextendsAnimal{Stringbreed;// 显式调用父类带参构造publicDog(Stringname,intage,Stringbreed){super(name,age);// 必须第一行this.breedbreed;}// 默认调用父类无参构造publicDog(){// 隐式调用 super()System.out.println(子类构造被调用);}}执行顺序验证publicclassTest{publicstaticvoidmain(String[]args){Dogdog1newDog(旺财,3,金毛);System.out.println(---);Dogdog2newDog();}}输出父类构造被调用 --- 父类无参构造被调用 子类构造被调用b) 访问父类属性子类定义了与父类同名的属性属性隐藏需要访问父类的属性时。classParent{Stringname父类名字;StringparentOnly父类特有属性;}classChildextendsParent{Stringname子类名字;// 隐藏了父类的 namevoidprintNames(){System.out.println(直接访问 name: name);// 子类System.out.println(this.name: this.name);// 子类System.out.println(super.name: super.name);// 父类System.out.println(父类特有属性: parentOnly);// 可直接访问}}注意事项super不能跨级访问祖父类的属性只能直接访问父类如果父类属性是private则super也无法直接访问b) 调用父类方法子类重写了父类方法但仍需使用父类的原有功能在子类方法中扩展父类方法的功能classCalculator{protectedintadd(inta,intb){returnab;}protectedvoidshowInfo(){System.out.println(这是计算器类);}}classAdvCalculatorextendsCalculator{// 重写并扩展父类方法Overrideprotectedintadd(inta,intb){// 先调用父类方法再添加额外逻辑intresultsuper.add(a,b);System.out.println(计算结果: result);returnresult;}OverrideprotectedvoidshowInfo(){super.showInfo();// 复用父类逻辑System.out.println(这是高级计算器类);}}输出验证publicclassTest{publicstaticvoidmain(String[]args){AdvCalculatorcalcnewAdvCalculator();calc.add(3,5);calc.showInfo();}}输出计算结果: 8 这是计算器类 这是高级计算器类深入理解 super 的查找机制查找路径规则classGrandParent{voidmethod(){System.out.println(GrandParent);}}classParentextendsGrandParent{voidmethod(){System.out.println(Parent);}}classChildextendsParent{voidmethod(){System.out.println(Child);}voidtest(){// super 从直接父类开始向上查找super.method();// 调用 Parent 的 method// 无法直接调用 GrandParent 的 method需要间接方式}voidcallGrandParent(){// 间接调用祖父类的方法((GrandParent)this).method();// 注意这是运行时绑定实际仍是调用子类的// 正确方式通过父类的 supernewParent().method();// 创建父类实例调用}}重要理解super不是独立对象不能写super.super.method()super遵循静态绑定编译时确定调用哪个类的方法当调用super.method()时直接查找父类的方法定义不会向下查找super 与 this 的完整对比比较维度superthis查找起点从父类开始向上查找从当前类开始向上查找适用范围父类构造、父类成员本类构造、本类成员独立性不是独立对象只是引用当前对象的引用在构造方法中调用父类构造必须在第一行调用本类其他构造必须在第一行能否同时使用不能同时使用都需在第一行不能同时使用都需在第一行静态上下文中不能使用不能使用访问权限受父类访问修饰符限制受本类访问修饰符限制代码演示classBase{StringnameBase;voidshow(){System.out.println(Base show);}}classDerivedextendsBase{StringnameDerived;voidshow(){System.out.println(Derived show);}voiddemonstrate(){// this 和 super 的对比System.out.println(this.name);// DerivedSystem.out.println(super.name);// Basethis.show();// Derived showsuper.show();// Base show// 类型比较System.out.println(thisinstanceofBase);// true// System.out.println(super instanceof Base); // 编译错误super不能用于instanceof}}常见陷阱陷阱1在静态方法中使用 superclassTest{staticvoidstaticMethod(){// super.toString(); // 编译错误不能在静态上下文使用 super}publicstaticvoidmain(String[]args){// 不能在静态上下文使用 super}}陷阱2super 与多态的误解classParent{voidmethod(){System.out.println(Parent);}}classChildextendsParent{voidmethod(){System.out.println(Child);}voidcallParent(){// 即使子类重写了方法super.method() 一定调用父类的super.method();// 输出 Parent}}陷阱3构造方法中 this 和 super 的冲突classChildextendsParent{Child(){// this(hello); // 如果调用本类其他构造// super(); // 就不能同时调用 super()// 两者只能选其一}Child(Strings){super();// 必须第一行}}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2419459.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!