上机5.21

1、设计个类,分别是:(知识点:抽象类及抽象方法)

1Shape表示图形类,有面积属性area、周长属性per,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor

22个子类:

1Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPergetAreashowAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。

2Circle表示圆类,增加1个属性,radius表示半径,重写getPergetAreashowAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。

 3)一个测试类PolyDemo,在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。

2Cola公司的雇员分为以下若干类:(知识点:多态)

(1) ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。

Ÿ 方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。

(2) SalariedEmployee ColaEmployee 的子类,拿固定工资的员工。

Ÿ 属性:月薪

(3) HourlyEmployee ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。

Ÿ 属性:每小时的工资、每月工作的小时数

(4) SalesEmployee ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。

Ÿ 属性:月销售额、提成率

(5) 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。

package qwer;

public class ColaEmployee {


private String name;
private int months;
private int birthday;


public String getName() {
	return name;
}


public void setName(String name) {
	this.name = name;
}


public int getMonths() {
	return months;
}


public void setMonths(int months) {
	this.months = months;
}


public int getBirthday() {
	return birthday;
}


public void setBirthday(int birthday) {
	this.birthday = birthday;
}


public ColaEmployee(String name, int months,int birthday) {
	super();
	this.name = name;
	this.months = months;
	this.birthday = birthday;
}


void getSalary() {
	

}}

package qwer;

public class Company {
	ColaEmployee[]Z=new ColaEmployee[6]
	Z[0]=new SalariedEmployee("zxc", 3, 3, 2000);
	Z[1]=new HourlyEmployee("zx", 5, 7, 175, 13);
	Z[2]=new SalariedEmployee("zxcv", 4, 5, 3000);
	Z[3]=new SalariedEmployee("zxcvv", 8, 7, 3000);
	Z[4]=new SalesEmployee("zxcvb", 5, 5, 30000, 0.1);
	Z[5]=new SalesEmployee("zxcvbn",5, 5, 30000, 0.1);
}}


package qwer;

public class HourlyEmployee extends  ColaEmployee {
	private double hours;
	private int mon;
public HourlyEmployee(String name, int months, int birthday,double hours,int mon) {
	super(name, mon, mon);
	this.hours=hours;
	this.mon=mon;
	if(super.getBirthday()!=super.getMonths()&&super.getMonths()>=160) {
	System.out.println("员工"+name+months+"月的工资为"+(mon*160+(hours-160)*mon*1.5));
}else  if(super.getBirthday()==super.getMonths()&&super.getMonths()>=160){
	System.out.println("员工"+name+months+"月的工资为"+(mon*160+(hours-160)*mon*1.5+100));
}else if(super.getBirthday()==super.getMonths()&&super.getMonths()<160) {
	System.out.println("员工"+name+months+"月的工资为"+(mon*hours+100));
}else if(super.getBirthday()!=super.getMonths()&&super.getMonths()<160) {
	System.out.println("员工"+name+months+"月的工资为"+mon*hours);
}
}
}




package qwer;

public class SalariedEmployee extends ColaEmployee {

	private double money;
	public SalariedEmployee(String name, int months, int birthday,double money) {
		super(name, months, birthday);
		if(super.getMonths()==super.getBirthday()) {
			System.out.println("员工"+name+months+"月的工资为"+(money+100));
		}else {
			System.out.println("员工"+name+months+"月的工资为"+money);
		}
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}

}


package qwer;

public class SalesEmployee extends ColaEmployee {
	private double x;
	private double L;
public SalesEmployee(String name, int months, int birthday,double x,double L) {
super(name, months, birthday);
if(super.getMonths()==super.getBirthday()) {
System.out.println("员工"+super.getName()+"工资为"+x*L+100);
}else{
System.out.println("员工"+super.getName()+"工资为"+x*L);
}

}

}

  

3利用接口实现动态的创建对象:(知识点:接口

1)创建4个类

1苹果

2香蕉

3葡萄

4园丁

2)在三种水果的构造方法中打印一句话.

以苹果类为例

class apple

{

public apple()

{

System.out.println(“创建了一个苹果类的对象”);

}

}

3)类图如下:

4)要求从控制台输入一个字符串,根据字符串的值来判断创建三种水果中哪个类的对象。

运行结果如图:

猜你喜欢

转载自www.cnblogs.com/zxnm1234/p/12932793.html