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