public void CreatePorsche() { var car = new Porsche(); Assert.Equal("Porsche", car.Name); Assert.Equal(100000, car.Cost); }
static void Main(string[] args) { //We cannot create instances of Car, but we can //treat more specialized instances (like Porsche) //as a Car Car porsche = new Porsche(); Car mercedes = new Mercedes(); //The same with interfaces. Creating an instance //is not possible, but treating an existing instance //as the interface is possible. if (porsche is ITrunk) { ((ITrunk)porsche).OpenTrunk(); } //While the one before was obviously not right (we //see that porsche does not implement ITrunk, hence //porsche is ITrunk will be false and not execute //the OpenTrunk method), this one will work. if (mercedes is ITrunk) { ((ITrunk)mercedes).OpenTrunk(); } }
public void CreatePorscheWithColor() { Car car = new Porsche(); car = new ColorDecorator(car); Assert.Equal("Porsche + color", car.Name); Assert.Equal(100250, car.Cost); }
/* theory * private int mSomeMember = 3; * public int MyProperty { get; set; } = 10; * * public int MyPropertyExtended * { * get => mSomeMember; * set * { * if (value > 0) * mSomeMember = value; * } * } * * public Program() * { * mSomeMember = 5; * } * * static void Main(string[] args) * { * var program = new Program(); * * var car = new Car(); * * //car.WheelSize = 10; * * System.Console.WriteLine(car.WheelSize); * } */ public static void Main(string[] args) { var mazda = new Mazda(); var porsche = new Porsche(); var car = new Car(); SomeFunction(mazda); }
public void CreatePorscheWithColorAndLeather() { Car car = new Porsche(); car = new ColorDecorator(car); car = new LeatherDecorator(car); Assert.Equal("Porsche + color + leather", car.Name); Assert.Equal(100700, car.Cost); }
static void Main(string[] args) { //A new vehicle is created, the Console will display "Vehicle created." Vehicle veh = new Vehicle(); //Let's create a new car Car car = new Car(); //The Console displayed "Vehicle created." and "Car built." (in that order) //Here we create a new Porsche, but treat it as a vehicle Vehicle porsche = new Porsche(); //The console displayed all 3 constructor messages, starting with the most //basic constructor, the one from vehicle, then the one from the car and //finally the invoked one. veh.Drive(); //A driving vehicle. car.Drive(); //A driving car! porsche.Drive(); //WOOOOAH! This is a Porsche driving!! //So the re-implementation works - and it still works when we treat Porsche as Vehicle //However, if we just hide it like here: veh.Brake(); //Vehicle stopped. porsche.Brake(); //Vehicle stopped. //So hiding is different, but how to display the hidden implementation? Porsche myporsche = (Porsche)porsche; myporsche.Brake(); //A Porsche cannot be stopped ... //Casting works. A much better way (in general) would have been one of those two: //is will return true or false depending if a cast is possible if (porsche is Porsche) { ((Porsche)porsche).Brake(); } //Or using "as" (if no cast possible then the return value is null) Porsche anotherPorsche = porsche as Porsche; if (porsche != null) { anotherPorsche.Brake(); } }
public void ShowCurrentSpeed_NewPorsche_ReturnsPorscheSpeed() { //arrange const string expected = "40"; var test = new Porsche(); test.SpeedUp(); test.SpeedUp(); test.SlowDown(); test.UseNitrousOxideEngine(); //act var actual = test.ShowCurrentSpeed(); //assert Assert.Equal(expected, actual); }
public void StillOutOfSafetyGap() { var controller = new Controller(); var porsche = new Porsche(controller) { Location = 1500 }; var bmw = new BMW(controller) { Location = 1000 }; controller.Register(porsche); controller.Register(bmw); Assert.Equal(1500, porsche.Location); Assert.Equal(1000, bmw.Location); porsche.Location = 1090; Assert.Equal(1090, porsche.Location); porsche.Location = 1190; Assert.Equal(1100, porsche.Location); }