面向对象深入
创建父类:
package day7;
/*
*
* 父类
*/
public abstract class Pet {
//公共属性
String name="无名氏";
int health=100;
int love=0;
//公共方法
//公共构造
public String color;
protected int age;
public void show() {
System.out.println("自白:名字"+this.name+",健康值:"+this.health+"亲密度"+this.love);
}
}
子类
package day7;
public class Dog extends Pet {
private static final String name = null;
//属性
// String name="无名氏";
// int health=100;
// int love=0;
private String strain;
//构造
//方法
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
//pet show方法重写
public void show() {
super.show();
System.out.println("这是一只"+this.strain+"狗");
}
}
package day7;
public class Punguin extends Pet {
//属性
// String name=“无名氏”;
// int health=100;
// int love=0;
private String sex;
//构造
//方法
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
//pet show方法重写
public void show() {
super.show();
System.out.println("这是一只"+this.sex+"企鹅");
}
}
测试类
package day7;
public class DemoTest {
public static void main(String[] args) {
//Pet 创建对象
// Pet pet = new Pet() ;
// pet.show();
//创建狗类对象
Dog dog = new Dog();
dog.setStrain("金毛");
dog.show();
//创建企鹅类对象
Punguin p = new Punguin();
p.setSex("Q仔");
p.show();
}
}
汽车租赁练习:
父类:抽象类
package day7homework;
/*
- 抽象类
*/
public abstract class MotoVehicle {
private String no ;//汽车牌号
private String brand;//汽车品牌
//无参
public MotoVehicle() {
}
//有参数
public MotoVehicle (String no,String brand) {
this.brand=brand;
//有参构造方法:
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
//抽象方法,计算汽车租赁价格
public abstract int calRent(int days);
}
package day7homework;
/**
-
客户类。
*/public class Customer {
String id;//客户编号
String name;//客户姓名
/**
* 有参构造方法。
* @param id 客户编号
* @param name 客户姓名
/
public Customer(String id, String name) {
this.id=id;
this.name=name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
/*
* 计算多辆汽车总租赁价格
/
public int calcTotalRent(MotoVehicle motos[],int days){
int sum=0;
for(int i=0;i<motos.length;i++)
sum+=motos[i].calRent(days);
return sum;
}
}
package day7homework;
/
*-
轿车类,继承汽车类
*/
public final class Car extends MotoVehicle{
private String type;
//无参
public Car() {}
//有参
public Car (String no,String brand,String type) {
super(no, brand);
this.type=type;
}public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
//方法:计算汽车租赁价
@Override
public int calRent(int days) {
if (“1”.equals(type)) {//代表550i
return days500;
}
else if (“2”.equals(type)) {//代表商务GL8
return days600;
}
else {
return days*300;
}
}
}
package day7homework;public final class Bus extends MotoVehicle {
//属性
private int seatcount;//座位数
//无参
public Bus() {} //有参 public Bus (String no ,String brand,int seatcount) { super(no, brand); this.seatcount=seatcount; } public int getSeatcount() { return seatcount; } public void setSeatcount(int seatcount) { this.seatcount = seatcount; } //计算客车租赁价格 public int calRent(int days) { if (seatcount<=16) { return days *800; } else { return days * 1500; } }
}
package day7homework;
//测试类import java.util.Scanner;
public class CarrentTest {
public static void main(String[] args) { String no,brand,mytype,type; int seatcount,days,rent;
// MotoVehicle[] motos=new MotoVehicle[4];
// motos[0]=new Car(“宝马550i”,“苏AY8588”);
// motos[1]=new Car(“宝马550i”,“苏ANN328”);
// motos[2]=new Car(“别克林荫大道”,“苏AY8588”);
// motos[3]=new Bus(“金龙”,34);
Car car;
Bus bus;
Scanner input = new Scanner(System.in);System.out.println("欢迎您来到汽车租赁公司!"); System.out.println("请输入要租赁的天数:"); days = input .nextInt(); System.out.println("请输入要租赁的汽车类型(1.轿车 2.客车):"); mytype=input.next(); if ("1".equals(mytype)) { System.out.println("请输入要租赁的汽车品牌(1.宝马 2.别克):"); brand=input.next(); System.out.println("请输入轿车的型号:"); if ("1".equals(brand)) System.out.println("1.550i"); else System.out.println("2.商务GL8 3.林荫大道"); type=input.next(); no="苏A30KF9"; System.out.println("分配给您的汽车牌号是:"+no); car=new Car(no, brand, type); rent=car.calRent(days); } else { System.out.println("请输入要租赁的汽车品牌(1.金龙 2.金杯):"); brand=input.next(); System.out.println("请输入客车的座位数:"); seatcount = input.nextInt(); no="苏A30KF9"; System.out.println("分配给您的汽车牌号是:"+no); bus=new Bus(no, brand, seatcount); rent=bus.calRent(days); } System.out.println("\n顾客您好!您需要的租赁费用是:"+rent); }
}
-