Java实验三类的继承与派生

1.定义一个Person类,包含姓名(name)、性别(sex)、年龄(age)等字段;继承Person类设计Teacher 类,增加职称(pro)、部门(department)等字段;继承Person类设计Student类,增加学号(no)、入学时间(enter)、专业(major)等字段,定义各类的构造方法和toString()方法,并分别创建对象进行测试.

输出样例结果如下:

package step1;

import java.util.Scanner;

class Person {
    String name;
	String sex;
	int age;

	public Person(String name, String sex, int age) {
		this.name = name;
		this.sex = sex;
		this.age = age;
	}                                                
}

class Student extends Person {
    String no;
	String enter;
	String major;

	public Student(String name, String sex, int age, String no, String enter, String major) {
		super(name, sex, age);
		this.no = no;
		this.enter = enter;
		this.major = major;
	}

	@Override
	public String toString() {
		return   name + "," + sex + "," + age + "," + no + "," + enter + "," + major;
	}                                               
}

class Teacher extends Person {
    String pro;
	String department;

	public Teacher(String name, String sex, int age, String pro, String department) {
		super(name, sex, age);
		this.pro = pro;
		this.department = department;
	}

	@Override
	public String toString() {
		return  name + "," + sex + "," + age + "," + pro + "," + department;
	}                                                
}

public class Lab3_1 {
	public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
		Student student = new Student(sc.next(), sc.next(), sc.nextInt(), sc.next(), sc.next(), sc.next());
		Teacher teacher = new Teacher(sc.next(), sc.next(), sc.nextInt(), sc.next(), sc.next());
		System.out.println("学生基本信息为:" + student);
		System.out.println("教师的信息为:" + teacher);
		sc.close();	                                                     
	}
}	

2.定义一个抽象(abstract)类,类名为Employee。 Employee的子类有YearWorker、MonthWorker和WeekWorker。YearWorker对象按年领取薪水(每年10万),MonthWorker对象按月领取薪水(每月1万),WeekWorker对象按周领取薪水(每周0.5万,设定一个月4周)。Employee类有一个抽象(abstract)方法:public abstract double earnings();

子类必须重写父类的earnings()方法,给出各自领取每年报酬的具体方式。

有一个Company类,该类用Employee数组作为成员,Employee数组的单元可以是YearWorker对象、MonthWorker对象、WeekWorker对象的上转型对象。程序能输出Company对象一年需要支付的薪水总额。

package step2;



abstract class Employee {
	public abstract double earnings();
}

class YearWorker extends Employee {

	@Override
	public double earnings() {
		return 100000;
	}
	
}

class MonthWorker extends Employee {

	@Override
	public double earnings() {
		
		return 10000*12;
	}
	
}

class WeekWorker extends Employee {

	@Override
	public double earnings() {

		return 5000*4*12;
	}

}

class Company {
	Employee[] employees;
	double salaries = 0;

	Company(Employee[] employees) {
		this.employees = employees;
	}

	public double salariesPay() {
		salaries = 0;
		for (Employee employee : employees) {
            salaries += employee.earnings();
        }
		return salaries;
	}
}

public class HardWork {
	public static void main(String[] args) {
		Employee[] employees = new Employee[20];
		for (int i = 0; i < employees.length; i++) {
			if (i % 3 == 0)
				employees[i] = new WeekWorker();
			else if (i % 3 == 1)
				employees[i] = new MonthWorker();
			else if (i % 3 == 2)
				employees[i] = new YearWorker();
		}
		Company company = new Company(employees);
		System.out.println("公司年工资总额:" + company.salariesPay());
	}
}

3.定义一个接口,其屮包含一个 display() 方法用于显示信息;通知类(Inform)、汽车类(Car)、广告类(Adervise)均要实现该接口,以显示“通知内容”、“汽车油量”、“广告消息”。试编程实现并测试类的设计,创建的对象用接口引用,并通过接口引用变量执行 display() 方法。

package step4;

public class Student{

	private String sno;
	private String sname;
	private String sdept;
	
	public Student() {
		
	}
	public Student(String sno, String sname, String sdept) {
		this.sno = sno;
		this.sname = sname;
		this.sdept = sdept;
	}
	public String getSno() {
		return sno;
	}
	public void setSno(String sno) {
		this.sno = sno;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getSdept() {
		return sdept;
	}
	public void setSdept(String sdept) {
		this.sdept = sdept;
	}
	
	@Override
	public String toString() {
		return "学号: " + sno + "\t姓名: " + sname + "\t系部: " + sdept ;
        
	}
	
	

}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值