抽象类中的私有实例变量

时间:2016-05-31 00:14:45

标签: java oop

我有一个名为 Tea 的抽象类。该抽象类包含名为 size 的实例变量和具体方法 getPrice 。现在,当我扩展这个抽象类时,size的值没有保存到变量,这是导致问题的原因。

我错过了什么规则,为什么不能将大小的值保存到实例变量?

这是我的代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<div class="player1"></div>

Drink.class

package milktea;

import milktea.Drink.TeaDrink;
import milktea.Tea.Size;

public class MilkTea {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Tea tea1 = Drink.buyTea(TeaDrink.MATCHA, Size.LARGE);
        System.out.println(tea1.getName() + "..." + tea1.getPrice());
        Sinker sinker1 = tea1.getFreeSinker();
        System.out.println(sinker1.getName() + "..." + sinker1.getPrice());

        Tea tea2 = Drink.buyTea(TeaDrink.WINTERMELON, Size.SMALL);
        System.out.println(tea2.getName() + "..." + tea2.getPrice());

    }

}

Sinker.class

package milktea;

import milktea.Tea.Size;
import milktea.sinker.*;
import milktea.teaType.*;

public class Drink {

    public enum TeaDrink {
        BLACKTEA,
        CHRYSANTEMUM,
        MATCHA,
        OOLONG,
        WINTERMELON
    }

    public enum Sinkers {
        CRYSTAL,
        PEARL
    }

    public static Tea buyTea(TeaDrink tea, Size size){

        if(tea==TeaDrink.BLACKTEA){
            return new BlackTea(size);
        }else if(tea==TeaDrink.CHRYSANTEMUM){
            return new Chrysantemum(size);
        }else if(tea==TeaDrink.MATCHA){
            return new Matcha(size);
        }else if(tea==TeaDrink.OOLONG){
            return new Oolong(size);
        }else{
            return new Wintermelon(size);
        }
    }

    public static Sinker addSinker(Sinkers sinker){

        if(sinker==Sinkers.CRYSTAL){
            return new Crystal();
        }else{
            return new Pearl();
        }
    }
}

Tea.class

package milktea;

public interface Sinker {

    String getName();
    Double getPrice();    

}

Matcha.class

package milktea;

public abstract class Tea {

    public enum Size {
        LARGE, MEDIUM, SMALL
    }

    private Double largeSize;
    private Double mediumSize;
    private Double smallSize;
    private Size size;

    public abstract String getName();

    public abstract Sinker getFreeSinker();

    public Double getPrice(){
        System.out.println("LARGE " + this.size);
        if(this.size==Size.LARGE){   
            return largeSize;
        }else if(this.size==Size.MEDIUM){
            return mediumSize;
        }else if(this.size==Size.SMALL){
           return smallSize;
        }
        return 0.0;
    }

}

Wintermelon.class

package milktea.teaType;

import milktea.Sinker;
import milktea.Tea;
import milktea.sinker.Crystal;

public class Matcha extends Tea {

    private final Double largeSize = 3.15;
    private final Double mediumSize = 2.15;
    private final Double smallSize = 1.15;
    private final Size size;

    public Matcha(Size size){
        this.size = size;
    }

    @Override
    public String getName() {
        return "Matcha";
    }

    @Override
    public Sinker getFreeSinker() {
        return new Crystal(0.0);
    }

}

Crystal.java

package milktea.teaType;

import milktea.Sinker;
import milktea.Tea;
import milktea.sinker.NoFreeSinker;

public class Wintermelon extends Tea{

    private final Double largeSize = 3.15;
    private final Double mediumSize = 2.15;
    private final Double smallSize = 1.15;
    private final Size size;

    public Wintermelon(Size size){
        this.size = size;
    }

    @Override
    public String getName() {
        return "Wintermelon";
    }

    @Override
    public Sinker getFreeSinker() {
        return new NoFreeSinker();
    }   

}

NoFreeSinker.class

package milktea.sinker;

import milktea.Sinker;

public class Crystal implements Sinker{

    private Double price;

    public Crystal(Double price){
        this.price = price;
    }

    public Crystal() {
    }

    @Override
    public String getName() {
        return "Crystal";
    }

    @Override
    public Double getPrice() {
        return this.price;
    }

}

Pearl.class

package milktea.sinker;

import milktea.Sinker;

public class NoFreeSinker implements Sinker{

    @Override
    public String getName() {
        return "No free sinker.";
    }

    @Override
    public Double getPrice() {
        return 0.0;
    }

}

这个程序是一个非常简单的Milktea系统,它会输入客户订购的茶。茶可以有一个免费的沉降片,顾客可以添加他/她想要的沉降片。然后系统会写下订单和价格。

除了我上面的问题,你能否告诉我,我是否已经成功编写了这个程序的OOP最多?如果没有,我做错了什么?

1 个答案:

答案 0 :(得分:3)

每个子类都有自己的size定义,隐藏基类“size。您不应该在每个类中定义size,只能在基类中定义。提供一个访问者(按惯例Tea#getSize()),它返回size,以便所有子类都可以访问它。

您的子类应该像这样调用超类的构造函数:

public Matcha(Size size){
    super(size);
}