Bo Master these days just idle, smashing Foundation, after all, self-study, foundation is not reliable very deadly, to be practical AH ~ ~ ~
The 1.sealed keyword modifies a class and can also decorate an implementation method or property. When sealed is used to decorate a class, the class cannot be inherited, so this class is also called a sealed class. Corresponding, the class that is modified by abstract is called an abstract class, and the class must inherit to make sense.
classProgram {Static voidMain (string[] args) { } } classProduct {Private stringname; Public Virtual voiddisplay () {Console.WriteLine ("This is the product class \ n"); } } Sealed classcomputer:product { Publiccomputer () {}}/// <summary> ///will give an error, because the inherited computer is sealed class/// </summary> //class Surface:computer//{ //} classphone:product { PublicPhone () {} Public Sealed Override voiddisplay () {Console.WriteLine ("This is the phone class display, sealing Method! "); } } classWp:phone {/// <summary> ///An error will be given because the display in the inherited phone class is a sealed method and cannot be overridden/// </summary> //Public sealed override void display ()//{ //}}
Sealed modifies an instance method or property, which should be used with override.
The 2.interface interface is used to display and use the functionality of the user without having to know the implementation. When the function module needs to change, the program dog only need to change the implementation code of the function module, other calls to this interface program will not need to change. This application mode of the interface is called Bridge mode, and bridge mode separates the intention and implementation to get better extensibility.
classProgram {Static voidMain (string[] args) {Computer A=NewPC (); Console.WriteLine ("The CPU for this computer is: {0}", A.getcpu ()); Console.WriteLine ("The video card for this computer is: {0}", A.videocard); //error because Videocard is read-only in the declaration of the Computer interface (no set accessor)//A.videocard = Console.ReadLine ();PC b=NewPC (); //Instance B can write to the Videocard propertyB.videocard =Console.ReadLine (); } } InterfaceComputer {stringgetcpu (); stringVideocard {Get; } } classPc:computer {Private string_VC ="Nvidia"; Public stringVideocard {Get { return_VC; } Set{_VC=value; } } Public stringgetcpu () {return "Intel"; } }
And, to add, an interface can inherit multiple interfaces, but a class can inherit only one. The access permission for an interface is public, the members of the class or struct implementation interface must remain public, and the signature of the implementing method must be consistent with the interface method signature.
. NET re-think (a) sealed and interface