2016-02-06 79 views
-4
i am having trouble with my set and gets in my test class. it says it does not 

包含用於定義______和沒有擴展方法接受第一個參數。我讀過類似的問題,但我無法修復這些錯誤。我怎樣才能解決這個問題?具體來說,它說我的車類不包含定義不包含「」的定義,並沒有擴展方法接受第一個參數

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 string Color 
     { 
      get 
      { 
       return color; 
      } 
      set 
      { 
       color = value; 
      } 
     } 

     public virtual int NumOfWheels 
     { 
      get 
      { 
       return numOfWheels; 
      } 
      set 
      { 
       numOfWheels = value; 
      } 
     } 

     public virtual int StartingPoint 
     { 
      get 
      { 
       return startingPoint; 
      } 
      set 
      { 
       startingPoint = value; 
      } 
     } 

     public virtual int CurrentSpeed 
     { 
      get 
      { 
       return currentSpeed; 
      } 
      set 
      { 
       currentSpeed = value; 
      } 
     } 

     public virtual int Mileage 
     { 
      get 
      { 
       return mileage; 
      } 
      set 
      { 
       mileage = value; 
      } 
     } 


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

******************************************************************************** 
///this is the test program 

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

namespace Homework1 
{ 
    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.setNumOfWheels(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…….");  
     } 
    } 
} 
+3

爲什麼你會忽略錯誤信息中最相關的信息? –

+0

也注意到您正在使用以下內容 Console.WriteLine(「此車屬於n {1}」,名稱); 索引應始終從0開始 – Bart

回答

2

在你的車類,你有這個屬性(其中包括):

public virtual int NumOfWheels 
    { 
     get 
     { 
      return numOfWheels; 
     } 
     set 
     { 
      numOfWheels = value; 
     } 
    } 

,並嘗試使用這樣的:

myCar.setNumOfWheels(numOfWheels); 

setNumOfWheels()不存在。你,而不是需要使用屬性語法使用NumOfWheels

myCar.NumOfWheels = 4; // sets the property 
Console.WriteLine(myCar.NumOfWheels); // gets the property 

查看更多here


順便說一句,沒有什麼能夠阻止您對屬性使用自動屬性語法。就他們而言,除了獲得幕後的價值之外,他們什麼都不做。所以,你可以省略明確的私有變量,只是聲明NumOfWheels這樣的:

public virtual int NumOfWheels { get; set; } 

,讓你現在有相同的行爲。

相關問題