第一年CS考试,需要指导

时间:2017-05-07 20:22:34

标签: java c# inheritance computer-science abstract

我明天在java考试。它专注于封装,内聚,耦合,继承和接口。我创建了一个七个类的项目,扬声器和耳机类继承自声音类和产品类,而DjMixer仅从产品实现。主类仍然是空的。我需要知道我是否在代码设计中犯了任何重大错误。

public interface Product
{

    double getPrice();

    void setPrice(double price);

    String getId();

    void setId(String Id);

    String getPlace();

    void setPlace(String place);
}

public abstract class Sound
{

public abstract void setDesibel(int desibel);

public abstract int getDesibel();

public void amp() {
        System.out.println("Recomended amplifier effect is 40-250 watts");
    }

}

public class Speaker extends Sound implements Product
{

    double price;
    String id;
    String place;
    int desibel;

    /**Price in Norwegain Kroner */    
public double getPrice(){
        return price;
    }

    /**Price is set in Norwegain Kroner */ 
public void setPrice(double price){
        this.price = price;
    }

    /**Location of the product*/
public String getPlace(){
        return place;
    }

public void setPlace(String place){
        this.place = place;
    }

    /**The products European Article Number*/
public String getId(){
        return id;
    }

    /**type in the European Article Number under the barcode*/
public void setId(String serialNum){
        id = serialNum;
    }

    //Constructor
public Speaker(double price, String location, String serialNum) {
        super.amp();
        this.price = price;
        place = location;
        id = serialNum;
    }

public int getDesibel() {
        return this.desibel;
    }

public void setDesibel(int desibel) {
        this.desibel = desibel;
    } 
}

public class Headset extends Sound implements Product
{

    double price;
    String place;
    String id;
    int desibel;
    boolean wireless = true;

    /**Price is in Norwegain Kroner */ 
public double getPrice(){
        return price;
    }

    /**Price is set in Norwegain Kroner */ 
public void setPrice(double price){
        this.price = price;
    }

    /**Location of the product*/
public String getPlace(){
        return place;
    }

    /**Set location where item is stored */
public void setPlace(String location){
        place = location;
    }

    /**The products European Article Number*/
public String getId(){
        return id;
    }

    /**type in the European Article Number under the barcode*/
public void setId(String serialNum){
        id = serialNum;
    }

    //Constructor
public Headset(double price, String location, String serialNum) {
        super();
        this.price = price;
        place = location;
        id = serialNum;
    }

public void setDesibel(int desibel) {
        this.desibel = desibel;
    }

public int getDesibel() {
        return this.desibel;
    }
}

public class DjMixer implements Product
{


    double price;
    String id;
    String place = "Storage room, shelf 3";

    /**Price in Norwegain Kroner */
public double getPrice(){
        return price;
    }

    /**Price is set in Norwegain Kroner */ 
public void setPrice(double price){
        this.price = price;
    }

    /**Mixing tables is stored on shelf 3, if moved please set new place */
public String getPlace(){
        return place;
    }

    /**Set new place for mixing table if moved */
public void setPlace(String location){
        place = location;
    }

    /**The products European Article Number*/
public String getId(){
        return id;
    }

    /**type in the European Article Number under the barcode*/
public void setId(String serialNum){
        id = serialNum;
    }

public DjMixer(double price, String serialNum){
        this.price = price;
        id = serialNum;
    }
}

1 个答案:

答案 0 :(得分:0)

将任何可以通过“get”方法访问的变量声明为private是一个好主意。此外,你在一些方法中使用“desibel”而不是“decibel”。如果您的老师教过final关键字,则无线boolean似乎应该声明为此类。