Iii. comparison of abstract classes and interfaces-----"Big talk design mode"

Source: Internet
Author: User

First, abstract class

C # allows classes and methods to be declared abstract, that is, abstraction classes and abstract methods. Abstract classes cannot be instantiated, and abstract methods must be overridden by subclasses, and if the class contains abstract methods, the class must be defined as an abstract class, whether or not it contains other general methods.

Abstract classes have as much common code as possible as little data.

Abstract class The Animal                            // plus abstract keyword indicates that it is an abstract class {    ... protected Abstract string getshoutsound ();   // adding an abstract before the method return value indicates that this method is an abstraction.    ...                                                 // Abstract method has no method entity, directly after parentheses with a semicolon   }

Second, the interface

An interface is a collection of implicit public methods and properties that encapsulate a particular function, and once the class implements an interface, the class can support all the attributes and members specified by the interface, and the name of the interface is preceded by an uppercase letter "I".

The declaration interface is syntactically identical to declaring an abstract class, but does not allow the execution of any member of the interface, and the class implementing the interface must implement all the methods and properties in the interface.

Interfaces cannot be instantiated, cannot have constructor methods and fields, cannot have modifiers, and cannot declare virtual or static.

Interface Ichange                                    // declares an interface that has a method {      string changeting (string thing);              // parameter is a string variable that returns a string }
classMachinecat:cat,ichange//you can inherit the parent class, or you can inherit the interface at the same time{   PublicMachinecat ():Base()//constructors that inherit from the parent class  {  }   Public stringChangething (stringThing//method that implements the interface, cannot add override modifier  {    return Base. Shout () +"I have the almighty pocket, I variable out"+ thing;//methods to call the parent class cat  }}
Private void button6_click (object  sender, EventArgs e)         // window call        
{   new machinecat (" Ding cable ");                   // instantiation of Doraemon Cat class New Ichange (MCAT);                               // assigning a class instance to an interface     MessageBox.Show (array. Changething (" all kinds of things! "));      // Implementing interface Methods }

Iii. similarities and differences of abstract classes and interfaces

Abstract class

Interface

You can give implementations of some members that can contain general methods Cannot contain the implementation of a member, it must be implemented by the implementation class
Abstract members can be partially implemented by the quilt class Interface members need to implement class full implementation
A class can inherit only one abstract class A class can implement multiple interfaces
Abstraction of a class An abstraction of behavior
Design angle: Discover common things from subclasses, generalize out parent class, and subclass inherit parent class Design angle: There is no knowledge of the existence of the subclass, how the implementation of the method is not confirmed, pre-defined
Apply: For some similar class objects, use the Inheritance abstract class Applicable: If the behavior spans objects of different classes, the interface can be used

Iii. comparison of abstract classes and interfaces-----"Big talk design mode"

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.