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

抽象類的繼承

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

import pPack.Person;

class Teacher extends Person {

    private int salary;

    public Teacher(int Id,String name,int height,int age,int salary) {
        this.Id=Id;
        this.name=name;
        this.height=height;
        this.age=age;
        this.salary=salary;

    }
    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

  /*  private String getShowBasics() {
        return "Teacher{" +
                ", Id=" + Id +
                ", name='" + name + '\'' +
                ", height=" + height +
                ", age=" + age +
                '}';
    }

    private String getShowPrivacy() {
        return "Teacher{" +
                "salary=" + salary +
                '}';
    }

    Teacher teacher = new Teacher();




void showBasics() {
        System.out.println(teacher.getShowBasics());
    }

    void showPrivacy() {
        System.out.println(teacher.getShowPrivacy());
    }

    /**
     * 佈置作業
     */
   /* public void assignment() {
        System.out.println("開始佈置作業啦!");
    }*/

    public void showBasics(){
        System.out.println("佈置作業了!");
    }

    public void showPrivacy() {
        System.out.println("工資:" + this.getSalary()  );
    }
}

 

 

package pPack;/*
@author wb    
@great forever
i think,therefor i am
*/
public  abstract  class Person {
    //編號
    public int Id;
    //姓名
    public String name;
    //身高
    public int height;
    //年齡
    public int age;
    public Person() {}

    public Person(int Id,String name,int height,int age) {
    this.Id=Id;
    this.name=name;
    this.height=height;
    this.age=age;

    }
    public int getId() {
        return Id;
    }

    public void setId(int Id) {
        this.Id = Id;
    }
    public String getName() {
        return name;
    }
    public int Height() {
        return height;
    }

    public void Height(int height) {
        this.height = height;
    }
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public String toString() {
        return "Person [id="+Id+",name=" + name + ", height="+height+",age=" + age + "]";
    }
    public void display() {

        System.out.println( this );                //公共部分
        System.out.println("-------------------201627256 吳勃-----------------");
        showBasics();
        showPrivacy();                     //開發人員具體的實現


    }
    //輸出基本資訊
    public abstract void showBasics();

    //輸出隱私資訊
    public abstract void showPrivacy();
}

 

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

import pPack.Person;


public class Student extends Person {

    private int score;
    public Student(int Id,String name,int height,int age,int score) {
        this.Id=Id;
        this.name=name;
        this.height=height;
        this.age=age;
        this.score=score;

    }

    public int getScore() {
        return score;
    }

   /* public String getShowBasics() {
        return "Student{" +
                "Id=" + Id +
                ", name='" + name + '\'' +
                ", height=" + height +
                ", age=" + age +
                '}';
    }

    private String getShowPrivacy() {
        return "Student{" +
                "score=" + score +
                '}';
    }

    Student student = new Student();


    void showBasics() {
        System.out.println(student.getShowBasics());
    }


    void showPrivacy() {
        System.out.println(student.getShowPrivacy());
    }

    //做作業
   public void doHomeWork() {
        System.out.println("做作業");
    }*/
   public void showBasics(){
       System.out.println("做作業了!");
   }

    public void showPrivacy() {
        System.out.println("分數:" + this.getScore()  );
    }
}
package tsPack;/*
@author wb    
@great forever
i think,therefor i am
*/
import pPack.Person;
public class zhuhanshu {
    public static void main(String[] args) {

        Person a = new Teacher(1, "劉榮香", 168, 28, 20000);
        a.display();

        Person b = new Student(201627256, "吳勃", 175, 21, 99);
        b.display();

    }
}