私は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";
}
}
私はあなたのクラスの実現について話していません、あなたの例はほぼすべてのOOP言語の本彼らは車のどこかで、より多くのことを読んでいます。あなたのサンプルについては、DispleyTimeEventの 'rand.Next(1,3)'に問題があると思いますが、プログラミングのランダムは擬似です。結果値1になると思います。 – Seyran
あなたの問題は何ですか?そして、[MCVE](http://stackoverflow.com/help/mcve)を提供してください。 –