2017-04-09 13 views
0

私はC#の新機能で、なぜ動作しないのかわかりません。私は異なるプロパティを持つ新しいVehicleオブジェクトを作成する3つのメソッドを作成しようとしています。私は多型でそれをしようとしましたが、それはさらに悪化しています。私はその答えはとても簡単です賭ける..メソッドがオブジェクトの値を設定していません

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

namespace PetrolStation 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     {    

     Timer aTimer = new System.Timers.Timer(); 
     aTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent); 
     aTimer.Interval = 1000; // 1000=1s 
     aTimer.Enabled = true; 
     Console.ReadLine(); 

     void DisplayTimeEvent(object source, ElapsedEventArgs e) 
     { 
      Random rand = new Random(); 
      int temp = rand.Next(1, 3); 

      if (temp == 2) 
      { 
       Vehicles vehicle = new Vehicles(); 
       vehicle.newCar(); 
       Console.Out.WriteLine("test", vehicle.carType); // it should print "car" after test but it doesn't 

      } 
      if (temp == 3) 
      { 
       Vehicles vehicle = new Vehicles(); 
       vehicle.newVan(); 
       Console.Out.WriteLine("test", vehicle.carType);// it should print "van" after test but it doesn't 


      } 
     } 

    } 

} 

セカンドクラス:

public class Vehicles 
    { 

    public string carType; 
    public string fuelType; 
    public int tankCap; 
    public double fuelInTank; 
    public Random rand = new Random(); 



    public void newCar() 
    { 
     carType = "Car"; 
     tankCap = 40; 
     fuelInTank = rand.NextDouble() * 10; 
     int tempFuelType = rand.Next(1, 3); 
     switch (tempFuelType) 
     { 
      case 1: 
       fuelType = "petrol"; 
       break; 
      case 2: 
       fuelType = "Diesel"; 
       break; 
      case 3: 
       fuelType = "LPG"; 
       break; 
     } 

    } 
    public void newVan() 
    { 
     carType = "van"; 
     tankCap = 80; 
     fuelInTank = rand.NextDouble() * 20; 
     int tempFuelType = rand.Next(1, 2); 
     if (tempFuelType == 1) 
     { 
      fuelType = "Diesel"; 
     } 
     else 
     { 
      fuelType = "LPG"; 
     } 


    } 
+0

私はあなたのクラスの実現について話していません、あなたの例はほぼすべてのOOP言語の本彼らは車のどこかで、より多くのことを読んでいます。あなたのサンプルについては、DispleyTimeEventの 'rand.Next(1,3)'に問題があると思いますが、プログラミングのランダムは擬似です。結果値1になると思います。 – Seyran

+0

あなたの問題は何ですか?そして、[MCVE](http://stackoverflow.com/help/mcve)を提供してください。 –

答えて

2
using System; 

namespace StackOverflow_OOP 
{ 
    class Program 
    { 
     // Randomness removed: you want a driver to **consistently** pass or fail. 
     static void Main(string[] args) 
     { 
      Car car = new Car(VehicleFuelType.Petrol, 20); 
      // The first arg specifies format/placement of the second 
      Console.Out.WriteLine("Vehicle Type: {0}", car.Type); 
      // In background, uses String.Format() 
      // See https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx 

      Van van = new Van(VehicleFuelType.Diesel, 40); 
      Console.Out.WriteLine("Vehicle Type: {0}", van.Type); 

      Console.ReadLine(); 
     } 
    } 

    // A string that only takes a small number of values is called an enumeration 
    // See https://msdn.microsoft.com/en-us/library/sbbt4032.aspx 
    public enum VehicleFuelType 
    { 
     Petrol, 
     Diesel, 
     LPG 
    } 

    // Vehicle is clearly abstract in this context, while Car & Van are concrete. 
    // See explaination after code. 
    public abstract class Vehicle 
    { 
     public VehicleFuelType FuelType { get; } 
     public int TankCap { get; } 
     public double FuelInTank { get; private set; } 
     public string Type { get { return this.GetType().Name; } } 

     public Vehicle(VehicleFuelType fuelType, int tankCap, double fuelInTank) 
     { 
      FuelType = fuelType; 
      TankCap = tankCap; 
      FuelInTank = fuelInTank; 
     } 
    } 

    public class Car : Vehicle 
    { 
     public Car(VehicleFuelType fuelType, double fuelInTank) : base(fuelType, 40, fuelInTank) 
     { 
     } 
    } 

    public class Van : Vehicle 
    { 
     public Van(VehicleFuelType fuelType, double fuelInTank) : base(fuelType, 80, fuelInTank) 
     { 
     } 
    } 
} 

クラス:コンクリート

public abstract class Shape 
{ 
    public abstract double GetArea(); 
} 

public class Circle : Shape 
{ 
    public int Radius { get; } 

    public Circle(int radius) 
    { 
     Radius = radius; 
    } 

    public override double GetArea() 
    { 
     return Math.PI * Radius * Radius; 
    } 
} 

public class Square : Shape 
{ 
    public int SideLength { get; } 

    public Square(int sideLength) 
    { 
     SideLength = sideLength; 
    } 

    public override double GetArea() 
    { 
     return SideLength * SideLength; 
    } 
} 
対抽象

抽象クラスと具体的なクラスの最も単純な違い:
抽象クラスはインスタンス化できません(直接); 具体的はである。例えば

Shape shape = new Shape(); // impossible 
Circle circle = new Circle(); // fine 

決定的、

Shape circle = new Circle(); // fine 

概念的には、実際に具体的なクラス(円)を作成することなく、抽象クラス(例えば、形状)を作成することは不可能です。

さらに簡単に言えば、レストランに行き、ウェイターに「食べ物」を欲しいと言っていることを想像してください。明らかに彼は遵守することができますが、 "食品"という言葉に沿ったどこかのステーキやマグロやスパゲッティなどになる必要があります

2

あなたが言及した直接の問題について:

  1. 問題がWriteLineメソッドの呼び出しに関連しています。

  2. あなたは

    ます。Console.Out.WriteLine( "テスト"、vehicle.carType)のたてがみコールの署名。

    だからこの呼び出しの最初のパラメータは、(詳細はhttps://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspxを参照してください)あなたが複合したいアイテムを持っている必要があり

    パブリック仮想ボイドWriteLineメソッド(文字列形式、オブジェクトarg0に)

  3. です。 最終行は次のようなものでなければなりません

    Console.Out.WriteLine( "test {0}"、vehicles.carType);

あなたが言及した "多型" について:

をあなたが実装する多型ではありません。あなたは、多型について少し読みたいかもしれません :

  1. OOP:https://msdn.microsoft.com/en-us/library/mt656686.aspx

  2. 多型:https://msdn.microsoft.com/en-us/library/ms173152.aspx

関連する問題