The use of interfaces and methods in C # (fourteenth day)

Source: Internet
Author: User

Since Friday I had some things not enough time to summarize the knowledge of the day, so today summarizes the Friday in the cloud and the college learned about commissioned knowledge.

Theory:

Interface:

• Definition of the interface: interface keyword, the interface can have properties, methods (not implemented)• The name of the interface usually begins with "I", such as IList• If a class inherits the class and implements the interface, then the class must be written in front. A class can inherit only one parent class, but it is possible to implement multiple interfaces. class Student:person, IComparable (this is a defined interface) • A complete abstraction of a convention • Interfaces are used to achieve• Syntax:

[Access modifier] Interface interface Name

{

Interface member Definitions

}

Interface Iffly

{

void IFly ();

}

• Interface only declarations of methods, properties, indexes, and events, all members default to public what are the similarities and differences between interfaces and classes in C #

Different points:

• Interfaces cannot be instantiated directly.

• The interface does not contain the implementation of the method. • Interfaces can inherit multiple, and classes can only be inherited. • Class definitions can be split between different source files. • Same point:• Interfaces, classes, and structs can inherit from multiple interfaces. • An interface is similar to an abstract base class: Any non-abstract type that inherits an interface must implement all members of the interface. • Interfaces and classes can contain events, indexers, methods, and properties. • Unsupported between classes, support between interfaces. A class-to-interface is called an implementation, not an inheritance. The class is the father, the interface is capable, can have multiple abilities, but cannot have multiple fathers。 class and member modifiers• Class–public, internal, abstract, sealed, static• Member (method)–public, private, protected, internal–protected Internal–abstract, virtual, new, override–static static Methods and instance methods (definition and invocation) Real:

Rubber rubber duck, wood duck, real duck duck. Three ducks can swim, and the rubber ducks and the real ducks will be called, but the call is not the same, rubber duck "haw" called, The Real Duck "quack" called, wooden ducks will not be called. Interface implementation.

First define a parent class, Duck,

Defines the interface and three subclasses, respectively: Rubber,wood,real.

Used in the main function

Application of the method:

AnimalsAnimalAll have to eatEatand calledBarkThe method that the dogDogand catsCatThe method of barking is different.. There is no default implementation in the parent class. Consider using an abstract method.

According to test instructions, define the parent class, and two subclasses

Defining subclasses

Used in the main function

Well, today summed up here, tomorrow will start a new week, come on!

The use of interfaces and methods in C # (fourteenth day)

Related Article

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.