Simple examples of C # abstract classes and interfaces

Source: Internet
Author: User

1. Personality is more than common.

2. Different personalities have some of the same behavior.

3. There is a greater difference in the way the same behavior is implemented.

Give you three objects, carp, carp, goldfish, and still let you design the base class to summarize the relationship between them, then you first realize that they are all fish, followed by their way of swimming may be slightly different, then should use the abstract base class instead of the interface, compared with the above example, for three reasons:

 interface iswim
{
void Swim ();
}
Public class Person:iswim
{ public void Swim ()
{ //swimming in person ' s style.
}
}
public class Frog:iswim
{public void Swim ()
{ //swimming in frog ' s style.
}
}
public class Fish:iswim
{public void Swim ()
{
// swimming in fish ' s style.
}
}

1. Generality is greater than individuality 2. Individuals with the same characteristics must have the same attribute and behavior 3. There are certain differences in the way the same behavior is implemented

Abstract  Public classFish {Abstract  Public voidSwim (); }      Public classCrucian Carp: Fish { Public Override voidSwim () {//Swim like a crucian carp }   }       Public classCarp: Fish { Public Override voidSwim () {//Swim like a carp  }    }    Public classGoldfish: Fish { Public Override voidSwim () {//Swim like a goldfish }   }    

The third reason for observing the use of interfaces or the use of abstract base classes is the same, which describes the concept of multi-state in an object-oriented way, which is implemented by overriding the parent class, and invoking the corresponding method at run time based on the object reference being passed. The second reason begins to diverge, and the interface emphasizes the same behavior among the inherited objects, while the abstract class also emphasizes the same attributes among the inherited objects.      and the actual interface and the abstract base class is the reason one, summed up as follows: When the difference between the larger objects to seek functional commonality, the use of interfaces. Use abstract base classes when looking for functional differences among more common objects.

Simple examples of C # abstract classes and interfaces

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.