一个关于子类继承父类的问题,求高手指教下菜鸟…

wade0907cc wade0907cc 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
public class New {
public class Vehicle {

private String color;
private String kind;
private int speed;

Vehicle(){
color = "NULL";
kind = "NULL";
speed = 0;
}

public void setSpeed( int speed1){
speed = speed1;
}

public void setColor(String color1){
color = color1;
}

public void setKind(String kind1){
kind = kind1;
}

public int getSpeed(){
return speed;
}

public String getColor(){
return color;
}

public String getKind(){
return kind;
}
}
public class Car extends Vehicle{

private int passengerNumber;

Car(){
super();
passengerNumber = 0;
};

public void setPassengerNumber(int passgerNumber){
this.passengerNumber = passengerNumber;
}

public int getPassengerNumber(){
return passengerNumber;
}
}

public static void main(String []args){
Car myCar = new Car();
myCar.setColor("Black");
myCar.setSpeed(200);
myCar.setKind("Big");
myCar.setPassengerNumber(4);
System.out.println(" My Car :");
System.out.println("Color: " + myCar.getColor());
System.out.println("Speed: " + myCar.getSpeed());
System.out.println("Kind : " + myCar.getKind() );
System.out.println("The number of the passgers: "+ myCar.getPassengerNumber());

}
}

编译时报错为 MAIN方法中 Car myCar = new Car(); 有错,Exception in thread "main" java.lang.Error: Unresolved compilation problem: No enclosing instance of type New is accessible. Must qualify the allocation with an enclosing instance of type New (e.g. x.new A() where x is an instance of New).at New.main(New.java:57)

自己猜想可能是子类继承父类时构造顺序引发的错误,具体还不清楚,求高手指点! 非常感谢!
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
wade0907cc
Eclipse 58,439 成员 49,457 内容