-4

みなさん、こんにちは。宿題をしているのですが、行き詰まってしまったので、少しフィードバックが必要です。

1つのパッケージに抽象クラスGearと列挙型Infoがあり、次に、assertテストとjunitテストを使用してGearとInfoの正確さをテストするInfoTestがあります。

問題はInfoTestにあります。GearクラスにあるメソッドがInfo g = some Value;ありg.getWeight()、InfoTestからInfoオブジェクトを介してgetWeight()メソッドにアクセスできません。

そのため、InfoTestのInfoオブジェクトを使用してgetWeight()にアクセスする方法があるのか​​、それとも先生が間違えたのか疑問に思いました。サックス。

OKKKKKKKKKKKここにいくつかのコードサンプルがあります。

パブリック抽象クラスGear{

/**
 * weight stores the weight of the item in kg
 */
protected int weight;

/**
 * code store the code for the item (bedding, food, medical or sanitary, but it is better to use array.
 */
protected char code;

/**getWeight gets the weight of the item.
 * 
 * @return weight
 */        
public int getWeight() {
    return weight;
}

/**setWeight sets the weight for the item
 * 
 * @param weight 
 */
public void setWeight(int weight) {
    this.weight = weight;
}

/**getCode() gets the code of the item.
 * 
 * @return code
 */
public char getCode() {
    return code;
}

/**setCode sets the code of the item.
 * 
 * @param code 
 */
public void setCode(char code) {
    this.code = code;
} }

パブリック列挙型情報{

/**
 * BLANKETS represent the blankets.
 */
BLANKETS,

/**
 * PILLOWS represent the pillows 
 */
PILLOWS,

/**
 * FOOD represent the food
 */
FOOD,

/**
 * MEDKIT represent the medical kit.
 */
MEDKIT,

/**
 * OXYGEN represent the oxygen 
 */
OXYGEN,

/**
 * NAPKINS represent the napkins.
 */
NAPKINS,

/**
 * SICKNESSBAG represent the sickness bags.
 */
SICKNESSBAG
 }
> public class InfoTest {  
      /**
>      * Test of getWeight() method, of class Info. 
>      */
>     @Test
>     public void testGetWeight() {
>       System.out.print("\tChecking weights in Info");
>       Info g = Info.BLANKETS;
>       final int expectedValue1 = 2;
>       assertEquals(expectedValue1, g.getWeight()); }
> 
> }
  
4

1 に答える 1

1

問題は、Infoが列挙型クラスであり、現時点ではGearクラスにアクセスできないことです。あなたの宿題の範囲を正確に理解することは非常に困難です。

このようなもの...まず、抽象クラスを拡張する別のクラス(SubGearのようなもの)を作成します

public SubGear extends Gear

このクラスには、Infoオブジェクトを受け取り、Gearクラスのフィールドに次のようにデータを入力するコンストラクターがあります。

public SubGear (Info info, int weight, char code){
    this.code = code;
    this.weight= weight;
    this.info = info
}

public void setInfo(Info info){
     this.info = info;
}

public Info getInfo(){
    return info;
}

次に、テストで、必要に応じてgetWeightにアクセスするために、新しいSubGearオブジェクトを作成します。

于 2012-11-29T14:23:20.630 に答える