简体   繁体   中英

Polymorphism, packages,

Hi everyone I am doing my homework and I got stuck so I need a bit of feedback.

I have a abstract class Gear and enum type Info in one package, then I have InfoTest which uses the assert and junit test to test the correctness of the Gear and the Info.

Problem is in the InfoTest. I have Info g = some Value; and then g.getWeight() which method is in the Gear class and I can't access the getWeight() method from InfoTest through Info object.

So I was wondering whether there is a way to access getWeight() with an Info object from InfoTest or my teacher has made a mistake. Thaks.

OKKKKKKKKKKK here is some code sample.

public abstract class 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; } }

public enum Info {

 /** * 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()); }
> 
> }
  

The problem is that Info is an enumerated class, it does not have access to the Gear class at the moment. Its very difficult to work out exactly what the scope of your homework is.

Something like this... Firstly make another class which extends your abstract class (something like SubGear)

public SubGear extends Gear

In this class have a constructor which takes in the Info object and also populates the fields from the Gear class like this:

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;
}

Then in your test create a new SubGear object in order to access the getWeight as you require

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM