Java 多态性、包、,
大家好,我正在做作业,我被卡住了,所以我需要一些反馈 我在一个包中有一个抽象类Gear和enum类型Info,然后有InfoTest,它使用assert和junit测试来测试Gear和Info的正确性 问题出在InfoTest中。我有信息g=一些值;然后g.getWeight哪个方法在Gear类中,我不能通过Info对象从InfoTest访问getWeight方法 所以我想知道是否有一种方法可以通过InfoTest中的Info对象访问getWeight,或者我的老师犯了一个错误。塔克斯 这是一些代码示例 公共抽象类设备{Java 多态性、包、,,java,Java,大家好,我正在做作业,我被卡住了,所以我需要一些反馈 我在一个包中有一个抽象类Gear和enum类型Info,然后有InfoTest,它使用assert和junit测试来测试Gear和Info的正确性 问题出在InfoTest中。我有信息g=一些值;然后g.getWeight哪个方法在Gear类中,我不能通过Info对象从InfoTest访问getWeight方法 所以我想知道是否有一种方法可以通过InfoTest中的Info对象访问getWeight,或者我的老师犯了一个错误。塔克斯 这是一些
/**
* 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
}
问题是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;
}
然后,在测试中创建一个新的SubGear对象,以便根据需要访问getWeight,如果您只发布代码,将更容易理解您的问题。请包含一些代码,因为仅通过描述itHard来了解没有som的情况下发生了什么,几乎不可能找出问题的实际原因e代码。请发布类和测试的相关部分。粘贴代码会很有帮助,可能是一个私有方法。我如何确切地创建一个构造函数,以便枚举与齿轮相关,我已将枚举移动到齿轮中,构造函数类似于齿轮{Info}家庭作业的问题到底是什么?除非我们知道,否则很难知道你想做什么。比如你想测试毯子的重量吗?嗯,家庭作业更复杂。我需要做的是访问g.getWeight,因为目前我没有。
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;
}