static void Main(string[] args) { IStuff IStuff = new DerivedClass1("data1 here"); IStuff.DoStuff(); // prints "1: data here" IStuff = new DerivedClass2("data2 here"); IStuff.DoStuff(); // prints "1: data here" Console.ReadKey(); }
public static void Main (string[] args) { var a = new DerivedClass(); var b = new DerivedClass2(); a.Method1(); a.Method2(); b.Method1(); b.Method2(); }
public static void Main(string[] args) { var a = new DerivedClass(); var b = new DerivedClass2(); a.Method1(); a.Method2(); b.Method1(); b.Method2(); }
public static void TestUseCase2() { var dc1 = new DerivedClass2(); dc1.AddValue(5); Assert.AreEqual(dc1.GetValues().Count, 1, "Bridge522 dc1.Count = 1"); var dc2 = new DerivedClass2(); Assert.AreEqual(dc2.GetValues().Count, 0, "Bridge522 dc2.Count = 0"); }
static void SwapAndVirtualCalls() { BaseClass a = new DerivedClass1(); BaseClass b = new DerivedClass2(); for (int i = 0; i < 50000; i++) { a.Test(); Swap(ref a, ref b); } }
public static void TestUseCase2() { var dc1 = new DerivedClass2(); dc1.AddValue(5); Assert.AreEqual(1, dc1.GetValues().Count, "Bridge522 dc1.Count = 1"); var dc2 = new DerivedClass2(); Assert.AreEqual(0, dc2.GetValues().Count, "Bridge522 dc2.Count = 0"); }
public void ExerciseDerivedObjects() { Console.WriteLine($"Starting method: {MethodBase.GetCurrentMethod().Name}()"); SendMessages.WriteConsoleMessageStatic("Static func in regular class. Cannot access through an instance"); Messages msgs = new SendMessages(); msgs.WriteMessage("Writing msg using abstract class"); // msgs.WriteConsoleMessageStatic("..."); // Cannot call a static func through a class instance var sendMsgs = new SendMessages(); sendMsgs.WriteMessage("Calling implementation of abstract method"); sendMsgs.WriteConsoleMessage("Calling the override method in the derived class"); var bc = new BaseClass(); var dc = new DerivedClass(); BaseClass bcdc = new DerivedClass(); // Slicing occurs if assign an object of a derived class to an instance of a base class BaseClass bcdc2 = new DerivedClass2(); Console.WriteLine("Calling bc.Method1()"); bc.Method1(); Console.WriteLine("Calling dc.Method1()"); dc.Method1(); Console.WriteLine("Calling dc.Method2()"); dc.Method2(); Console.WriteLine("Calling bcdc.Method1()"); bcdc.Method1(); // Will call method1 in derived class because it is overridden Console.WriteLine("Calling bcdc.Method2()"); bcdc.Method2(); // Will call method2 in base class because it is not overridden Console.WriteLine("Calling dc.Method3()"); dc.Method3(); // Will call method3 in derived class because it is overridden // and will also call the base class method throught base.Method3() dc.Name = ""; // Assign a value to a property override dc.Name = "Bart"; dc.Name = null; Console.WriteLine("Call methods in DerivedClass2 from BaseClass Instance"); bcdc2.Method1(); bcdc2.Method2(); bcdc2.Method3(); // bc.staticValue = 500; // Not assesible through instance BaseClass.staticValue = 500; // Assesible only through type Mystatic.Y = 777; Mystatic.WriteValue(); // var m = new Mystatic(); // Cannot create an instance of a static class }
public static void TestUseCase2(Assert assert) { assert.Expect(2); var dc1 = new DerivedClass2(); dc1.AddValue(5); assert.Equal(dc1.GetValues().Count, 1, "Bridge522 dc1.Count = 1"); var dc2 = new DerivedClass2(); assert.Equal(dc2.GetValues().Count, 0, "Bridge522 dc2.Count = 0"); }
public void FindClass_BaseDerived_Base() { var foo1 = new DerivedClass1 { Foo = "bar" }; var foo2 = new DerivedClass2 { Foo = "baz" }; var expression1 = (Expression <Func <object> >)(() => foo1.Foo); var expression2 = (Expression <Func <object> >)(() => foo2.Foo); var(type, instance) = ClassFinder.FindClass(expression2); Assert.AreEqual(typeof(DerivedClass2), type); Assert.AreSame(foo2, instance); }
public void Ex2() { DerivedClass B = new DerivedClass(); B.DoWork(); // Calls the new method. BaseClass A = (BaseClass)B; A.DoWork(); // Calls the old method. Console.WriteLine(subcaption); B.DoWork2(); // переопределённый метод A.DoWork2(); // переопределённый метод Console.WriteLine(subcaption); BaseClass C = new DerivedClass2(); B.DoWork3(); A.DoWork3(); C.DoWork3(); }