一、继承的实现 我们的子类可以通过继承获取父类的属性和方法,实例化是通过实例化的变量操作对象
//定义Person类 public class Person { String name; int age; String gender; public void say(){ System.out.println("人能说话"); } public void sleep(){ System.out.println("人能睡觉"); } } //定义Student类继承Person类 public class Student extends Person{ String StudentNO; public void study(){ System.out.println("学习"); } } //定义Teacher 类继承Person类 public class Teacher extends Person{ String TeacherNo; public void teach(){ System.out.println("教书"); } } //调用 Student s1=new Student(); Teacher t1=new Teacher(); s1.say(); t1.sleep();二、注:java只能单继承不能多继承,可以连续继承
//定义Person类 public class Person { String name; int age; String gender; public void say(){ System.out.println("人能说话"); } public void sleep(){ System.out.println("人能睡觉"); } } //定义Student类继承Person类 public class Student extends Person{ String StudentNO; public void study(){ System.out.println("学习"); } } //定义GoodStudent继承Student类 public class GoodStudent extends Student{} //调用 GoodStudent goodStudent=new GoodStudent(); goodStudent.sleep();三、方法的重写 当子类继承父类之后,子类调用父类的方法,但是如果父类额方法,不满足子类的需求,子类可以重写这个方法。 Person有南方人和北方人,南方人吃米饭,北方人吃馒头 重写的注意点:子类重写父类的方法,方法的声明必须和父类保持一致
//定义Person类 public class Person { String name; int age; String gender; public void eat(){ System.out.println("人要吃饭"); } } //北方人类继承Person类 public class NorthPerson extends Person{ @Override public void eat() { // TODO Auto-generated method stub System.out.println("北方人吃馒头"); } } //南方人类继承Person类 public class SouthPerson extends Person{ @Override public void eat() { // TODO Auto-generated method stub System.out.println("南方人吃米饭"); } } //调用 NorthPerson northPerson = new NorthPerson(); SouthPerson southPerson = new SouthPerson(); northPerson.eat(); southPerson.eat();四、Super super是一个关键字,代表父类的存储空间标识(可以理解为父类的引用)
//定义Person类 public class Person { String name; int age; String gender; String address="山东"; public void dosomthing(String thing){ System.out.println("人能"+thing); } public void eat(){ System.out.println("山东人吃煎饼卷大葱"); } public void sleep(){ System.out.println("人要睡觉"); } } //定义学生类使用,super调用父类的属性 public class Student extends Person{ public void print(){ System.out.println(super.address); } } //使用super调用父类的方法 public class Student extends Person{ public void print(){ System.out.println(super.address); //使用super调用父类的方法 super.eat(); } }五、继承中子类的构造方法
//定义Person类 public class Person { String name; int age; String gender; String address="山东"; //定义构造方法 public Person(String name,int age){ this.name=name; this.age=age; } public void dosomthing(String thing){ System.out.println("人能"+thing); } public void eat(){ System.out.println("山东人吃煎饼卷大葱"); } public void sleep(){ System.out.println("人要睡觉"); } } //此时要在Student中加上一个构造方法 public class Student extends Person{ public Student(String name, int age) { super(name, age); // TODO Auto-generated constructor stub } public void print(){ System.out.println(super.address); super.eat(); } } //调用 public class Demo { public static void main(String[] args) { Student student = new Student("zs",22); student.print(); System.out.println(student.name+student.age); } }总结:如果父类的构方法带有参数,则必须在子类的构造方法中通过super关键字调用父类的构造方法并加上合适的参数 如果父类构造方法没有参数,则在子类的构造方法中不需要使用super关键字调用父类构造方法,系统会自动调用父类的无参构造 标签:Java,继承,void,基础,System,Person,println,public,out From: https://www.cnblogs.com/YHSDDJM/p/16768272.html