java中继承中构造方法_Java——继承中的构造方法

package TestExtends;

class Person{

private String name;

private String address;

private String phone;

private String email;

public Person() {

}

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 getPhone() {

return phone;

}

public void setPhone(String phone) {

this.phone = phone;

}

public String getEmail() {

return email;

}

public void setEmail(String email) {

this.email = email;

}

public Person(String name, String address) {

this.name = name;

this.address = address;

}

public Person(String name, String address, String phone, String email) {

this(name, address);

this.phone = phone;

this.email = email;

}

public String toString() {

return "姓名:" + name + '\n' + "住址:" + address + '\n' + "电话:" + phone + '\n' + "邮箱:" + email + '\n';

}

}

class Student extends Person{

private int grade;

public Student(){

}

public Student(String n, String a, String p, String e, int grade){

super(n, a, p, e);

this.grade = grade;

}

public String toString() {

return super.toString() + "年级:" + grade + '\n';

}

}

class MyDate{

private int year;

private int month;

private int day;

public MyDate() {

}

public MyDate(int year, int month, int day) {

this.year = year;

this.month = month;

this.day = day;

}

public String toString() {

return year + "年" + month + "月" + day + "日";

}

}

class Employee extends Person{

private String office;

private double salary;

private MyDate date;

public Employee() {

}

public String getOffice() {

return office;

}

public void setOffice(String office) {

this.office = office;

}

public double getSalary() {

return salary;

}

public void setSalary(double salary) {

this.salary = salary;

}

public MyDate getDate() {

return date;

}

public void setDate(MyDate date) {

this.date = date;

}

public Employee(String n, String a, String p, String e, String office, double salary, MyDate date) {

super(n, a, p, e);

this.office = office;

this.salary = salary;

this.date = date;

}

public String toString() {

return super.toString() + "办公室:" + office + '\n' + "工资:" + salary + '\n' + "受聘日期:" + date.toString() + '\n';

}

}

class Faculty extends Employee{

private String office_hours;

private int level;

public Faculty() {

}

public String getOffice_hours() {

return office_hours;

}

public void setOffice_hours(String office_hours) {

this.office_hours = office_hours;

}

public int getLevel() {

return level;

}

public void setLevel(int level) {

this.level = level;

}

public Faculty(String n, String a, String p, String e, String o, double s, MyDate d, String office_hours, int level) {

super(n, a, p, e, o, s, d);

this.office_hours = office_hours;

this.level = level;

}

public String toString() {

return super.toString() + "办公时间:" + office_hours + '\n' +"级别:" + level + '\n';

}

}

class Staff extends Employee{

private String title;

public Staff() {

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public Staff(String n, String a, String p, String e, String o, double s, MyDate d, String title) {

super(n, a, p, e, o, s, d);

this.title = title;

}

public String toString() {

return super.toString() + "头衔:" + title + '\n';

}

}

public class TestExtends {

public static void main(String[] args) {

Person p = new Person();

p.setName("赵钱");

p.setAddress("北京市朝阳区");

p.setPhone("12345678901");

p.setEmail("zhaoqian@163.com");

System.out.println(p.toString());

System.out.println("- - - - - - - - - - - - - - - - - 分割线- - - - - - - - - - - - - - - - -");

Student s = new Student("孙李", "北京市海淀区", "13456789012","sunli@126.com", 1);

System.out.println(s.toString());

System.out.println("- - - - - - - - - - - - - - - - - 分割线- - - - - - - - - - - - - - - - -");

Employee e = new Employee();

e.setName("周吴");

e.setAddress("北京市西城区");

e.setPhone("14567893201");

e.setEmail("zhouwu@bistu.edu.cn");

e.setOffice("网工系办公室");

e.setSalary(7500);

e.setDate(new MyDate(2000,11,8));

System.out.println(e.toString());

System.out.println("- - - - - - - - - - - - - - - - - 分割线- - - - - - - - - - - - - - - - -");

Faculty f = new Faculty("郑王", "北京市东城区", "15678943210", "zhengwang@163.com", "计科系办公室", 8000, new MyDate(1999,9,21), "9:00—17:00", 1);

System.out.println(f.toString());

System.out.println("- - - - - - - - - - - - - - - - - 分割线- - - - - - - - - - - - - - - - -");

Staff sf = new Staff("冯陈", "北京市朝阳区", "16789543210", "fengchen@126.com", "软工系办公室", 9000, new MyDate(2000,11,28), "管理员");

System.out.println(sf.toString());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值