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;
                }
}

猜你喜欢

转载自blog.csdn.net/j_linlian/article/details/79836280