2015-11-20 82 views
-2
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Assignment13 { 

    class Car 
    { 
     int Speed = 0; 
     double Temp = 0.0; 

     public void setSpeed(int speed) 
     { 
      this.Speed = 50; 
     } 
     public void getSpeed() 
     { 
      return; 
     } 
     public void setTemp(double Temp) 
     { 
      this.Temp = 70.5; 
     } 
     public void getTemp() 
     { 
      return; 
     } 
    } 
    class program 
    { 
     static void Main (string[]args) 
     { 
    Car ford = new Car(); 
    ford.setSpeed(50); 
    ford.setTemp(70.5); 
    Console.WriteLine("Speed = " + ford.setSpeed); 
    Console.WriteLine("Temp = " + ford.setTemp); 



    Console.Write("Hit any key to close"); Console.ReadKey(true); 
     } 
    } 
} 

我無法打印出速度和溫度//如何添加參數 list要符合方法簽名?溫度和價格?

+0

Console.WriteLine( 「速度=」 + ford.get); Console.WriteLine(「Temp =」+ ford.get); –

回答

1

您應該使用get方法而不是set ... 那麼你應該返回值作爲更新下面的代碼..

class Car 
{ 
    private int Speed = 0; 
    private double Temp = 0.0; 

    public void setSpeed(int value) 
    { 
     this.Speed = value; 
    } 
    public int getSpeed() 
    { 
     return Speed; 
    } 
    public void setTemp(double value) 
    { 
     this.Temp = value; 
    } 
    public double getTemp() 
    { 
     return Temp; 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     Car car = new Car(); 
     car.setSpeed(50); 
     car.setTemp(20.50); 
     Console.WriteLine("Speed = " + car.getSpeed()); 
     Console.WriteLine("Temp = " + car.getTemp()); 
    } 
} 
+1

void方法如何返回一個值? –

+0

@RonBeyer Opps我沒有看到是無效的...應該改變它返回int ...更新代碼.. – Nic

+0

@Nic仍然無法編譯它出於某種原因! – AnsBekk