. NET re-think (a) sealed and interface

Source: Internet
Author: User
Tags instance method

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.