"Java Programming" Experiment 6 - Interface Application, Inheritance and Interface Implementation, Abstract Method

Experiment 6 of "Java Programming"

1. Interface application: interface inheritance and interface implementation

Specific requirements:

(1) The breath() abstract method is defined in the Biology biological interface;

package homework.test;
public interface Biology {
    
    
	public void breathe();
}

(2) The Animal animal interface inherits the Biology interface, adding two abstract methods eat() and sleep();

package homework.test;
public interface Animal extends Biology {
    
    
	public void eat();
	public void sleep();
}

(3) The Human interface inherits the Animal interface and adds two abstract methods, think() and learn();

package homework.test;
public interface Human extends Animal {
    
    
	public void think();
	public void learn();
}

(4) Define an ordinary human Person to implement the Human interface and test it.

package homework.test;
public class Person implements Human {
    
    
	@Override
	public void eat() {
    
    
		// TODO Auto-generated method stub
		System.out.println("吃");
	}

	@Override
	public void sleep() {
    
    
		// TODO Auto-generated method stub
		System.out.println("睡");
	}

	@Override
	public void breathe() {
    
    
		// TODO Auto-generated method stub
		System.out.println("呼吸");
	}

	@Override
	public void think() {
    
    
		// TODO Auto-generated method stub
		System.out.println("思考");
	}

	@Override
	public void learn() {
    
    
		// TODO Auto-generated method stub
		System.out.println("学习");
	}
	public static void main(String[] args) {
    
    
		Person person = new Person();
		person.eat();
		person.sleep();
		person.breathe();
		person.think();
		person.learn();
	}
}

2. Dynamic polymorphic application: enterprise employee salary management

Specific requirements: Employees of a company are divided into the following categories:

(1) Employee employee class, including protected attributes name (name), birthDate (date of birth), public double
getSalary (Employee e) method (this method returns employees of different categories according to the salary calculation method of this month salary);

package homework.test;
public class Employee {
    
    
	protected String name;
	protected String birthDate;
	
	public String getName() {
    
    
		return name;
	}

	public void setName(String name) {
    
    
		this.name = name;
		System.out.println("姓名为:"+name);
	}

	public double getSalary(Employee e) {
    
    
		return 0;
	}
	
	public static void main(String[] args) {
    
    
        Employee a = new SalariedEmployee(2000);
        System.out.println("SalariedEmployee:"+a.getSalary(a));
        Employee b = new HourlyEmployee(200, 200);
        System.out.println("HourlyEmployee:"+b.getSalary(b));
        Employee c = new SalesEmployee(200000,0.5);
        System.out.println("SalesEmployee:"+c.getSalary(c));
        Employee d = new BasePlusSalesEmployee(20000,0.5, 4000);
        System.out.println("BasePlusSaleEmployee:"+d.getSalary(d));
    }
}

(2) The SalariedEmployee class inherits from the Employee class, representing employees who receive a fixed salary, including the private attribute salary (fixed salary);

package homework.test;
public class SalariedEmployee extends Employee {
    
    
	private double salary;
	
	public SalariedEmployee(double salary) {
    
    
		this.salary = salary;
	}
	public double getSalary(Employee e) {
    
    
		return this.salary;
	}
}

(3) The HourlyEmployee class inherits the Employee class, representing employees who are paid by the hour, including private attributes hourSalary (hourly salary), hours (the number of hours worked per month). Salary calculation method: monthly salary = hourSalary * hours, the part of the monthly work exceeding 160 hours will be paid at 1.5 times the hourly salary.

package homework.test;
public class HourlyEmployee extends Employee {
    
    
	private double hourSalary;
	private double hours;
	
	public HourlyEmployee(double hourSalary,double hours) {
    
    
		this.hours = hours;
		this.hourSalary = hourSalary;
	}
	public double getSalary(Employee e) {
    
    
		if(hours > 160) {
    
    
			return hourSalary * ((hours - 160) * 1.5 + 160);
		}
		else {
    
    
			return hours * hourSalary;
		}
	}
}

(4) The SalesEmployee class inherits from the Employee class, representing the salesperson, including the protected attributes sale (monthly sales), commissionRate (commission rate). Salary calculation method: monthly salary = sale * commissionRate.

package homework.test;
public class SalesEmployee extends Employee {
    
    
	protected double sale;
	protected double commissionRate;

	public SalesEmployee(double sale, double commissionRate) {
    
    
		super();
		this.sale = sale;
		this.commissionRate = commissionRate;
	}

	public double getSalary(Employee e) {
    
    
		return sale * commissionRate;
	}
}

(5) The BasePlusSalesEmployee class inherits the SaleEmployee class, which represents a salesperson with a fixed basic salary, and the salary consists of a basic salary plus a sales commission. Its private attribute basicSalary (basic salary). Salary calculation method: monthly salary = basicSalary + sale * commissionRate.

package homework.test;
public class BasePlusSalesEmployee extends SalesEmployee {
    
    
	private double basicSalary;

	public BasePlusSalesEmployee(double sale, double commissionRate, double basicSalary) {
    
    
        super(sale, commissionRate);
        this.basicSalary = basicSalary;
    }

    public double getSalary(Employee e) {
    
    
        return basicSalary+super.sale*super.commissionRate;
    }
}

Guess you like

Origin blog.csdn.net/mercury8124/article/details/129252044