嘿,我刚刚练习继承,我遇到了一个问题.我在我的汽车类(子类)中得到错误,车辆(父)中的变量不可见.我没有做任何改变这一点,我甚至不知道如何让它隐形.谁能帮我这个.
public class Vehicle
{
private String make, model, colour;
private int registrationNumber;
public Vehicle()
{
this.make = "";
this.model = "";
this.colour = "";
this.registrationNumber = 0;
}
public Vehicle(String make, String model, String colour,
int registrationNumber)
{
this.make = make;
this.model = model;
this.colour = colour;
this.registrationNumber = registrationNumber;
}
public String getMake()
{
return make;
}
public void setMake(String make)
{
this.make = make;
}
public String getModel()
{
return model;
}
public void setModel(String model)
{
this.model = model;
}
public String getColour()
{
return colour;
}
public void setColour(String colour)
{
this.colour = colour;
}
public int getRegistrationNumber()
{
return registrationNumber;
}
public void setRegistrationNumber(int registrationNumber)
{
this.registrationNumber = registrationNumber;
}
public String toString()
{
return "Vehicle [make=" + make + ", model=" + model + ", colour="
+ colour + ", registrationNumber=" + registrationNumber + "]";
}
}
public class Car extends Vehicle
{
private int doors;
private String shape;
public Car()
{
super();
this.doors = 0;
this.shape = "";
}
public Car(String make, String model, String colour, int registrationNumber)
{
super(make, model, colour, registrationNumber);
this.make = make;
this.model = model;
this.colour = colour;
this.registrationNumber = registrationNumber;
}
}
Run Code Online (Sandbox Code Playgroud)
错误消息:
Description Resource Path Location Type
The field Vehicle.make is not visible Car.java /VehicleApp/src line 19 Java Problem
The field Vehicle.model is not visible Car.java /VehicleApp/src line 20 Java Problem
The field Vehicle.colour is not visible Car.java /VehicleApp/src line 21 Java Problem
The field Vehicle.registrationNumber is not visible Car.java /VehicleApp/src line 22 Java Problem
Run Code Online (Sandbox Code Playgroud)
private
变量将不可见.它们仅限于它们所定义的类.protected
变量(和函数)对于子类是可见的.
更改:
private String make, model, colour;
private int registrationNumber;
Run Code Online (Sandbox Code Playgroud)
至
protected String make, model, colour;
protected int registrationNumber;
Run Code Online (Sandbox Code Playgroud)
我建议不要将它们保密,因为这意味着它Car
不会从中继承这些变量Vehicle
.强迫a Car
调用它自己的父母的getter和setter正在引入Car
和之间的差距Vehicle
.它说,a Car
没有颜色,品牌,型号或注册号,但它必须转到一个单独的类(Vehicle
)并向类询问它的颜色,品牌,型号和注册号.
归档时间: |
|
查看次数: |
1162 次 |
最近记录: |