2018/12/12

2018/12/12

1.定义一个汽车类Vehicle,
2.1.1 属性包括:汽车品牌brand(String类型)、颜色color(String类型
)和速度speed(double类型)。
2.1.2 至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度
的初始值必须为0)。
2.1.3 为属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。
2.1.4 定义一个一般方法run(),用打印语句描述汽车奔跑的功能
2.1.5 在main方法中创建一个品牌为“benz”、颜色为“black”的汽车。
2.2 定义一个Vehicle类的子类轿车类Car,要求如下:
2.2.1 轿车有自己的属性载人数loader(int 类型)。
2.2.2 提供该类初始化属性的构造方法。
2.2.3 重新定义run(),用打印语句描述轿车奔跑的功能。
2.2.4 在main方法中创建一个品牌为“Honda”、颜色为“red”,载人数为2人的轿车。

public  class Vehicle {
private String brand;
	private String color;
	private double speed=0;
	public String getBrand() {
		return brand;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public double getSpeed() {
		return speed;
	}
	public void setSpeed(double speed) {
		this.speed = speed;
	}
	public Vehicle(String brand, String color) {
		this.brand = brand;
		this.color = color;
	}
	void run(double speed){
		
	}
}
public class Car extends Vehicle{
	private int loader;
	
	public int getLoader() {
		return loader;
	}
	public void setLoader(int loader) {
		this.loader = loader;
	}
	public Car(String brand,String color,int loader){
		super(brand,color);
		this.setLoader(loader);
	}
	public void run(double speed){
		this.setSpeed(speed); 
		System.out.println(this.getColor()+"色的"+this.getBrand()
		+"速度是"+speed+"载人数:"+loader);
	}
}
Car car = new Car("honda","红",2);
		car.run(100);

2.设计四个类,分别如下: [必做题]
3.1 设计Shape表示图形类,有面积属性area、周长属性per
,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋
值的),还有3个抽象方法,分别是:getArea计算面积、getPer计
算周长、showAll输出所有信息,还有一个求颜色的方法getColor。
3.2 设计 2个子类:
3.2.1 Rectangle表示矩形类,增加两个属性,Width表示长
度、height表示宽度,重写getPer、getArea和showAll三个
方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。
3.2.2 Circle表示圆类,增加1个属性,radius表示半径,重
写getPer、getArea和showAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。
3.3 测试类中,在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。

public abstract class Shape {
private int area;
	private int per;
	private String color;
	public String getColor() {
		return color;
	}
	public Shape() {
		
	}
	public Shape(String color) {
		this.color = color;
	}
	public abstract double getArea();
	public abstract double getPer();
	public abstract void showAll();			
}
public class Rectangle extends Shape{
	private int width;
	private int heigth;
	public Rectangle(){
		
	}
	public Rectangle(int width,int heigth,String color){
		super(color);
		this.width = width;
		this.heigth = heigth;
	}
	@Override
	public double getArea() {
		// TODO Auto-generated method stub
		
		return width*heigth;
	}
	@Override
	public double getPer() {
		// TODO Auto-generated method stub
		return 2*(width+heigth);
	}
	@Override
	public void showAll() {
		// TODO Auto-generated method stub
		System.out.println("面积:"+this.getArea()+"周长:"+this.getPer()
		+"颜色:"+this.getColor());
	}
}
public class Circle extends Shape{
	private int radius;
public Circle(){
		
	}
	public Circle(int radius,String color){
		super(color);
		this.radius = radius;
	}
	@Override
	public double getArea() {
		// TODO Auto-generated method stub
		
		return 3.14*radius*radius;
	}
	@Override
	public double getPer() {
		// TODO Auto-generated method stub
		return 2*radius*3.14;
	}
	@Override
	public void showAll() {
		// TODO Auto-generated method stub
		System.out.println("面积:"+this.getArea()+"周长:"+this.getPer()
		+"颜色:"+this.getColor());
	}
}
Rectangle r = new Rectangle(10,10,"红");
		Circle c = new Circle(10,"黑");
		r.showAll();
		c.showAll();

3.Cola公司的雇员分为以下若干类:(知识点:多态) [必做题]
4.1 ColaEmployee :这是所有员工总的父类,属性:员工的姓名,
员工的生日月份。方法:getSalary(int month) 根据参数月份来确定
工资,如果该月员工过生日,则公司会额外奖励100 元。
4.2 SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。属性:月薪
4.3 HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工
,每月工作超出160 小时的部分按照1.5 倍工资发放。属性:每小时的工资、每月工作的小时数
4.4 SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销
售额和提成率决定。属性:月销售额、提成率
4.5 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某
月某个员工的工资数额,写一个测试类TestCompany,在main方法,把若干各种
类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。

public  class ColaEmployee {
private String name;
	private int born;
	
	public ColaEmployee(String name, int born) {
		this.name = name;
		this.born = born;
	}

	public String getName() {
		return name;
	}

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

	public int getBorn() {
		return born;
	}

	public void setBorn(int born) {
		this.born = born;
	}

	public  int getSalary(int month){
		return 0;
	}
	public void show(){
	
	}
}
public class SalariedEmployee extends ColaEmployee {
	private int mpay;

	public SalariedEmployee(String name, int born, int mpay) {
		super(name, born);
		this.mpay = mpay;
	}
	public int getSalary(int month){
		if(month==this.getBorn()){
			mpay+=100;
		}
		return mpay;
	}
	public void show(){
		System.out.print("姓名:"+this.getName()+"生日:"+this.getBorn()
			+"月工资:");
	}
}
public class HourlyEmployee extends ColaEmployee {
	private int hpay;
	private int hour;
	public HourlyEmployee(String name, int born, int hpay, int hour) {
		super(name, born);
		this.hpay = hpay;
		this.hour = hour;
	}
	public int getSalary(int month){
		int money = 0;
		if(hour<=160&&hour>=0){
			money =hour*hpay;
		}else if(hour>160){
			money =  (int) ((int)160*hpay+(hour-160)*hpay*1.5);
		}
		if(month == this.getBorn()){
			money+=100;
		}
		return money;
	}
	public void show(){
		System.out.print("姓名:"+this.getName()+"生日:"+this.getBorn()
			+"每小时"+hpay+"元 "+"工时:"+hour+"工资:");
	
	}
}
public class SalesEmployee extends ColaEmployee{
	private int msale;
	private double rate;
	public SalesEmployee(String name, int born, int msale, double rate) {
		super(name, born);
		this.msale = msale;
		this.rate = rate;
	}
	public int getSalary(int month){
		int money = 0;
		money = (int) (msale*rate);
		if(month == this.getBorn()){
			money+=100;
		}
		return money;
	}
	public void show(){
		System.out.print("姓名:"+this.getName()+"生日:"+this.getBorn()+"月销售额:"+msale+
				"提成:"+rate+"工资:");
	}
}
public class Company {
	public void check(ColaEmployee c,int month){
		
		System.out.println(c.getSalary(month));
	}
}
public static void main(String[] args) {
Company com = new Company();
		ColaEmployee[] co = new ColaEmployee[3];
		co[0] = new SalariedEmployee("张三",5,2000);
		co[1] = new HourlyEmployee("李四",4,15,150);
		co[2] = new SalesEmployee("王五",11,15000,0.1);
		for(ColaEmployee c:co){
			c.show();
			com.check(c, 3);
			System.out.println();
		}
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43986054/article/details/84979009