多态的写法

在这里插入图片描述
在这里插入图片描述

package murauchi;


public class Test2 {
    public static void main(String[] args) {
        Person person1 = new Person("Kate", "Jones", 27, 1.6, 50.0);
        Person person2 = new Person("John", "Christopher", "Smith", 65, 1.75, 80.0);

        Car car = new Car("フェラーリ", "赤");
        // setOwnerを用いて、carの所有者をperson1にしてください
        car.setOwner(person1);

        Bicycle bicycle = new Bicycle("ビアンキ", "緑");
        // setOwnerを用いて、bicycleの所有者をperson2にしてください
        bicycle.setOwner(person2);


        System.out.println("【車の情報】");
        car.printData();
        System.out.println("-----------------");
        System.out.println("【車の所有者の情報】");
        // getOwnerメソッドを用いてcarのownerを取得し、
        // さらにprintDataメソッドを用いてownerの情報を出力してください
        car.getOwner().printData();

        System.out.println("=================");
        System.out.println("【自転車の情報】");
        bicycle.printData();
        System.out.println("-----------------");
        System.out.println("【自転車の所有者の情報】");
        // getOwnerメソッドを用いてbicycleのownerを取得し、
        // さらにprintDataメソッドを用いてownerの情報を出力してください
        bicycle.getOwner().printData();
    }
}

abstract class Vehicle {
    private String name;
    private String color;
    protected int distance = 0;//让子类和父类可以取到
    private Person owner;

    public Person getOwner() {
        return owner;
    }

    public void setOwner(Person owner) {
        this.owner = owner;
    }

    Vehicle(String name, String color) {
        this.name = name;
        this.color = color;
    }

    public String getName() {
        return this.name;
    }

    public String getColor() {
        return this.color;
    }

    public int getDistance() {
        return this.distance;
    }

    public void setName(String name) {
        this.name = name;
    }

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

    public void printData() {
        System.out.println("名前:" + this.name);
        System.out.println("色:" + this.color);
        System.out.println("走行距離:" + this.distance + "km");
    }
    public abstract void run(int distance);
}

class Car extends Vehicle {
    private int fuel = 50;
    Car(String name, String color) {
        super(name, color);
    }

    public int getFuel() {
        return this.fuel;
    }

    public void printData() {
        super.printData();
        System.out.println("ガソリン量:" + this.fuel + "L");
    }

    public void run(int distance) {
        System.out.println(distance + "km走ります");
        int dis = this.getDistance();
        if (distance <= this.fuel) {
            this.distance += distance;
        } else {
            System.out.println("ガソリンが足りません");
        }
        //System.out.println("走行距離:" + dis + "km");
        System.out.println("走行距離:" + this.distance + "km");
        System.out.println("ガソリン量:" + this.fuel + "L");
    }

    public void charge(int litre) {
        System.out.println(litre + "L給油します");
        if (litre <= 0) {
            System.out.println("給油できません");
        } else if (litre + this.fuel >= 100) {
            System.out.println("満タンまで給油します");
            this.fuel = 100;
        } else {
            this.fuel += litre;
        }
        System.out.println("ガソリン量:" + this.fuel + "L");
    }
}

class Bicycle extends Vehicle {
    Bicycle(String name, String color) {
        super(name, color);
    }

    public void run(int distance) {
        System.out.println(distance + "km走ります");
        this.distance += distance;
        System.out.println("走行距離:" + this.distance + "km");
    }
}

class Person {
    private String firstName;
    private String middleName;
    private String lastName;
    private int age;
    private double height;
    private double weight;

