java設計一個名爲person的類和它的兩個名爲student和employee的子類

person,student,employee,faculty和staff類,設計一個名爲person的類和它的兩個名爲student和employee的子類。employee類又有子類:教員類faculty和職員類staff。每個人都有姓名,地址電話號碼和電子郵件地址。學生有班級狀態。將這些常量定義爲變量。—–java語言程序設計11.2

這裏沒有把常量定義爲變量而是輸入任意值

import java.util.*;
public class eleven112 {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.print("student or employee?0 or 1:");
        if(input.nextInt()==0) {
            System.out.print("enter the classstate:");
            student a=new student();
            String classstate=input.nextLine();
            a.setclasstate(classstate);
            input.nextLine();
            System.out.print("enter the name address tel email:");
            String name=input.nextLine();
            String address=input.nextLine();
            String tel=input.nextLine();
            String email=input.nextLine();
            person b=new person();
            b.setall(name, address, tel, email);
            System.out.println("classstate: "+a.get()+b.toString());
        }
        else {
            System.out.print("enter salary: ");
            double salary=input.nextDouble();
            input.nextLine();
            System.out.print("enter office ");

            String office=input.nextLine();

            staff c =new staff();
            c.em(office, salary);
            System.out.print("enter the chenghao:");
            c.chenghao=input.nextLine();
            person d=new person();
            System.out.print("enter the name address tel email:");
            String name=input.nextLine();
            String address=input.nextLine();
            String tel=input.nextLine();
            String email=input.nextLine();
            d.setall(name, address, tel, email);
            System.out.println(d.toString());
            System.out.println(c.toString());

        }

    }
}

class student extends person{
    private String classstate;
    public void setclasstate(String classtate) {
        this.classstate=classtate;
    }
    public String get() {
        return classstate;
    }
}
class employee extends person{
    String office;
    double salary;
    public void em(String office,double salary) {
        this.office=office;
        this.salary=salary;
    }
    public String get() {
        return "office: "+office+"salary: "+salary;
    }

}
class staff extends employee{
    String chenghao;
    public String toString() {
        return "office:"+super.office+"salary: "+super.salary+"稱號:"+chenghao;
    }

}
class person{
    String name,address,tel,email;
        public void setall(String name,String address,String tel,String email) {
        this.name=name;
        this.tel=tel;
        this.address=address;
        this.email=email;

    }
    public String toString() {
        return "name: "+name+"address: "+address+"tel: "+tel+"email: "+email;
                }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章