面向对向 第4章

4.5
package four_4_5;

public abstract class Moto {
	private String no;//车牌
	private int total;//总金额
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	public abstract void calRent(int days);
}

package four_4_5;

import java.util.Scanner;

public final class Car extends Moto {
	int money;
	public void print() {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入要租赁的去汽车品牌(1、宝马 2、别克):");
		int choice1 = input.nextInt();//选择汽车品牌
		if(choice1 == 1) {
			money = 500;
			super.setNo("湘D.88888");//宝马的租赁单价为500,车牌为88888
		} else if(choice1 == 2) {
			System.out.print("请输入轿车的型号 2、商务舱GL8 3、林荫大道");
			int choice2 = input.nextInt();//选择轿车型号
			if(choice2 == 2) {
				money = 600;
				super.setNo("湘D.12345");//商务舱的租赁单价为600,车牌为12345
			}else if(choice2 == 3) {
				money = 300;
				super.setNo("湘D.23456");//林荫大道的租赁单价为300,车牌为23456
			}
		}
	}
	public void calRent(int days) {
		super.setTotal(money*days);//输入总金额
	}

}

package four_4_5;

import java.util.Scanner;

public final class Bus extends Moto {
	int money;
	public void print() {
		Scanner input = new Scanner(System.in);
		System.out.print("请输入需要租赁的客车品牌:(1、金杯 2、金龙)");
		int choice1 = input.nextInt();//选择客车品牌
		System.out.print("请输入客车的座位数:");
		 int seatCount = input.nextInt();//输入客车座位数
		 if(seatCount > 16) {
			 money = 1500;
			 super.setNo("湘D.54321");//大于16座 单价为1500 车牌为54321
		 }else {
			 money = 800;
			 super.setNo("湘D.65432");//小于等于16座 单价为800 车牌为65432
		 }
	}
	public void calRent(int days) {
		super.setTotal(money*days);
	}

}

package four_4_5;

import java.util.Scanner;

public class Show {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("欢迎您来到汽车租赁公司!");
		System.out.print("请输入要租凭的天数:");
		int days = input.nextInt();
		System.out.print("请输入要租赁的汽车类型(1:轿车 2、客车):");
		int choice = input.nextInt();
		if(choice == 1) {//如果输入1,则调用轿车方法
			Car car = new Car();
			car.print();
			System.out.println("分配给您的汽车牌号是:"+car.getNo());
			System.out.println();
			car.calRent(days);
			System.out.println("顾客您好!您需要支付的租赁费用是"+car.getTotal()+"。");
		} else if(choice == 2) {//如果输入2,则调用客车方法
			Bus bus = new Bus();
			bus.print();
			System.out.println("分配给您的汽车牌号是:"+bus.getNo());
			System.out.println();
			bus.calRent(days);
			System.out.println("顾客您好!您需要支付的租赁费用是"+bus.getTotal()+"。");
		}
	}
}

课后练习5:
package four_c5;

public abstract class Animal {
	private String name;//名字或种类
	private int age;//年龄
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public abstract void info();
}

package four_c5;

public class Bird extends Animal{
	String color = "红色";
	public Bird() {
		super.setName("鸟");
		super.setAge(4);//鸟的姓名和年龄
	}
	public void info() {//重写抽象方法
		System.out.println("我是一只"+color+"的"+getName()+"!");
		System.out.println("今年"+getAge()+"岁了!");
	}
}

package four_c5;

public class Fish extends Animal{
	String weight = "5斤重";
	public Fish() {
		super.setName("鱼");
		super.setAge(2);//鱼的姓名和年龄
	}
	public void info() {//重写抽象方法
		System.out.println("我是一只"+weight+"的"+getName()+"!");
		System.out.println("今年"+getAge()+"岁了!");
	}
}

package four_c5;

public class Show {
	public static void main(String[] args) {
		Animal show1 = new Bird();
		Animal show2 = new Fish();
		show1.info();
		show2.info();
	}
}

课后练习6:
package four_c6;

public class Pet {
	private String name;
	private String kind;
	public Pet() {
		
	}
	public Pet(String name,String kind) {
		this.name = name;
		this.kind = kind;
	}
	public void print() {
		System.out.println("我叫"+name+",是一只"+kind+"!");
	}
}

package four_c6;

public class Chicken1 extends Pet{
	private String eat1;
	private String do1;
	public Chicken1(String name,String kind,String eat1,String do1) {
		super(name,kind);
		this.eat1 = eat1;
		this.do1 = do1;//输入芦花鸡的信息
	}
	public void print() {
		super.print();
		System.out.println("我喜欢"+eat1+"!");
		System.out.println("我会"+do1+"!");
	}
}

package four_c6;

public class Chicken2 extends Pet{
	private String eat2;
	private String do2;
	public Chicken2(String name,String kind,String eat2,String do2) {
		super(name,kind);
		this.eat2 = eat2;
		this.do2 = do2;//输入斑嘴鸭的信息
	}
	public void print() {
		super.print();
		System.out.println("我喜欢"+eat2+"!");
		System.out.println("我会"+do2+"!");
	}
}

package four_c6;

import java.util.Scanner;

public class Show {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		Pet show1 = new Pet();
		Pet show2 = new Pet("汪汪","蠢狗");
		show1.print();
		show2.print();
		Chicken1 show3 = new Chicken1("喔喔","芦花鸡","吃虫子","打鸣");
		Chicken2 show4 = new Chicken2("嘎嘎","斑嘴鸭","小鱼虾","游泳");
		show3.print();
		show4.print();
		input.close();
	}
}

猜你喜欢

转载自blog.csdn.net/lsxdbd/article/details/80092069