继承:是对于类的抽象
子类继承父类,就会拥有父类的方法和属性。
快捷键:Ctrl+H查看继承树
主程序App.java
public class App { public static void main(String[] args) { //1.实例化对象 Student student = new Student(); //2.传递参数 student.test("魏君"); } }
父类Person.java
public class Person { protected String name = "june"; public void print(){ System.out.println("Person"); } }
子类Student.java
public class Student extends Person{ private String name="weijun"; public void test(String name){ //3.调用参数 System.out.println(name); //4.调用本类属性 System.out.println(this.name); //5.调用父类属性 System.out.println(super.name); } }
执行结果
魏君 weijun june
私有的方法和属性无法被继承
方法的继承主程序App.java
public class App { public static void main(String[] args) { //1.实例化对象 Student student = new Student(); //2.传递的参数 student.test(); } }
父类Person.java
public class Person { protected String name = "june"; public void print(){ System.out.println("Person"); } }
子类Student.java
public class Student extends Person{ private String name="weijun"; public void print(){ System.out.println("Student"); } public void test(){ //3.调用类方法 print(); //4.调用本类方法 this.print(); //5.调用父类方法 super.print(); } }
执行结果
Student Student Person
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)