Basic Object-Oriented Knowledge-encapsulation, inheritance, polymorphism, and basic knowledge Polymorphism

Source: Internet
Author: User

Basic Object-Oriented Knowledge-encapsulation, inheritance, polymorphism, and basic knowledge Polymorphism

Object-oriented principles:

Multiple combinations and less inheritance; low coupling and High Cohesion

Inheritance focuses more on commonality; polymorphism focuses on Difference

Polymorphism

Through inheritance, a class can be used as multiple types: it can be used as its own type, any base type, or any interface type when implementing an interface. This is known as polymorphism.Every type in C # Is polymorphism.. Types can be used as their own types or as Object instances, becauseObjectAs the base type.

1 public class animal 2 {3 4 public string SayName () 5 {6 return "hello im animal"; 7 8} 9 public string Eat () 10 {11 12 return "this is animal's eat"; 13} 14 public virtual string Call () 15 {16 17 return "this is animal's speak "; 18} 19} 20 public class Bird: animal21 {22 public new string Eat () // Replace the base MEMBER 23 {24 return "this is bird's Eat"; 25} 26 public override string Call () with the new derived Member () // rewrite the virtual Method 27 {28 return "this is Bird's Call"; 29} 30 31}

 

 

1/* inherit the eat and call functions of the base class */2/* 3 * result: 4 hello im animal 5 this is bird's Eat 6 this is Bird's Call 7 */8 Bird bb = new Bird (); 9 Response. write (bb. sayName () + "</br>"); 10 Response. write (bb. eat () + "</br>"); 11 Response. write (bb. call () + "</br>"); 12 13/* 14 * result: 15 hello im animal16 this is animal's eat17 this is Bird's Call18 */19 animal AB = new Bird (); 20 Response. write (AB. sayName () + "</br>"); 21 Response. write (AB. eat () + "</br>"); 22 Response. write (AB. call () + "</br> ");

New modifier: when used as a modifier, The new Keyword can explicitly hide members inherited from the base class.

When a derived class overrides a virtual member, the Member is called even if the instance of the derived class is accessed as a base class instance.


 

 

Encapsulation

Encapsulation Principle 1: Define a field as private

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.