C # Keywords Series 8 interface & amp; virtual & amp; abstr

Source: Internet
Author: User

This article describes how to use interfaces, virtual, abstract, override, and new (modifier. The interface can only contain methods, properties, events, and indexes. The class that implements the interface must implement interface members. It only provides some method Conventions, does not provide subjects, and cannot be modified using public or abstract. See examples: [csharp] interface IAnimal {void Eat (); void Run (); void Play ();} class Dog: IAnimal {public void Eat () {// Eat Console. writeLine ("Your dog is eating");} public void Run () {// Run Console. writeLine ("Your dog is running");} public void Play () {// Play Console. writeLine ("Your dog is playing");} static void Main (string [] args) {// declare an interface instance IAnimal animal = new Dog (); // call animal. ea T () ;}} an interface can be inherited from one or more basic interfaces. Repeat the differences between interface and abstract: 1. interface cannot contain method implementation, abstract class can contain or not. 2. an interface can only inherit interfaces. abstract class can inherit class or one or more interfaces. the interface cannot contain fields and attributes. abstract class can contain fields. the interface cannot contain constructor and destructor. abstract class can both be 5. the interface supports multi-inheritance. abstract class does not support multi-inheritance. abstract keywords are used to modify methods, classes, attributes, indexes, and event declarations, the member of the abstract class must be implemented through the derived class of the abstract class. The abstract is not mentioned here. Give an address: http://blog.csdn.net/ywy0012011/article/details/8167161. Virtual keywords are used to modify methods, attributes, indexes, and event declarations so that they can be overwritten in subclasses. It is incorrect to use virtual for static attributes. By using the override attribute declaration, You can override the virtual inheritance attribute in the derived class. Override provides member implementations inherited from the base class. You cannot override non-virtual or static methods. The override base class methods must be virtual, abstract, or override-modified. Override declares that the accessibility of the virtual method cannot be changed. The override and virtual methods must have the same access level modifier. The new Keyword can show hidden members inherited from the base class. A general method. If it is rewritten in a derived class, the new keyword must be used. The difference between new and override, that is, the difference between rewriting and hiding: new usage: [csharp] class Circle: Shape {// hide the field 'X' new public static int x = 100; static void Main () {// display the new value of x Console. writeLine (x); // display the hidden value of x Console. writeLine (Shape. x); // display non-Hidden values y Console. writeLine (y) ;}}/* Output: 100 22 55 */override usage [csharp] abstract class ShapesClass {abstract public int Area ();} class Square: shapesClass {int side = 0; public Square (int n) {side = n;} public override int Area () {return side * side;} static void Main () {www.2cto.com Square sq = new Square (12 ); console. writeLine ("Area of the square = {0}", sq. area () ;}}/* Output: Area of the square = 144 */difference between abstract and virtual: 1: Abstract methods can only be declared in abstract classes, but virtual methods are not. Abstract methods must be rewritten in a derived class, while virtual methods do not (either override or override) 2: Abstract METHODS cannot declare method entities, abstract public void SD (); the virtual method can be 3. abstract: new and override are required for virtual implementation, while interfaces do not.

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.