A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 仲德明 中级黑马   /  2015-6-3 22:20  /  300 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

实现一个名为Person的类和它的两个子类Student和Employee。Employee有子类Faculty和Staff。Person中的人有姓名、地址和电话号码。 Student中的学生有班级状态(一、二、三、四年级)。将这些状态定义为常量。 Employee中的雇员有办公室、工资。Faculty中的教员有级别。Staff中的职员有职务称号。覆盖每个类中的toString方法,显示类名和人名。
public class Person {
        private String name;
        private String address;
        private String number;
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public String getAddress() {
                return address;
        }
        public void setAddress(String address) {
                this.address = address;
        }
        public String getNumber() {
                return number;
        }
        public void setNumber(String number) {
                this.number = number;
        }
        public String toString(){
                return this.getClass()+"\nname::"+name+"\naddress::"+getAddress()+"\nnumber::"+getNumber();
        }
}
package shiyanliu;
public class Student extends Person{
        private final String GRADE_1 = "一年级";
        private final String GRADE_2 = "二年级";
        private final String GRADE_3 = "三年级";
        private final String GRADE_4 = "四年级";
        public String getGRADE_1() {
                return GRADE_1;
        }
        public String getGRADE_2() {
                return GRADE_2;
        }
        public String getGRADE_3() {
                return GRADE_3;
        }
        public String getGRADE_4() {
                return GRADE_4;
        }
       
        public String toString(){
                return super.toString()+"\ngrade::"+getGRADE_2();
        }
}

package shiyanliu;
public class Employee extends Person{
        private String office;
        private double pay;
        public String getOffice() {
                return office;
        }
        public void setOffice(String office) {
                this.office = office;
        }
        public double getPay() {
                return pay;
        }
        public void setPay(double pay) {
                this.pay = pay;
        }
        public String toString(){
                return super.toString()+"\noffice::"+getOffice()+"\npay::"+getPay()+"元";
        }
}
package shiyanliu;
public class Faculty extends Employee{
        private String title;
        public String getTitle(){
                return title;
        }
        public void setTitle(String title){
                this.title = title;
        }
        public String toString(){
                return super.toString()+"\ntitle::"+getTitle();
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马