1. 程式人生 > >java抽象類的繼承

java抽象類的繼承

package abstractjava;/*
@author wb    
@great forever
i think,therefor i am
*/



public abstract class Person {

    private String name;       //姓名
    private int age;           //年齡
    private String sex;
    private String address;    //地址
    private String telephone;  //電話

    //定義構造方法

    public Person() {}

    public Person(String name, int age, String sex, String address,
                  String telephone) {

        this.name = name;
        this.age = age;
        this.sex = sex;
        this.address = address;
        this.telephone = telephone;
    }

    //定義setter()和getter()方法

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    //覆寫toString()方法
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", sex=" + sex
                + ", address=" + address + ", telephone=" + telephone + "]";
    }

    //使用模板模式,定義好簡單模板,列印資訊
    /*
     * 模板模式為面向物件設計模式之一,其大概思想(個人理解):
     * 在抽象方法中完成已經確定的內容,而不確定的部分,寫成抽象方法並呼叫
     * 而抽象方法的具體實現,交給繼承他的類去具體實現,
     * 這樣開發人員只要寫好自己的實現部分便可,然後在主方法中,
     * 通過例項化後的抽象類直接呼叫模板方法便可
     *
     * 以下方法只是一個簡單的表示
     *
     * */
    public void display() {

        System.out.println( this );                //公共部分
        System.out.println("-------------------風騷java分割線-----------------");
        getInfo();            //開發人員具體的實現
        System.out.println("-------------------風騷java分割線-----------------");
        System.out.println( "本輸出方法採用模板設計模式,以上為學生資訊!" );    //公共部分

    }

    //宣告抽象方法,列印子類的新增資訊
    public abstract void getInfo();


}

 

package abstractjava;/*
@author wb    
@great forever
i think,therefor i am
*/



public class Student extends Person {

    private String sno;            //學號
    private String className;   //班級

    //定義構造方法
    public Student(String name, int age, String sex, String address,
                   String telephone,String sno,String className) {

        super(name,age,sex,address,telephone);
        this.sno = sno;
        this.className = className;
    }


    //定義setter()和getter()方法
    public String getSno() {
        return sno;
    }

    public void setSno(String sno) {
        this.sno = sno;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }


    //實現父類抽象方法

    public void getInfo() {

        System.out.println( "學號:" + this.getSno() + "\t班級:" + this.getClassName() );
    }

}

 

 

package abstractjava;/*
@author wb    
@great forever
i think,therefor i am
*/

public class main {

    public static void main(String[] args) {
        Person a=new Student("吳勃",23,"男","中國","23432","25038","tongx");
        a.display();
    }
}