. NET Fundamentals Review (III)

Source: Internet
Author: User

A. Richter Replacement principle: LSP

Definition: Subclasses can replace the location of the parent class, and the function of the program is not affected (the parent class variable points to the subclass object). Because there are functional subclasses of the parent class, they do not affect the functionality of the program.

Examples of programs:

 1  person p = new   person ();  2  p.sayhi (); //  call the parent class  3  Student s = new   Student ();  4  s.sayhi (); //  call the subclass  5   Richter Replacement principle  6  person p1 = new   Student ();  7  P1. Sayhi (); //  Call the parent class's  

two. Type Conversions

1. bool B = p is person; determines the type of class that the variable refers to.

2. As conversion

Person p = new person ();

Student S1 = (Student) p;//may report an exception

If the conversion succeeds, the reference points to S2, and if the conversion fails, returns null

Student s2 = p as Student;

Three Virtual method, polymorphic

1. Virtual method: Allows subclasses to override methods of the parent class.

Examples of programs:

1 //Person.cs2 class Person3     {4          Public Virtual voidSayhi ()5         {6Console.WriteLine ("I'm a human being. ");7         }8 }9 Ten //Chinese.cs One classChinese:person A     { -          Public Override voidSayhi () -         { theConsole.WriteLine ("I am a Chinese. "); -         } - } -  + //GuangDongRen.cs - classGuangdongren:chinese +     { A          Public Override voidSayhi () at         { -Console.WriteLine ("I am a Cantonese person. "); -         } -     } -  - //Main in Static voidMain (string[] args) -         { toPerson p =NewGuangdongren (); + P.sayhi (); - //export of "Guangdong people" the             //if the Guangdongren class does not override the Sayhi () method, the output "Chinese" *  $ Console.readkey ();Panax Notoginseng}

2. Polymorphism, the same behavior, for different things, there are different manifestations.

Examples of programs:

1 //Person.cs2  Public Virtual voidwashbody ()3         {4Console.WriteLine ("people in the shower");5         }6 7 //Boy.cs8 classBoy:person9     {Ten          Public Override voidwashbody () One         { AConsole.WriteLine ("Boy in the shower"); -         } -     } the  - //Girl.cs - classGirl:person -     { +          Public Override voidwashbody () -         { +Console.WriteLine ("Girl in the shower"); A         } at } -  - //WashMachine.cs - classWashmachine -     { -          Public voidWash (person p) in         { -Console.WriteLine ("Start Bathing"); to p.washbody (); +Console.WriteLine ("Bath End"); -         } the     } *  $ //MainPanax Notoginseng Static voidMain (string[] args) -         { theBoy P1 =NewBoy (); +Girl P2 =NewGirl (); AWashmachine WM =Newwashmachine (); theWm. Wash (p1);//Boy Bathing +Wm. Wash (p2);//Girl Bathing -Console.readkey ();

3. Use the parent class type as the return value of the method. (What is the principle?) )

Examples of programs:

1 //Pet.cs2 classPet3 {4      Public Virtual voidShout ()5     {6Console.WriteLine ("Pets are called");7     }8 }9 Ten //Cat.cs One classCat:pet A { -      Public Override voidShout () -     { theConsole.WriteLine ("The cat called"); -     } - } -  + //Dog.cs - classDog:pet + { A      Public Override voidShout () at     { -Console.WriteLine ("Dog Barking"); -     } - } -  - //Shop.cs in class Shop - { to      PublicPet Sellpet (stringtype) +     { -         Switch(type) the         { *              Case "Dog": $                 return NewDog ();Panax Notoginseng              Case "Cat" -                 return NewCat (); the             default: +                 return NULL; A         } the     } + } -  $ //Main $ Static voidMain (string[] args) - { -Shop shop =NewShop (); thePet pet = shop. Sellpet ("Dog"); -Pet. Shout ();//output "Dog bark"Wuyi}

. NET Fundamentals Review (III)

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.