寒假复习作业

public abstract class Animal {

    private int age;

    public Animal(){

    }

    public Animal(int age){
        this.age = age;
    }

    public int getAge(){
        return age;
    }

    public void setAge(int age){
        this.age =age;
    }

    public abstract void introduce();
}

class Fish extends Animal{

    private int weight;

    public Fish(){

    }

    public Fish(int age,int weight){
        super(age);
        this.weight = weight;
    }

    public int getWeight(){
        return weight;
    }

    public void setWeight(int weight){
        this.weight = weight;
    }

    @Override
    public void introduce() {
        System.out.println("鱼的年龄是:" + super.getAge() + ",重量是:" + weight + "kg");
    }

    public void swim(){
        System.out.println("我是一条鱼,我正在游泳呢... ...");
    }
}

class Bird extends Animal{

    private String color;

    public Bird(){

    }

    public Bird(int age,String color){
        super(age);
        this.color = color;
    }

    public String getColor(){
        return color;
    }

    public void setColor(String color){
        this.color = color;
    }

    @Override
    public void introduce() {
        System.out.println("鸟的年龄是:" + super.getAge() + ",颜色是:" + color);
    }

    public void fly(){
        System.out.println("我是一只鸟,我正在飞呢... ...");
    }
}

class AnimalText {

    public static void main(String[] args) {
        Fish fish = new Fish(17,2);
        fish.introduce();
        fish.swim();
        Bird bird = new Bird(16,"彩色");
        bird.introduce();
        bird.fly();
    }
}
public abstract class MotoVehical {

    private String no;
    private String brand;

    public MotoVehical(){

    }

    public MotoVehical(String no, String brand) {
        this.no = no;
        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 calcRent(int days);

}

class Car extends MotoVehical {

    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 calcRent(int days) {
        if (this.type.equals("0")) {
            return 600 * days;
        } else if (this.type.equals("1")) {
            return 500 * days;
        } else if (this.type.equals("2")) {
            return 300 * days;
        } else {
            System.out.println("您输入的车型不存在!");
            return 0;
        }
    }
}

class Bus extends MotoVehical {

    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;
    }

    @Override
    public int calcRent(int days) {
        if(this.seatCount > 16){
            return 1500 * days;
        }else{
            return 800 * days;
        }
    }
}

class Truck extends MotoVehical {

    private int weight;

    public Truck(String no,String brand,int weight){
        super(no,brand);
        this.weight = weight;
    }

    @Override
    public int calcRent(int days) {
        return 50 * days * this.weight;
    }
}

class MotoVehicalTest {

    public static void main(String[] args) {
        MotoVehical[] motoArray = new MotoVehical[5];
        motoArray[0] = new Car("豫A 00099999", "宝马550i", "1");
        motoArray[1] = new Car("豫B 00088888", "宝马550i", "1");
        motoArray[2] = new Car("豫C 00066666", "别克林荫大道", "2");
        motoArray[3] = new Bus("豫D 00011111", "金龙", 34);
        motoArray[4] = new Truck("豫E 000333333", "东方红", 50);

        int totalRent = MotoVehicalTest.calcRent(motoArray, 5);
        System.out.println("租5天的总租金是:" + totalRent);
        }

        public static int calcRent(MotoVehical[] motoArray,int days){
            int totalRent = 0;
            for(int i = 0; i<motoArray.length;i++){
                totalRent += motoArray[i].calcRent(days);
            }
            return totalRent;
        }
}
public class Pet {
    private String name;
    protected int health=100;
    protected int love = 0;

    public Pet() {
        super();
    }

    public Pet(String name, int health, int love) {
        super();
        this.name = name;
        this.health = health;
        this.love = love;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getHealth() {
        return health;
    }
    public void setHealth(int health) {
        this.health = health;
    }
    public int getLove() {
        return love;
    }
    public void setLove(int love) {
        this.love = love;
    }
}

class Dog extends Pet {

    private String strain;

    public Dog() {
        super();
    }

    public Dog(String strain) {
        super();
        this.strain = strain;
    }

    public void catchingFlyDisc(){
        System.out.println("小狗正在接飞盘!");
        super.health =super.health-10;
        System.out.println("健康值:" + super.health);
        super.love = super.love +5;
        System.out.println("亲密度:" + super.love);
    }

    public String getStrain() {
        return strain;
    }

    public void setStrain(String strain) {
        this.strain = strain;
    }
}

class Penguin extends Pet {

    private String sex;

    public Penguin() {
        super();
    }

    public Penguin(String sex) {
        super();
        this.sex = sex;
    }

    public void swimming(){
        System.out.println("企鹅正在接飞盘!");
        super.health =super.health-10;
        System.out.println("健康值:" + super.health);
        super.love = super.love +5;
        System.out.println("亲密度:" + super.love);
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

class Master {

    private String name;
    private int age;

    public Master() {
        super();
    }
    public Master(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public void play(Pet pet){
        if(pet instanceof Dog){
            Dog dog = (Dog)pet;
            dog.catchingFlyDisc();
        }else if(pet instanceof Penguin){
            Penguin p = (Penguin)pet;
            p.swimming();
        }
    }

    public static Pet getPet(String typeId){
        if (typeId.equals("1")){
            return new Dog();
        }else if (typeId.equals("2")){
            return new Penguin();
        }else {
            return null;
        }
    }

    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;
    }
}

class Test {

    public static void main(String[] args) {
        Master m = new Master();
        Pet pet = Master.getPet("1");
        if (pet instanceof Dog){
            System.out.println("领养的是狗狗");
        }else if (pet instanceof Penguin){
            System.out.println("领养的是企鹅");
        }
        m.play(new Dog());
        m.play(new Penguin());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值