コード例 #1
0
        public static void InheritanceTransportExample()
        {
            Transport transport = new FuelCar()
            {
                FuelUsage = 10, Fuel = 45, Distance = 25045
            };
            var transport2 = new Transport {
                Distance = 34, MaxSpeed = 5
            };

            Transport unknowedTransport = new Transport {
                Distance = 34, MaxSpeed = 5
            };

            unknowedTransport = new FuelCar()
            {
                FuelUsage = 10, Fuel = 45, Distance = 25045
            };
            //var fuel = unknowedTransport.Fuel;

            FuelCar maserati = new FuelCar()
            {
                FuelUsage = 10, Fuel = 45, Distance = 25045
            };
            Transport winner     = maserati;
            FuelCar   firstPlace = (FuelCar)winner;


            Transport fCar = new FuelCar()
            {
                Distance = 150, Fuel = 50, FuelUsage = 13
            };
            Transport eCar = new ElectroCar()
            {
                Distance = 300, Battery = 500, DistanceBattery = 200
            };

            var cars = new Transport[] { fCar, eCar };

            foreach (var car in cars)
            {
                car.Move(10);
            }
        }
コード例 #2
0
        public static void InheritanceTransportExample()
        {
            var transport = new FuelCar()
            {
                FuelUsage = 10, Fuel = 45, Distance = 25045
            };
            var transport2 = new Transport {
                Distance = 34, MaxSpeed = 5
            };


            var unknowedTransport = new FuelCar()
            {
                FuelUsage = 10, Fuel = 45, Distance = 25045
            };
            var electroVehicle = new ElectroCar()
            {
                MaxSpeed = 140, Battery = 55
            };

            FuelCar maserati = new FuelCar()
            {
                FuelUsage = 10, Fuel = 45, Distance = 25045
            };
            Transport winner     = maserati;
            FuelCar   firstPlace = (FuelCar)winner;

            transport.Engine         = 350;
            maserati.Engine          = 240;
            unknowedTransport.Engine = 168;
            electroVehicle.Engine    = 100;
            Console.WriteLine(electroVehicle < unknowedTransport);
            Console.WriteLine(maserati > transport);
            Console.WriteLine(transport != electroVehicle);
            Console.WriteLine(unknowedTransport == maserati);
        }