所以这不是一项任务,但我的一张演讲幻灯片没有说清楚,当我尝试自己编写类似的代码时,我遇到了问题。
我不知道如何填充我的子类中的变量。
到目前为止,这是我的测试代码:
实现类:
import javax.swing.JOptionPane;
公共类房子{
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int count = 0;
room[] r = new room[3];
int option = JOptionPane.showConfirmDialog(null, "type");
if(option==0){
r[count]=new type();
r[count].setSize(25);
r[count].setType("Bedroom");
}
else if(option==1){
r[count] = new room();
r[count].setSize(25);
}
JOptionPane.showMessageDialog(null, r[count].toString());
}
}
超类:
public class room {
double size;
public room(){
}
public room(double size){
this.size=size;
}
public double getSize(){return this.size;}
public boolean setSize(double size){
if(size<0.0){
return false;
}
else{
return true;
}
}
public String toString(){
return "Room size: " + this.size;
}
}
子类:
public class type extends room {
String type;
public type(){
}
public type (double size, String type){
super(size);
this.type=type;
}
public String getType(){return this.type;}
public boolean setType (String type){
if(type.equals("")){
return false;
}
else{
return true;
}
}
public String toString(){
return super.toString() + "Room Type: " + this.getType();
}
}
当我尝试运行代码时,如果我尝试将数据插入到类型类中的 setType() 方法中,它会给我一个错误。有人可以告诉我我在哪里搞砸了吗?谢谢!