Application of the Richter conversion in C # and forced type conversion in Java in polymorphism

Source: Internet
Author: User

In C #:

Note: The subclass does not inherit the constructor of the parent class, but instead defaults to the parameterless constructor of the parent class. If a subclass inherits a parent class, the subclass can use members that inherit from the parent class in addition to its own members. However, the parent class can always use only its own members, not the members of the child classes. Sub-classes cannot use each other's members. The concept of the Richter conversion:1), subclasses can be assigned to the parent class2if the parent class is loaded with a subclass object, it can be said that the parent class is strongly converted to a child class object. namespaceRichter Conversion _ interface Exercise {classProgram {Static voidMain (string[] args) {            //Test 1//Ifavoratefood iff = new Cat ();//subclasses can assign values to parent classes//iff.            Food (); //Ivoice IV = new Cat (); //Iv.. Voice ()//Test 2//ivoice pvoice = new Cat (); //Pvoice.voice ();//only methods in Ivoice are called//Cat cat = Pvoice as Cat;//if the parent class has child class objects, you can strongly convert the parent class to a child class object//Cat.            Food (); //Test 3Ivoice Pvoice =NewCat (); Ifavoratefood Pfavoratefood= Pvoice asIfavoratefood;            Pfavoratefood.food ();        Console.ReadLine (); }    }} Public classCat:ifavoratefood, ivoice{ Public voidFood () {Console.WriteLine ("The thing I like is a rat."); }     Public voidVoice () {Console.WriteLine (" Meow Meow"); }}Interfaceifavoratefood{voidFood ();}Interfaceivoice{voidVoice ();}

The application of forced type conversion in polymorphism in Java:

//Animal ClassAbstract classanimal{String name;  PublicAnimal (String name) { This. Name =name; }     Public Abstract voidrun ();}//MouseclassMouseextendsanimal{ PublicMouse (String name) {Super(name); }         Public  voidrun () {System.out.println (name+ "Four legs walk slowly!"); }    //Mouse-specific methods---hole-punching     Public voiddig () {System.out.println ("The mouse is punching the hole."); }}//FishclassFishextendsanimal{ PublicFish (String name) {Super(name); }     Public  voidrun () {System.out.println (name+ "shake tail, swim, swim!"); }    //Blowing Bubbles     Public voidBubble () {System.out.println (name+ "Blowing bubbles ...!"); }}classDemo2 { Public Static voidMain (string[] args) {/*Animal a = new Mouse ("Mouse");  Polymorphic//Call subclass-specific methods Mouse m = (Mouse) A;        Coercion type conversion m.dig (); */Mouse M=NewMouse ("Mickey Mouse"); Fish F=NewFish ("Grass carp");    Print (f); }    //requirements: Define a function to receive any type of animal object, within the function to invoke the animal-specific method.      Public Static voidPrint (Animal a) {//Animal a = new Mouse ("Mickey Mouse");        if(AinstanceofFish) {Fish F=(Fish) A;        F.bubble (); }Else if(AinstanceofMouse) {Mouse m=(Mouse) A;        M.dig (); }    }}

Application of the Richter conversion in C # and forced type conversion in Java in polymorphism

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.