-1
存在自動屬性問題。下面是從我的類片段:Getters Setters Autoproperty問題
class Car
{
string brand;
string model;
Engine CarEngine {get; set;}
現在該類引擎:
class Engine
{
double FuelCap { get; }
double fuelCapDefault = 50;
double FuelCount { get; set;}
double engineCap;
我創建了構造一個對象:
public Car(string brand, string model, double engineCap, double FuelCount, double FuelCap)
{
this.brand = brand;
this.model = model;
this.CarEngine = new Engine(engineCap, FuelCount, FuelCap);
}
和發動機的構造:
public Engine(double engineCap, double FuelCount, double FuelCap)
{
this.engineCap = engineCap;
this.FuelCount = FuelCount;
this.FuelCap = FuelCap;
}
我有crea泰德與對象:
Car car = new Car(x, y, z, a, b);
我們的主要觀點,最後:
我試圖訪問car.CarEngine.FuelCount我在主程序中獲得的價值,但視覺甚至不建議我CarEngine。我是新來的整個autoproperty的東西。 我做錯了什麼?
你忘了做'CarEngine'公開。 「'公共引擎CarEngine {get; set;}'」,與'FuelCount'相同 –
@ScottChamberlain我使用getters/setters的整個目的不是爲了公開它:( – user3653415
Getters和setter默認使用父範圍,如果你想要一個公共的getter和一個私人的setter,那麼''public Engine CarEngine {get; private set;}' –