标签:将不 rgs 继承 调用 ons 最好 def animal ror
1)第一种用法:super 关键字是父类对象的引用
package com.example; public class Person{ // 父类 public String name="张三"; public int age = 20; public void speak(){ System.out.println("Person: speak()"); } public void cry(){ System.out.println("Person: cry()"); } }
package com.example; public class Man extends Person{ // 子类 public void speak(){ System.out.println("Man: speak()"); super.speak(); // 调用父类的 speak cry(); // 调用父类的 cry System.out.println("name: " + name); // 调用父类的 name System.out.println("age: " + super.age); // 调用父类的 age } }
package com.example; public class Test{ public static void main(String[] args){ Man m = new Man(); m.speak(); } }
运行结果: Man: speak() Person: speak() Person: cry() name: 张三 age: 20
2)第二种用法:super() 或者 super(参数列表) 是父类的构造器
例1:
package com.example; public class Person{ // 父类 public Person(){ System.out.println("Person()"); } }
package com.example; public class Man extends Person{ // 子类 public Man(){ super(); // 父类构造器 System.out.println("Man()"); // super(); //error } }
package com.example; public class Test{ public static void main(String[] args){ new Man(); } }
运行结果: Person() Man()
例2:
package com.example; public class Person{ // 父类 public Person(int i){ System.out.println("Person" + "(" + i + ")"); } public Person(String str){ System.out.println("Person" + "(" + str + ")"); } }
package com.example; public class Man extends Person{ // 子类 public Man(){ super(1); // 调用父类构造器 System.out.println("Man()"); } public Man(int i){ super("张三"); // 调用父类构造器 System.out.println("Man" + "(" + i + ")"); } public Man(String str){ super(2); // 调用父类构造器 System.out.println("Man" + "(" + str + ")"); } }
package com.example; public class Test{ public static void main(String[] args){ new Man(); } }
运行结果: Person(1) Man()
1)Java 的访问控制权限有 public,protected,<default>,private 其中如果没有涉及继承关系时,protected 和 <default> 都只能在包内访问
package net.example; public class S{ public int i = 10; protected double d = 51.2; String str1 = "张三"; // <default> private String str2 = "李四"; }
package com.example; public class T{ public int i = 10; protected double d = 51.2; String str1 = "张三"; // <default> private String str2 = "李四"; }
package com.example; import net.example.S; // 导入 S 类 public class Test{ public static void main(String[] args){ S s1 = new S(); System.out.println("S: " + s1.i); //System.out.println("S: " + s1.d); // error //System.out.println("S: " + s1.str1); // error //System.out.println("S: " + s1.str2); // error T t1 = new T(); System.out.println("T: " + t1.i); System.out.println("T: " + t1.d); System.out.println("T: " + t1.str1); //System.out.println("T: " + t1.str2); // error } }
运行结果: S: 10 T: 10 T: 51.2 T: 张三
2)子类可以访问父类中用 protected 限制的属性和方法,即使子类和父类不在同一个包内
package net.example; public class Animals{ // 父类 protected int age = 2; String name = "旺财"; // <default> protected void bark(){ System.out.println("汪汪"); } }
package com.example; import net.example.Animals; // 导入 Animals 类 public class Dogs extends Animals{ // 子类 public Dogs(){ System.out.println(age); //System.out.println(name); // error bark(); } }
package com.example; public class Test{ public static void main(String[] args){ new Dogs(); } }
运行结果: 2 汪汪
package com.example; public class Person{ // 父类 public void p1(){ System.out.println("Person: p1"); } protected void p2(){ System.out.println("Person: p2"); } void p3(){ System.out.println("Person: p3"); } private void p4(){ System.out.println("Person: p4"); } }
package com.example; public class Man extends Person{ // 子类 public void p1(){ System.out.println("Man: p1"); } /* public void p2(){ System.out.println("Man: p2"); } */ protected void p2(){ System.out.println("Man: p2"); } /* void p2(){ // error, protected 的访问控制权限比 <default> 的高 System.out.println("Man: p2"); } */ /* public void p3(){ System.out.println("Man: p3"); } protected void p3(){ System.out.println("Man: p3"); } */ void p3(){ System.out.println("Man: p3"); } /* public void p4(){ System.out.println("Man: p4"); } protected void p4(){ System.out.println("Man: p4"); } void p4(){ System.out.println("Man: p4"); } */ private void p4(){ System.out.println("Man: p4"); } }
package com.example; public class Test{ public static void main(String[] args){ Man m = new Man(); m.p1(); m.p2(); m.p3(); //m.p4(); // error } }
运行结果: Man: p1 Man: p2 Man: p3
1)初始化顺序:父类静态属性 -> 子类静态属性 -> 父类非静态属性 -> 父类构造器调用 -> 子类非静态属性 -> 子类构造器调用
package com.example; public class T{ public T(int i){ System.out.println("T" + "(" + i + ")"); } }
package com.example; public class Person{ // 父类 public static T t1 = new T(1); // 静态属性 public T t2 = new T(2); // 非静态属性 public Person(){ System.out.println("Person()"); } public static T t3 = new T(3); // 静态属性 }
package com.example; public class Man extends Person{ // 子类 public static T t4 = new T(4); // 静态属性 public T t5 = new T(5); // 非静态属性 public Man(){ System.out.println("Man()"); } public static T t6 = new T(6); // 静态属性 }
package com.example; public class Test{ public static void main(String[] args){ new Man(); } }
运行结果: T(1) T(3) T(4) T(6) T(2) Person() T(5) Man()
1)Java 为了保证方法重写时不会出现误写成方法重载的情况,所以引入了 @Override
package com.example; public class Person{ // 父类 public void speak(int i){ System.out.println("Person: speak" + "(" + i + ")"); } }
package com.example; public class Man extends Person{ // 子类 /* @Override public void speak(String str){ System.out.println("Man: speak" + "(" + str + ")"); } */ // error,用了 @Override ,那么 @Override 下面的方法必须是方法重写,否则编译时会出错 @Override public void speak(int i){ System.out.println("Man: speak" + "(" + i + ")"); } }
package com.example; public class Test{ public static void main(String[] args){ Man m = new Man(); m.speak(10); } }
运行结果: Man: speak(10)
1)继承抽象类时,如果父类中有抽象方法,那么子类必须全部重写父类中的抽象方法
package com.example; public abstract class Person{ public abstract void p1(); // 抽象方法 public abstract void p2(); public void p3(){ System.out.println("Person: p3()"); } public void p4(){ System.out.println("Person: p4()"); } }
package com.example; public class Man extends Person{ @Override public void p1(){ } @Override public void p2(){ } }
1)由 final 关键字修饰的类不能被继承,例如:public final class Person{}
2)在继承关系中,如果父类的方法由 final 关键字修饰,那么该方法将不能被子类重写
参考资料:
《Java 编程思想》第4版
标签:将不 rgs 继承 调用 ons 最好 def animal ror
原文地址:https://www.cnblogs.com/huangzenglie/p/8837979.html