static void Main(string[] args) { #region 接口实现多态 //麻雀会飞,鹦鹉会飞,鸵鸟会飞,企鹅会飞,直升机会飞 //用多态来实现,方法,抽象类,接口 //IFlyable fly = new Helicopper();//new YinWu();//new MaQue(); //fly.Fly(); #endregion #region 接口的修饰符 MyBase mb = new MyBase(); MyDerivedClass dc = new MyDerivedClass(); mb.DoSomething(); IMyInterface imb = mb; imb.DoSomethingElse(); Console.WriteLine("-------------------"); dc.DoSomething(); dc.DoSomethingElse(); Console.WriteLine("-------------------"); //MyDerivedClass dc1 = (MyDerivedClass) mb;//父类不能赋值给子类 #endregion Console.ReadKey(); }
public void Test() { MyDerivedClass.MyAbstractString = "I am MyDerivedClass"; MyDerivedClassTwo.MyAbstractString = "I am MyDerivedClassTwo"; Debug.Print(MyDerivedClass.GetMyAbstracString()); Debug.Print(MyDerivedClassTwo.GetMyAbstracString()); }
static void Main() { MyDerivedClass derived = new MyDerivedClass(); MyBaseClass mybc = (MyBaseClass)derived; Console.WriteLine(derived.MyProperty); Console.WriteLine(mybc.MyProperty); }
static void Main() { MyDerivedClass derived = new MyDerivedClass(); MyBaseClass mybc = (MyBaseClass)derived; derived.Print(); // Call Print from derived portion. mybc.Print(); // Call Print from base portion. }
static void Main(string[] args) { //MyClass myClass = new MyClass(); // Запрещено! НЕЛЬЗЯ СОЗДАТЬ ЭКЗЕМПЛЯР АБСТРАКТНОГО КЛАССА (АК) MyClass myClass = new MyDerivedClass () ; // создаем экземпляр производного класса //myClass.DoSomething(); //ОШИБКА - т.к. не был реализован абстрактный метод(АК) }
public static void Main() { MyBaseClass b = new MyBaseClass(); MyDerivedClass d = new MyDerivedClass(); Console.WriteLine("Base Method One: {0}", b.MethodOne()); Console.WriteLine("Derived Method One: {0}", d.MethodOne()); }
static void Main() { MyDerivedClass derived = new MyDerivedClass(); MyBaseClass mybc = (MyBaseClass)derived; derived.Print(); mybc.Print(); }
public void _01() { //---------------------------------------------------------------------------------------------------- // 创建子类实例时,会先调用父类的 constructor,再调用子类的 constructor, // 默认调用的是父类的 parameterless constructor,因此如果父类没有 parameterless constructor,会编译报错, //---------------------------------------------------------------------------------------------------- var myDerivedClass = new MyDerivedClass(); }
public static void Main() { MyBaseClass myBase = new MyBaseClass(); MyDerivedClass myDerived = new MyDerivedClass(); object o = myDerived; //MyBaseClass b = myDerived; var b = myDerived; Console.WriteLine("myBase: Type is {0}", myBase.GetType()); Console.WriteLine("myDerived: Type is {0}", myDerived.GetType()); Console.WriteLine("object o = myDerived: Type is {0}", o.GetType()); Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType()); Console.WriteLine("-----------------------------------------"); int n1 = 12; int n2 = 82; long n3 = 12; Console.WriteLine("n1 and n2 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n2.GetType())); Console.WriteLine("n1 and n3 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n3.GetType())); Console.WriteLine("-----------------------------------------"); object[] values = { (int)12, (long)10653, (byte)12, (sbyte)-5, 16.3, "string" }; foreach (var value in values) { Type t = value.GetType(); if (t.Equals(typeof(byte))) { Console.WriteLine("The value: {0} is an unsigned byte.", value); } else if (t.Equals(typeof(sbyte))) { Console.WriteLine("The value: {0} is a signed byte.", value); } else if (t.Equals(typeof(int))) { Console.WriteLine("The value: {0} is a 32-bit integer.", value); } else if (t.Equals(typeof(long))) { Console.WriteLine("The value: {0} is a 32-bit integer.", value); } else if (t.Equals(typeof(double))) { Console.WriteLine("The value: {0} is a double-precision floating point.", value); } else { Console.WriteLine("The value: '{0}' is another data type.", value); } } }
static void Main(string[] args) { var x = new MyDerivedClass(); Console.WriteLine(x.ToString()); Console.WriteLine(x.Instance.ToString()); Console.ReadKey(); }
public void CallMethodWithThrow_InstanceMethodExistsOnDerivedType_ExpectToBeCalled() { MyDerivedClass myClass = new MyDerivedClass(); ReturnValueClass retVal = (ReturnValueClass)ReflectionHelper.CallMethodWithThrow(myClass, "MyDerivedObjectMethod", 3, 9); Assert.IsNotNull(retVal); Assert.AreEqual(3, retVal.X); Assert.AreEqual(9, retVal.Y); }
public void GetResult_InstanceMethodExistsOnDerivedType_ExpectToBeCalled() { MyDerivedClass myClass = new MyDerivedClass(); ReturnValueClass retVal = (ReturnValueClass)ReflectionHelper.GetResult(myClass, "MyDerivedObjectMethod", new object[] { 3, 9 }); Assert.IsNotNull(retVal); Assert.AreEqual(3, retVal.X); Assert.AreEqual(9, retVal.Y); }
public void Should_Map_Derived_To_Target() { var derived = new MyDerivedClass { Value = "fortytwo" }; var target = _mapper.Map <MyTargetClassToMap>(derived); target.Value.ShouldBe("fortytwo"); }
public void TestThatMyDependencyIsCalled() { var unitUnderTest = new MyDerivedClass(); var unitUnderTest.Method2(); /* verify base class behavior #1 inside Method1() */ /* verify base class behavior #2 inside Method1() */ /* ... */ }
public void _test3b() { IReturnOnly <int> myDerivedClass = new MyDerivedClass(); AssertSame(42, myDerivedClass.ReturnOnly()); IReturnOnly returnOnly = new MyDerivedClass(); AssertSame(42, returnOnly.ReturnOnly()); }
static void Main() { MyDerivedClass derived = new MyDerivedClass(); MyBaseClass mybc = (MyBaseClass)derived; derived.Print(); // Call Print from derived portion. mybc.Print(); // Call Print from base portion. // mybc.var1 = 5; // Error: base class reference cannot // access derived class members. }
public void _01() { var derived = new MyDerivedClass(); derived.Print(); // 父类变量指向子类对象, 通过父类变量只能访问父类成员 MyBaseClass mybc = derived; mybc.Print(); }
public void test3() { var myDerivedClass = new MyDerivedClass(); myDerivedClass.Foo(12); AssertSame(12, myDerivedClass.Value); AssertSame(22, myDerivedClass.FooInt(22)); myDerivedClass.Foo2(32, true); AssertSame(32, myDerivedClass.Value); }
public void _01() { var derived = new MyDerivedClass(); derived.Print(); // 父类变量指向子类对象, 通过父类变量只能访问父类成员, // 但是如果这个成员在父类是标记为 virtual, 在子类是标记为 override, 则此时访问的是子类成员. MyBaseClass mybc = derived; mybc.Print(); }
/// <summary> Вызов интерфейсного метода с помощью интерфейсной переменной </summary> private static void Example1() { IMyInterface b = new MyBaseClass(); b.Method(); // output: Base class implementation IMyInterface d = new MyDerivedClass(); d.Method(); // output: Base class implementation }
static void Main() { var myDerivedClass = new MyDerivedClass(); myDerivedClass.PublicFromDerived(); myDerivedClass.PublicMethod(); //myDerivedClass.PrivateMethod(); //myDerivedClass.ProtectedMethod(); myDerivedClass.RunProtectedFromBase(); }
public void _01() { var myDerivedClass = new MyDerivedClass(1, 2); Assert.Equal(2, myDerivedClass.A); Assert.Equal(1, myDerivedClass.X); var myDerivedClass02 = new MyDerivedClass(1); Assert.Equal(2, myDerivedClass02.A); Assert.Equal(1, myDerivedClass02.X); }
public static void Main() { MyBaseClass myBase = new MyBaseClass(); MyDerivedClass myDerived = new MyDerivedClass(); object o = myDerived; MyBaseClass b = myDerived; Console.WriteLine("mybase: Type is {0}", myBase.GetType()); Console.WriteLine("myDerived: Type is {0}", myDerived.GetType()); Console.WriteLine("object o = myDerived: Type is {0}", o.GetType()); Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType()); }
public void AccessToParentMember() { const BindingFlags flags = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.SetProperty | BindingFlags.Instance; var prop = typeof(BaseClassOne).GetProperty("Counter", flags); Assert.NotNull(prop); var instance = new MyDerivedClass(-1, "comment", "comment2", 2.5); prop.SetValue(instance, 5, null); Assert.AreEqual(5, instance.Counter); }
public static void Main() { MyClass c = new MyClass(); MyDerivedClass dc = new MyDerivedClass(); //c.x = 1; // error //直接访问保护成员 dc.x = 10; dc.y = 15; Console.WriteLine("x = {0}, y = {1}", dc.x, dc.y); Console.ReadLine(); }
static void Main(string[] args) { //var myBaseClass = new MyBaseClass(); //myBaseClass.DoSomeAction(); var myDerivedClass = new MyDerivedClass(); myDerivedClass.DoSomeAction(); ((MyBaseClass)myDerivedClass).DoSomeAction(); myDerivedClass.DoSomeAction(); }
static void Main(string[] args) { // Multilevel Inheritance MyDerivedClass d = new MyDerivedClass(); // Here compiler get ambiguity to call Add() method,whether it needs to call // from class1 or class2 // So to avaoid this complexity microsoft decided not to support multiple // inheritance in C#. // d.Add(); Console.ReadLine(); }
public void TestOnComplexDerivedClasses() { var mapper = FactoryMapper.DynamicResolutionMapper <MyDerivedClass, DerivedClassLast>(); Assert.NotNull(mapper); Assert.AreEqual(4, mapper.PropertyMappers.Count()); MyDerivedClass source = new MyDerivedClass(1, "mycomment", "mycomment2", 12.5); var res = mapper.Map(source); Assert.NotNull(res); Assert.AreEqual(source.Avarage, res.Avarage); Assert.AreEqual(source.Comment, res.Comment); Assert.AreEqual(source.CommentTwo, res.CommentTwo); Assert.AreEqual(source.Counter, res.Counter); }
public static void Main(String[] args) { Type imyifcType = typeof(IMyIfc); MyClass mc = new MyClass(); Type mcType = mc.GetType(); MyClass mdc = new MyDerivedClass(); Type mdcType = mdc.GetType(); int[] array = new int[10]; Type arrayType = typeof(Array); Console.WriteLine("Is int[] an instance of the Array class? {0}.", System.Convert.ToString(arrayType.IsInstanceOfType(array))); Console.WriteLine("Is myclass an instance of MyClass? {0}.", System.Convert.ToString(mcType.IsInstanceOfType(mc))); Console.WriteLine("Is myderivedclass an instance of MyClass? {0}.", System.Convert.ToString(mcType.IsInstanceOfType(mdc))); Console.WriteLine("Is myclass an instance of IMyIfc? {0}.", System.Convert.ToString(imyifcType.IsInstanceOfType(mc))); Console.WriteLine("Is myderivedclass an instance of IMyIfc? {0}.", System.Convert.ToString(imyifcType.IsInstanceOfType(mdc))); Console.Read(); } //main
static void Main(string[] args) { MyBaseClass myBase = new MyBaseClass(); MyDerivedClass myDerived = new MyDerivedClass(); object o = myDerived; MyBaseClass b = myDerived; Console.WriteLine("mybase: Type is {0}", myBase.GetType()); Console.WriteLine("myDerived: Type is {0}", myDerived.GetType()); Console.WriteLine("object o = myDerived: Type is {0}", o.GetType()); // Class Type不會因此被覆蓋 Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType()); int n1 = 12; int n2 = 82; long n3 = 12; Console.WriteLine("n1 and n2 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n2.GetType())); Console.WriteLine("n1 and n3 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n3.GetType())); Console.ReadLine(); }
static void Main(string[] args) { MyDerivedClass myObj = new MyDerivedClass(42); }
public void _test3a() { var myDerivedClass = new MyDerivedClass(); AssertSame(42, myDerivedClass.ReturnOnly()); }
public void Should_Map_Derived_To_Target() { var derived = new MyDerivedClass { Value = "fortytwo" }; var target = _mapper.Map<MyTargetClassToMap>(derived); target.Value.ShouldBe("fortytwo"); }