首页 > 编程语言 >40.Java继承案例:各类水果的价格

40.Java继承案例:各类水果的价格

时间:2022-11-27 16:38:42浏览次数:60  
标签:Java name gprice double price 40 mark 案例 public

水果是​​超类​​,而对应的苹果、橙子等为子类,子类通常继承超类的特性,然后具有独有的属性对价格进行优惠。

1、Fruit类,超类
定义属性​​​private​​​私有---》构造方法初始化对象---》用get方法取得值
```language
package com.em.fruits;

public class Fruits {
    private String name;
    private double gprice;
    private double price;
    private String mark;

    public Fruits(String name, double gprice,double price, String mark){
        this.name = name;
        this.gprice = gprice;
        this.price = price;
        this.mark = mark;
    }

    public String getName() {
        return name;
    }

    public double getGprice(){
        return gprice;
    }

    public double getPrice() {
        return price;
    }

    public String getMark() {
        return mark;
    }
}

```

2、子类 Apple.java

定义属性private私有---》​​构造方法​​​初始化对象---》set方法赋值属性---》get方法获取超类的getPrice(),这里不能直接返回想要的值 ; 这会报错的 ---原因是 我文章36.继承私有域的理解
---》解决 借助接口 + super 

```language
package com.em.fruits;

class Apple extends Fruits {
    private double discount;

    public void setDiscount(double discount) {
        this.discount = discount;
    }

    public Apple(String name, double gprice, double price, String mark) {
        super(name, gprice, price, mark);
        discount = 0.00;
    }

    public double getPrice() {
        double price = super.getPrice();
        return price  - discount;
    }

}

```

2、Orange类 子类

定义属性private私有---》构造方法初始化对象---》set方法赋值属性---》get方法获取超类的getPrice(),这里不能直接返回想要的值 ; 这会报错的 ---原因是 我文章36.继承私有域的理解

```language
package com.em.fruits;

class Orange extends Fruits {

    private double discount2;

    public void setDiscount2(double discount2) {
        this.discount2 = discount2;
    }

    public Orange(String name, double gprice, double price, String mark) {
        super(name, gprice, price, mark);
        discount2 = 1.00;
    }

    public double getPrice(){
        double price = super.getPrice();
        return price * discount2;
    }
}

```

3、测试类
用子类创建对象(new)并赋值---》该对象调用子类里面赋好值的set方法---》用超类创建对象(new)并赋值
后面是循环遍历出来,用上面那个创建好的值+重新建new了3个对象---》for(Employee e : staff)(或用其他方法)把数组循环遍历出来---》sout+e.get方法

总结:
    public Orange(。。。。。。) {
        super(。。。。。。);
        discount2 = 1.00;
    }

    public double getPrice(){
        。。。。。。
        return price * discount2;
    }
这边要初始化discount2为1,不然它默认问0,初始价格可能会得到0.00,即gprice.
另外数组表示方式及遍历方法不止一种。get set方法看需要使用,get得到值,set属性赋值。

```language
package com.em.fruits;

public class fruitstest {
    public static void main(String[] args) {
        Apple a1 = new Apple("apple",5.00,5.00,"特价");
        a1.setDiscount(1.01);
        System.out.println("name="+a1.getName()+" gprice="+a1.getGprice()+" mark="+a1.getMark()+" price="+a1.getPrice());

        double discount2 = 8;
        Orange a2 = new Orange("orange",6.00,6.00,"促销"+discount2+"折");
        a2.setDiscount2(discount2 * 0.1);
        double price2 = a2.getPrice();
        price2 = (double)Math.round(price2 * 100) / 100;
        System.out.println("name="+a2.getName()+" gprice="+a2.getGprice()+" mark="+a2.getMark()+" price="+price2);

        System.out.println("--------------循环遍历----------------");
//        Fruits[] fruits = new Fruits[5];//1.常用
        Fruits fruits[] = new Fruits[5];
        fruits[0] = a1;
        fruits[1] = a2;
        fruits[2] = new Fruits("Peach",5.50,5.50,"正常价格");
        fruits[3] = new Fruits("Hami melon",10.50,10.50,"正常价格");
        fruits[4] = new Fruits("Banana",2.40,2.40,"正常价格");
        //第一种方法 需要修改
//        for(int i = 0; i < fruits.length; i++){
//            System.out.println("name=" + fruits[i].getName() +
//            " mark=" + fruits[i].getMark() + " price=" + fruits[i].getPrice());
//        }
        //第二种方法
        for (Fruits e : fruits){
            double pricefruits = (double)Math.round(e.getPrice() * 100) / 100;
            System.out.println("name=" + e.getName() + " gprice=" + e.getGprice() + " mark=" + e.getMark() + " price=" + pricefruits);
        }

    }
}

```

4、结果
```language
name=apple gprice=5.0 mark=特价 price=3.99
name=orange gprice=6.0 mark=促销8.0折 price=4.8
--------------循环遍历----------------
name=apple gprice=5.0 mark=特价 price=3.99
name=orange gprice=6.0 mark=促销8.0折 price=4.8
name=Peach gprice=5.5 mark=正常价格 price=5.5
name=Hami melon gprice=10.5 mark=正常价格 price=10.5
name=Banana gprice=2.4 mark=正常价格 price=2.4

```

标签:Java,name,gprice,double,price,40,mark,案例,public
From: https://blog.51cto.com/u_14604401/5890175

相关文章