java22 作业 2018-12-12

#作业
##1/定义一个汽车类Vehicle,要求如下:[选做题]
2.1属性包括:汽车品牌brand(String类型)、颜色color(String类型)
和速度speed(double类型),并且所有属性为私有。
2.2至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,
但速度的初始值必须为0)。
2.3为私有属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。
2.4定义一个一般方法run(),用打印语句描述汽车奔跑的功能
2.5定义测试类VehicleTest,在其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 abstract class Vehicle {
	private String brand;
	private String color;
	private double speed;

	public Vehicle(String brand, String color) {
		super();
		this.brand = brand;
		this.color = color;
	}

	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 String getBrand() {
		return brand;
	}

	public abstract void run();

}

//子类

public class Car extends Vehicle {
	private int loader;

	public Car(String brand, String color, int loader) {
		super(brand, color);
		this.loader = loader;
	}

	public void run() {
		System.out.println(getColor() + "色" + getBrand() + "可载" + this.loader + "时速为" + getSpeed() + "米/小时");
	}

}

//测试类

public class VehicleTest {

	public static void main(String[] args) {
		Vehicle car = new Car("Honda", "black", 2);

		car.setColor("red");
		car.setSpeed(300);
		car.run();
	}

}

##3./设计四个类,分别如下: [必做题]
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 Shape() {
		super();
	}

	public Shape(String color) {
		super();
		this.color = color;
	}

	public abstract void getArea();// 计算面积

	public abstract void getPer();// 计算周长

	public abstract void showAll();// 输出所有信息

	public void getColor() {
		System.out.println("颜色为:"+this.color);
	}
}

//子类

public class Rectangle extends Shape {
	private int Width;
	private int height;
	
	public Rectangle() {
		
	}

	public Rectangle(int width, int height,String color) {
		super(color);
		this.Width = width;
		this.height = height;
	}

	@Override
	public void getArea() {
		System.out.println("矩形的面积为:"+this.Width*this.height);
		
	}

	@Override
	public void getPer() {
		System.out.println("矩形的周长为:"+2*(this.Width+this.height));
		
	}

	@Override
	public void showAll() {
		System.out.println("长为:"+this.height+"宽为:"+this.Width);
		this.getArea();
		this.getPer();
		getColor();
	}
	

}
public class Circle extends Shape {
	private double radius;

	public Circle(double radius, String color) {
		super(color);
		this.radius = radius;
	}

	@Override
	public void getArea() {
		System.out.println("圆的面积为:" + this.radius * this.radius * 3.14);

	}

	@Override
	public void getPer() {
		System.out.println("圆的周长为:" + 2 * this.radius * 3.14);

	}

	@Override
	public void showAll() {
		System.out.println("半径为:" + this.radius);
		this.getArea();
		this.getPer();
		getColor();

	}

}

//测试类

public class Delineation {

	public static void main(String[] args) {

		Shape f = new Rectangle(2, 4, "黄色");
		Shape y = new Circle(2, "蓝色");

		f.showAll();
		y.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 数组里,并单元出数组中每个员工当月的工资

//父类

猜你喜欢

转载自blog.csdn.net/weixin_43986060/article/details/84976869