static void Main(string[] args) { DerivedClass instance = new DerivedClass(); instance.Method(); instance.Method1(); instance.Method2(); Console.WriteLine(new string('-', 55)); BaseClass instance0 = instance as BaseClass; //видно только методы класса BaseClass instance0.Method(); IInterface1 instance1 = instance as IInterface1; //видно только методы интерфейса IInterface1 instance1.Method1(); IInterface2 instance2 = instance as IInterface2; //видно только методы интерфейса IInterface2 instance2.Method2(); //Delay Console.ReadKey(); }
static void Main(string[] args) { DerivedClass instance = new DerivedClass(); instance.Method(); instance.Method1(); instance.Method2(); Console.WriteLine(new string('-', 30)); BaseClass instanse1 = instance as BaseClass; instanse1.Method(); //Другие два метода недоступны IInterface1 instance2 = instance as IInterface1; instance2.Method1(); //Другие два метода недоступны IInterface2 instance3 = instance2 as IInterface2; instance3.Method2(); //Другие два метода недоступны IInterface2 instance4 = instanse1 as IInterface2; instance4.Method2(); Console.WriteLine(new string('-', 30)); BaseClass inst = new BaseClass(); IInterface2 inst1 = inst as IInterface2; //А вот это недопустимо, потому что даункаст без апкаста до этого? inst1.Method2(); }
static void Main(string[] args) { ConcreteClass instance = new ConcreteClass(); instance.Method1(); instance.Method2(); IInterface1 instance1 = instance as IInterface1; instance1.Method1(); IInterface2 instance2 = instance as IInterface2; instance2.Method1(); instance2.Method2(); // Delay Console.ReadKey(); }
static void Main() { ConcreteClass instance = new ConcreteClass(); instance.Method1(); instance.Method2(); IInterface1 instance1 = instance as IInterface1; instance1.Method1(); IInterface2 instance2 = instance as IInterface2; instance2.Method1(); instance2.Method2(); // Delay. Console.ReadKey(); }
static void Main(string[] args) { ConcreteClass instance = new ConcreteClass(); instance.Method1(); instance.Method2(); IInterface1 instance1 = instance as IInterface1; instance1.Method1(); IInterface2 instance2 = instance as IInterface2; //тут у нас есть доступ и к Method1 и к Method2 instance2.Method1(); // т.к IInterface2 наследуется от IInterface1 instance2.Method2(); //Delay Console.ReadKey(); }
static void Main(string[] args) { ConcreteClass instance = new ConcreteClass(); instance.Method1(); instance.Method2(); IInterface1 instance2 = instance as IInterface1; instance2.Method1(); IInterface2 instance3 = instance2 as IInterface2; instance3.Method1(); instance3.Method2(); Console.WriteLine(new string('-', 30)); ConcreteClass concreteClass = new ConcreteClass(); IInterface2 interface2 = concreteClass as IInterface2; interface2.Method1(); interface2.Method2(); }
public int Sideeffect() { _interface1.Method1(); return(1); }
public virtual string Method1UsingClass1Method1() { return("Class2.Method1" + _class1.Method1()); }