. NET re-think (ii) interfaces and the trade-offs of abstract classes

Source: Internet
Author: User

It must be said that interfaces and abstract classes do not seem to be able to instantiate both, and the parts that are not implemented are implemented by derived classes.

They have a major difference:

(1) Derived classes of abstract classes can be derived classes, in other words, abstract members are not necessarily fully implemented in derived classes, and interfaces require their derived classes or structs to fully implement their members.

(2) A class can inherit only one abstract class, but it is possible to implement multiple interfaces.

(3) A class can contain a field or something, but the interface contains only the members that are not implemented, and does not contain fields.

(4) Abstract classes can only derive classes, but interfaces can derive classes and structs.

As I understand it, a derived class is a group of similar classes that are put together to extract their common members into a base class for code reuse. Sort of like a resource in WINRT (Resource) or a CSS in a Web page. And the interface is a collection of functions, I put some of the functions of the class exposed to you, you use good, do not care about how he actually realized, put together, simple and good-looking. Specific, but also to see ... Experience

Interface's example is written in the previous article, so write an example of an abstract class ~

classProgram {Static voidMain (string[] args) {Mengnan a=NewMengnan (); Console.WriteLine ("the Msg field inherited by the Mengnan instance is: {0}", a.msg); Console.WriteLine ("The Mengnan instances are implemented as follows:", A.getname ()); Console.WriteLine ("the properties that are implemented by the Mengnan instance are:", A.ismale); }    }    Abstract classPerson {Internal stringmsg ="This is the person class"; Abstract  Public stringgetname (); Abstract  Public BOOLIsmale {Get; }    }    classMengnan:person { Public Override BOOLIsmale {Get            {                return true; }        }         Public Override stringgetname () {return "I'm the MSP's dream, Sister Nan."; }    }

. NET re-think (ii) interfaces and the trade-offs of abstract classes

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.