JAVA 第二章继承
今天学习的Java的继承:使用继承可以优化代码,将重复代码抽取到父类当中,减少代码量,方便修改代码。
运用了修改属性可见性 – 设为private,创建get/set方法 – 用于属性读写,使用super关键字
父类
// An highlighted block
package java;
public abstract class Father {
String name;
private int age;
private final String play=" 球"
public Father(){
this.name="益";
this.age=100;
}
public void father (String name,int age){
this.name=name;
this.age=age;
}
public int getAge(){
return age;
}
public void setAge (int age){
this.age=age;
}
public void print (String name,int age){
}
public void print(){
System.out.println(1);
}
public abstract void eat();
子类
public class Dog extends Father {
public void eat() {
super.print();
super.setAge(0);
super.getAge();
System.out.println(getAge());
}
public void print(){
System.out.println(2);
}
public static void main(String[] args) {
Father father =new Father();
father.print();
father.eat();
}
}