方法不设置对象的值

时间:2017-04-09 04:57:48

标签: c#

我是C#的新手,不知道为什么它不起作用。我正在尝试创建3个方法来创建具有不同属性的新Vehicle对象。我试图用多态来做这件事,但情况更糟。我打赌这个答案很容易......

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";
        }


    }

2 个答案:

答案 0 :(得分:2)

关于你提到的直接问题:

  1. 问题与WriteLine调用有关。

  2. 你鬃毛的签名

    Console.Out.WriteLine(“test”,vehicle.carType);

    public virtual void WriteLine(string format,object arg0)

  3. 因此,此调用中的第一个参数应该包含您要合成的项目(有关详细信息,请参阅https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx)。 底线调用应该像

    Console.Out.WriteLine(“test {0}”,vehicle.carType);

  4. 关于你提到的“多态性”:

    您实施的不是多态性。 您可能想稍微阅读一下多态性:

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

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

答案 1 :(得分: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

从概念上讲,在不实际创建具体类(圆形)的情况下创建抽象类(例如形状)是不可能的。

更简单地说,想象一下去餐馆并告诉服务员你想要“食物”。显然他可以遵守,但沿线的“食物”必须成为牛排或金枪鱼或意大利面等。