0
点赞
收藏
分享

微信扫一扫

水果类计算价格

小桥流水2016 2022-03-21 阅读 63
java

父类

package com.ytzl.zhouce.fruit;

/**
 * Create with IntelliJ IDEA.
 * Description:
 * User: 稚萱
 * Author 86185
 * Date: 2022-03-19
 * Time: 12:21
 */
public abstract class fruit {
    private String color;//颜色
    private double price;//价格

    public fruit() {
    }

    public fruit(String color, double price) {
        this.color = color;
        this.price = price;
    }

    public String getColor() {
        return color;
    }

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

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
    public abstract int calcu();

    @Override
    public String toString() {
        return "fruit{" +
                "颜色为='" + color + '\'' +
                ", 价格为=" + price +
                '}';
    }
}

苹果子类

vpackage com.ytzl.zhouce.fruit;

/**
 * Create with IntelliJ IDEA.
 * Description:
 * User: 稚萱
 * Author 86185
 * Date: 2022-03-19
 * Time: 12:25
 */
public class apple extends fruit{
    private int count;//个数

    public apple() {
    }

    public apple(int count) {
        this.count = count;
    }

    public apple(String color, double price, int count) {
        super(color, price);
        this.count = count;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public int calcu() {
        return (int) (getCount()*super.getPrice());
    }

    @Override
    public String toString() {
        System.out.println(super.toString());
        return "apple{" +
                "个数为=" + count +
                '}';
    }
}

西瓜子类

package com.ytzl.zhouce.fruit;

/**
 * Create with IntelliJ IDEA.
 * Description:
 * User: 稚萱
 * Author 86185
 * Date: 2022-03-19
 * Time: 12:25
 */
public class watermelon extends fruit{
    private int weight;//个数

    public watermelon() {
    }

    public watermelon(int weight) {
        this.weight = weight;
    }

    public watermelon(String color, double price, int weight) {
        super(color, price);
        this.weight = weight;
    }

    public int getWeight() {
        return weight;
    }

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

    @Override
    public int calcu() {
        return (int) (getWeight()*super.getPrice());
    }

    @Override
    public String toString() {
        System.out.println(super.toString());
        return "apple{" +
                "个数为=" + weight +
                '}';
    }
}

测试类

package com.ytzl.zhouce.fruit;

/**
 * Create with IntelliJ IDEA.
 * Description:
 * User: 稚萱
 * Author 86185
 * Date: 2022-03-19
 * Time: 12:28
 */
public class test {
    public static void main(String[] args) {
        apple apple = new apple("红色",15,8);
        apple apple1 = new apple("白色",23,2);
        watermelon watermelon = new watermelon("绿色",23,10);
        manager manager = new manager();
        apple[] apples=new apple[2];
        apples[0]=apple;
        apples[1]=apple1;
        watermelon[] watermelons=new watermelon[1];
        watermelons[0]=watermelon;
        System.out.println(apple.toString());
        System.out.println("价格为"+manager.calcuAll(apples));
        System.out.println(watermelon.toString());
        System.out.println("价格为"+manager.calcuAll(watermelons));
        fruit[] fruits = new fruit[3];
        fruits[0]=apple;
        fruits[1]=apple1;
        fruits[2]=watermelon;
        System.out.println("总价格为"+manager.calcuAll(fruits));
    }
}
举报

相关推荐

0 条评论