コード例 #1
0
        public static void ClassExamples1Func()
        {
            Car car1 = new Car();
            car1.m_Name = "MX5";
            car1.m_nDoors = 2;
            car1.m_Motor.m_nPowerBHP = 140;

            Car car2 = new Car();
            car2.m_Name = "9-3";
            car2.m_Manufacturer = "Saab";
            car2.m_nDoors = 4;
            car2.m_Motor.m_nPowerBHP = 150;

            Bike bike = new Bike();
            bike.m_Name = "GSX-R750";
            bike.m_Manufacturer = "Suzuki";
            bike.m_Motor.m_nPowerBHP = 120;

            car1.DisplayDetails();
            car2.DisplayDetails();

            Car.DisplayStaticDetails();

            // Default arguments...
            bike.Colour("Red");
            bike.Colour("White", "Blue");
            bike.DisplayDetails();

            // Out parameters...
            int nKW;
            bike.m_Motor.UpdatePowerBHP(160, out nKW);

            // ref parameters...
            int nBHP=0;
            nKW=0;
            bike.m_Motor.UpdateCylinders(4, ref nBHP, ref nKW);

            // Object initializer
            Motor TestMotor = new Motor
            {
                m_nCylinders= 6,
                m_nPowerBHP= 320,
                // m_nPowerKW= 0
            };

            // Is and as conversions
            Vehicle transportation = new Car();

            if (transportation is Car)
                transportation.m_BaseColour = "red";

            if (transportation is Bike)
                transportation.m_BaseColour = "blue";

            Console.WriteLine("transportation is {0}", transportation.m_BaseColour);

            Bike mybike = transportation as Bike;
            if (mybike != null)
            {
                mybike.m_Manufacturer = "yamaha";
            }

            Car mycar = transportation as Car;
            if (mycar != null)
            {
                mycar.m_Manufacturer = "ford";
            }
        }