    Person(String firstName, String lastName, int age, double height, double weight) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.height = height;
        this.weight = weight;
    }

    Person(String firstName, String middleName, String lastName, int age, double height, double weight) {
        this(firstName, lastName, age, height, weight);
        this.middleName = middleName;
    }

    public String fullName() {
        if (this.middleName == null) {
            return this.firstName + " " + this.lastName;
        } else {
            return this.firstName + " " + this.middleName + " " + this.lastName;
        }
    }

    public void printData() {
        System.out.println("名前は" + this.fullName() + "です");
        System.out.println("年齢は" + this.age + "歳です");
        System.out.println("身長は" + this.height + "mです");
        System.out.println("体重は" + this.weight + "kgです");
        System.out.println("BMIは" + Math.round(this.bmi()) + "です");
    }

    public double bmi() {
        return this.weight / this.height / this.height;
    }

    public void buy(Car car){
        car.setOwner(this);
    }

    // Bicycle型の引数を受け取るbuyメソッドを定義してください
    public void buy(Bicycle bicycle){
        bicycle.setOwner(this);
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

package murauchi;


public class Test2 {
    public static void main(String[] args) {
        Person person1 = new Person("Kate", "Jones", 27, 1.6, 50.0);
        Person person2 = new Person("John", "Christopher", "Smith", 65, 1.75, 80.0);

        Car car = new Car("フェラーリ", "赤");
        Bicycle bicycle = new Bicycle("ビアンキ", "緑");

        person1.buy(car);

        person2.buy(bicycle);

        System.out.println("【車の情報】");
        car.printData();
        System.out.println("-----------------");
        System.out.println("【車の所有者の情報】");
        car.getOwner().printData();

        System.out.println("=================");
        System.out.println("【自転車の情報】");
        bicycle.printData();
        System.out.println("-----------------");
        System.out.println("【自転車の所有者の情報】");
        bicycle.getOwner().printData();
    }
}

abstract class Vehicle {
    private String name;
    private String color;
    protected int distance = 0;//让子类和父类可以取到
    private Person owner;

    public Person getOwner() {
        return owner;
    }

    public void setOwner(Person owner) {
        this.owner = owner;
    }

    Vehicle(String name, String color) {
        this.name = name;
        this.color = color;
    }

    public String getName() {
        return this.name;
    }

    public String getColor() {
        return this.color;
    }

    public int getDistance() {
        return this.distance;
    }

    public void setName(String name) {
        this.name = name;
    }

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

    public void printData() {
        System.out.println("名前:" + this.name);
        System.out.println("色:" + this.color);
        System.out.println("走行距離:" + this.distance + "km");
    }
    public abstract void run(int distance);
}

class Car extends Vehicle {
    private int fuel = 50;
    Car(String name, String color) {
        super(name, color);
    }

    public int getFuel() {
        return this.fuel;
    }

    public void printData() {
        super.printData();
        System.out.println("ガソリン量:" + this.fuel + "L");
    }

    public void run(int distance) {
        System.out.println(distance + "km走ります");
        int dis = this.getDistance();
        if (distance <= this.fuel) {
            this.distance += distance;
        } else {
            System.out.println("ガソリンが足りません");
        }
        //System.out.println("走行距離:" + dis + "km");
        System.out.println("走行距離:" + this.distance + "km");
        System.out.println("ガソリン量:" + this.fuel + "L");
    }

    public void charge(int litre) {
        System.out.println(litre + "L給油します");
        if (litre <= 0) {
            System.out.println("給油できません");
        } else if (litre + this.fuel >= 100) {
            System.out.println("満タンまで給油します");
            this.fuel = 100;
        } else {
            this.fuel += litre;
        }
        System.out.println("ガソリン量:" + this.fuel + "L");
    }
}

class Bicycle extends Vehicle {
    Bicycle(String name, String color) {
        super(name, color);
    }

    public void run(int distance) {
        System.out.println(distance + "km走ります");
        this.distance += distance;
        System.out.println("走行距離:" + this.distance + "km");
    }
}

class Person {
    private String firstName;
    private String middleName;
    private String lastName;
    private int age;
    private double height;
    private double weight;

    Person(String firstName, String lastName, int age, double height, double weight) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.height = height;
        this.weight = weight;
    }

    Person(String firstName, String middleName, String lastName, int age, double height, double weight) {
        this(firstName, lastName, age, height, weight);
        this.middleName = middleName;
    }

    public String fullName() {
        if (this.middleName == null) {
            return this.firstName + " " + this.lastName;
        } else {
            return this.firstName + " " + this.middleName + " " + this.lastName;
        }
    }

    public void printData() {
        System.out.println("名前は" + this.fullName() + "です");
        System.out.println("年齢は" + this.age + "歳です");
        System.out.println("身長は" + this.height + "mです");
        System.out.println("体重は" + this.weight + "kgです");
        System.out.println("BMIは" + Math.round(this.bmi()) + "です");
    }

    public double bmi() {
        return this.weight / this.height / this.height;
    }

    // 以下2つを一つのメソッドで書き換えてください
    public void buy(Vehicle vehicle) {
        vehicle.setOwner(this);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值