using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { interface IDrivingLicenseB { void GetLicense(); } interface IDrivingLicenseA : IDrivingLicenseB { new void GetLicense(); } class Teacher : IDrivingLicenseA { public void GetLicense() { Console.WriteLine("Teacher got license A."); } } class Student : IDrivingLicenseB { public void GetLicense() { Console.WriteLine("Student got license B."); } } class Program { static void DriveCar(string name, IDrivingLicenseB o) { IDrivingLicenseB dl = o as IDrivingLicenseB; if (dl != null) { Console.WriteLine("drive car."); } else { Console.WriteLine("cannot drive car."); } } static void DriveBus(string name, IDrivingLicenseB o) { IDrivingLicenseA dl = o as IDrivingLicenseA; if (dl != null) { Console.WriteLine("drive bus."); } else { Console.WriteLine("cannot drive bus."); } } static void Main(string[] args) { Teacher t = new Teacher(); DriveCar("Teacher", t); DriveBus("Teacher", t); Student s = new Student(); DriveCar("Student", s); DriveBus("Student", s); Console.ReadKey(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { interface IDrivingLicenseB { void GetLicense(); } interface IDrivingLicenseA : IDrivingLicenseB { new void GetLicense(); } class Teacher : IDrivingLicenseA { void IDrivingLicenseA.GetLicense() { Console.WriteLine("Teacher got license A."); } void IDrivingLicenseB.GetLicense() { Console.WriteLine("Teacher got license B."); } public void GetLicense() { Console.WriteLine("VVVB."); } } class Student : IDrivingLicenseB { public void GetLicense() { Console.WriteLine("Student got license B."); } } class Program { static void DriveCar(string name, IDrivingLicenseB o) { IDrivingLicenseB dl = o as IDrivingLicenseB; if (dl != null) { Console.WriteLine("drive car."); } else { Console.WriteLine("cannot drive car."); } } static void DriveBus(string name, IDrivingLicenseB o) { IDrivingLicenseA dl = o as IDrivingLicenseA; if (dl != null) { Console.WriteLine("drive bus."); } else { Console.WriteLine("cannot drive bus."); } } static void Main(string[] args) { Teacher t = new Teacher(); ((IDrivingLicenseA)t).GetLicense(); ((IDrivingLicenseB)t).GetLicense(); t.GetLicense(); //DriveCar("Teacher", t); //DriveBus("Teacher", t); //Student s = new Student(); //DriveCar("Student", s); //DriveBus("Student", s); Console.ReadKey(); } } }
徹底限定名不能用public修飾符修飾。去掉public,即爲private。ide
接口的多繼承:spa
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { interface IA { void F(); } interface IB : IA { new void F(); } interface IC : IA { void G(); } interface IBC : IB, IC { } class D: IBC { public void F() { Console.WriteLine("IB.F()"); } public void G() { Console.WriteLine("IC.G()"); } } class Program { static void Main(string[] args) { D d = new D(); ((IBC)d).F(); ((IB)d).F(); ((IC)d).F(); ((IA)d).F(); Console.ReadKey(); } } }
Note:
調用IC的F方法,IC的F方法繼承自IA接口。接口多重繼承原則:直觀隱藏規則。若是成員在任何一個路徑中被隱藏,則他在任何一個路徑中也被隱藏掉。3d
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { interface IA { void F(); } class B: IA { void IA.F() { Console.WriteLine("B.F"); } } class C : B, IA { public void F() { Console.WriteLine("C.F"); } } class Program { static void Main(string[] args) { C c = new C(); ((IA)c).F(); //((B)c).F(); Console.ReadKey(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication1 { interface IA { void F(); } class B: IA { void IA.F() { Console.WriteLine("B.F"); } } class C : B, IA { public void F() { Console.WriteLine("C.F"); } } class Program { static void Main(string[] args) { C c = new C(); ((IA)c).F(); B b = c; ((IA)b).F(); Console.ReadKey(); } } }