2016-02-06 43 views
-1

嘿,我剛剛由C#類2周前開始,所以我是初學者程序員 ,我有我的代碼有問題。我有2個類,其中一個是運行程序的測試用例,另一個是私有變量。我的變量顏色,NumOfWheels,StartingPoint,CurrentSpeed和Mileage表示屬性或索引器不能分配給 - 它是隻讀,當我嘗試構建它。我該如何解決?屬性或索引器不能分配到 - 它是隻讀的

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Homework1 
{ 
    class Car 
    { 
     private string color; 
     private int numOfWheels; 
     private int startingPoint; 
     private int mileage; 
     private int currentSpeed; 

     public Car() 
     { 
      Color = ""; 
      NumOfWheels = 4; 
      StartingPoint = 100000; 
      CurrentSpeed = 0; 
      Mileage = 0; 
     } 

     public Car(string color, int numOfWheels, int startingPoint, int currentSpeed, int mileage) 
     { 
      Color = color; 
      NumOfWheels = numOfWheels; 
      StartingPoint = startingPoint; 
      CurrentSpeed = currentSpeed; 
      Mileage = mileage; 
     } 

     public virtual void setcolor(string color) 
     { 
      this.color = color; 
     } 

     public virtual void setnumOfWheels(int numOfWheels) 
     { 
      this.numOfWheels = numOfWheels; 
     } 


     public virtual string Color 
     { 
      get 
      { 
       return color; 
      } 
     } 

     public virtual double NumOfWheels 
     { 
      get 
      { 
       return numOfWheels; 
      } 
     } 

     public virtual int StartingPoint 
     { 
      get 
      { 
       return startingPoint; 
      } 
     } 

     public virtual int CurrentSpeed 
     { 
      get 
      { 
       return currentSpeed; 
      } 
     } 

     public virtual int Mileage 
     { 
      get 
      { 
       return mileage; 
      } 
     } 


     public override string ToString() 
     { 
      return (" color " + color + " numOfWheels" + numOfWheels + "startingPoint " + startingPoint + "mileage" + mileage + "current speed" + currentSpeed); 
     } 
    } 
} 

******************************************************************************** 
/// this is the test case that runs the program 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication8 
{ 
    class CarTest 
    { 
     static void Main(string[] args) 
     { 


      Car myCar = new Car(); 


       Console.WriteLine("*****************************"); 
       Console.WriteLine("*       *"); 
       Console.WriteLine("* WELCOME TO CAR MANAGER *"); 
       Console.WriteLine("* By <<my Name>> *"); 
       Console.WriteLine("*       *"); 
       Console.WriteLine("*****************************"); 



      Console.WriteLine("\nEnter the number of wheels of a car"); 
      int numOfWheels = Console.Read(); 
       myCar.setWheels(numOfWheels); 



      Console.WriteLine("Enter the color of the car"); 
      String color = Console.ReadLine(); 

      Console.WriteLine("Current mileage will be set to zero"); 

      Console.WriteLine("The current starting point will be set to 100000"); 

      Console.Write("The current status of your car \n{0:D} Wheels, \n{1}, \n{2:D} Miles and \nCAR POINT = {3:D}", myCar.getNumOfWheels, 
      myCar.getColor, myCar.getMileage, myCar.getStartingPoint); 

      Console.WriteLine("\nEnter the owner's name"); 
      String name = Console.ReadLine(); 

      Console.WriteLine("Enter the miles the car ran in this week"); 
      int milesThisWeek = Console.ReadLine; 
      myCar.setMileage(Mileage); 

      Console.WriteLine("This car is owned by n{1}", name); 


       Console.WriteLine("===>The current status of your car:"); 
      Console.WriteLine("Wheels: " + myCar.getWheels()); 
      Console.WriteLine("Color: " + myCar.getColor()); 
       Console.WriteLine("Current Mileage: " + myCar.getMileage()); 
      Console.WriteLine("Starting Point: " + myCar.getStartingPoint()); 
      Console.WriteLine("************ Thank you for using CAR MANAGER *************"); 
      Console.WriteLine("----------------------------------------------------------"); 
      Console.WriteLine("----------------------------------------------------------"); 
      Console.WriteLine("Press ENTER to close console…….");  
     } 
    } 
} 

回答

5

你試圖設置屬性:

Color = ""; 

(其他地方),但財產沒有一個二傳手,只有一個getter:

public virtual string Color 
{ 
    get 
    { 
     return color; 
    } 
} 

在爲了設置一個屬性的值,它需要一個setter:

public virtual string Color 
{ 
    get 
    { 
     return color; 
    } 
    set 
    { 
     color = value; 
    } 
} 

(重複你的其他屬性也一樣)


看起來你想創建類似Java的setter方法:

public virtual void setcolor(string color) 
{ 
    this.color = color; 
} 

工作,你可以調用這些,而不是試圖設置屬性:

setColor(""); 

卜t這不是C#中的預期約定。這些屬性可以管理支持變量本身。事實上,你可以完全刪除後備變量,並使用自動實現的屬性的簡單值:

public virtual string Color { get; set; } 

如果你只需要保存一個值,一個簡單的屬性確實是就好了。方法更多的是代碼操作,而不是獲取/設置簡單的值。 (另外,你不希望養成從構造函數調用很多方法的習慣,構造函數應該只是建立對象的狀態,而不是其他任何東西)。

+5

並刪除所有'類似java 'setX,setY方法。 – Steve

相關問題