JavaDay07 构造器练习

1. 根据要求编写类,编写完成后使用下面的测试类去测试,要求如下:

(1)Duration1.int类型属性hours、minutes、seconds
2.构造器(三参),对hours、minutes、seconds进行初始化
3.构造器(一参),对hours、minutes、seconds进行初始化,参数为总的seconds,例如:将x秒,转为a小时b分钟c秒,3661,就是1小时1分钟14.对每个属性提供get方法
5.getTotalSeconds()方法,用于返回总的seconds
6.toString()方法,代码为 return hours + ":" + minutes + ":" + seconds;
//测试类
public class Driver {
	public static void main(String[] args) {
		Track myTrack = new Track();
		myTrack.setTitle("Watching The Wheels");
		//Duration myDuration = new Duration(234);
		Duration myDuration = new Duration(0, 3, 48);
		myTrack.setDuration(myDuration);
		System.out.println("myTrack is: " + myTrack);
		System.out.println("Title = " + myTrack.getAddress ());
		System.out.println("Duration = " + myTrack.getDuration().toString());
	}
}
(2)Track1.String类型属性title、Duration类型属性duration
2.对每个属性提供get和set方法
3.toString()方法,代码为 return "my Track address is: " + title + " duration is: " + duration;

(1)Duration类的编写:

public class Duration {
	private int hours;
	private int minutes;
	private int seconds;
	public Duration() {}
	public Duration(int hours,int minutes,int seconds) {
		this.hours=hours;
		this.minutes=minutes;
		this.seconds=seconds;
	}
	public Duration(int totalSeconds) {
		this.hours=totalSeconds/3600;
		this.minutes=(totalSeconds-hours*3600)/60;
		this.seconds=totalSeconds-hours*3600-minutes*60;
	}
	public int getHours() {
		return hours;
	}
	public int getMinutes() {
		return minutes;
	}
	public int getSeconds() {
		return seconds;
	}
	public int getTotalSeconds() {
		return hours*3600+minutes*60+seconds;
	}
	@Override
	public String toString() {
		return hours + ":" + minutes + ":" + seconds;
	}
}

(2)Track类的编写:

public class Track {
	private String title;
	private Duration duration;
	public void setTitle(String title) {
		this.title=title;
	}
	public String getTitle() {
		return title;
	}
	public void setDuration(Duration duration) {
		this.duration=duration;
	}
	public Duration getDuration() {
		return duration;
	}
	@Override
	public String toString() {
		return "my Track address is: " + title + " duration is: " + duration;
	}
}

2. 某汽车租赁公司有多种汽车可以出租;使用面向对象的方式,分析此场景,写出对应的代码结构。

1.Vehicle是所有车的父类,有属性:品牌、车牌号,有返回总租金的方法getsumrent(int days)
2.小轿车类CarVehicle的子类,属性:车型(两厢、三厢、越野),两厢每天300,三厢每天350,越野每天5003.多座汽车类=Vehicle的子类,属性:座位数,座位数<=16的每天400,座位数>16的每天600

编写代码:

public class Vehicle {
	//品牌
	private String kind;
	//车牌
	private String carId;
	public int getsumrent(int days) {
		return 0;
	}
	public String getKind() {
		return kind;
	}
	public void setKind(String kind) {
		this.kind = kind;
	}
	public String getCarId() {
		return carId;
	}
	public void setCarId(String carId) {
		this.carId = carId;
	}
	@Override
	public String toString() {
		return "Vehicle [kind=" + kind + ", carId=" + carId + "]";
	}
	public Vehicle(String kind, String carId) {
		super();
		this.kind = kind;
		this.carId = carId;
	}
	public Vehicle() {
	super();
	}
}
class Car extends Vehicle{
	private String type;
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	@Override
	public String toString() {
		return "Car [type=" + type + "]";
	}
	public Car(String kind, String carId, String type) {
		super(kind, carId);
		this.type = type;
	}
	public Car() {}
	@Override
	public int getsumrent(int days) {
	if(type.equals("越野车")) {
		return days*500;
	}else if(type.equals("两厢车")) {
		return days*300;
	}else if(type.equals("三厢车")) {
		return days*350;
	}else {
		return 0;
	}
}
class Bus extends Vehicle{
	private int pNum;
	public int getpNum() {
		return pNum;
	}
	public void setpNum(int pNum) {
		this.pNum = pNum;
	}
	@Override
	public String toString() {
		return "bus [pNum=" + pNum + "]";
	}
	public Bus(String kind, String carId, int pNum) {
		super(kind, carId);
		this.pNum = pNum;
	}
	public Bus(String kind, String carId) {
		super(kind, carId);
	}
	@Override
	public int getsumrent(int days) {
		if(pNum>16) {
			return days*600;
		}else{
			return days*400;
		}
	}
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值