OOP: inheritance and polymorphism

zhaozj2021-02-12  200

Wednesday, October 10 2001 8:30 PM

In the article "OOP profile: understanding and object", we discussed the benefits of inheritance and polymorphism. We are still roughly learning how to extend the base class definition subclass, inherit the appropriate behavior and attributes in the base class. Obtaining those acts and attributes that are not suitable. This way can reduce the code macro and the incorrect accumulation. Now we will more in-depth inspections and how Java handles it. We will also learn dynamic binding To learn polymorphism. In-depth inheritance Some object-oriented languages ​​provide "multiple inheritance" characteristics, which is valuable when an object needs to inherit behavior and attributes from more than one base class. Multi-inheritance is in some The situation is complicated. For example, suppose we need to define a base class, Animal, then the two subclasses of Animal, Landanimal and Wateranimal. Now we want to define a class to represent the frog. Frog is amphibians, so we will naturally Think of the definition from Landanimal and WateraNimal class. This allows the Frog class to inherit the behavior and attributes you need from Landanimal and WateraNimal class. It seems that this is quite simple; however, let us add a Livingenvironment for Animal Attribute, and use method getLivinGenVironment to return it. We assume that the Landanimal and WateraNimal classes have overloaded this method to implement special features. Slandanimal will return to the value of its LivinGenvironment property, while WateraNimal will return to Water as its LivinGenvironment properties Value. Now, when we use the Frog class as the Landanimal and WateraNimal subclass implementation, we want to get the LivinGenvironment attribute value of Frog, which will encounter a trouble: The getLivinGInvironment method of the Frog class is to return the LAND value or a Water value? The answer depends on how the compiler handles multiple inheritance. I have said in the previous article, Java does not support multiple inheritance. But it does allow an object to have multiple characteristics by using the "interface" function. The following example shows Possible definition code for defining the interface of Landanimal: public interface landanimal {public interface landAns (); public boolean Hasatail ();} A class using the interface adds the IMPLEments interface name of the class definition statement. For example, in Java, we The frog class is defined in the following manner: public class frog extends Animal Implements Landanimal, the WateRanimal interface does not have any actual features; their role is to contact the user and implement this interface object. The interface guarantees the object implementation interface definition Method. Moreover, an object that implements an interface can be forced into an interface type at runtime. For example, use the above Frog definition, and assume that the Landanimal class defines a method called getNumberoflegs and WateraNimal defines a method called HASGILLS. Then an example of a Frog class can be enforced to Landanimal or WateRanimal objects at runtime: frog AFROG = New frog (); int legcount = (LANDANIMAL) AFROG) .GETNumberoflegs (); boolean gillflag = ((Wateranimal) AFROG .hasgills (); Note For why Forg can be forced to convert into a Landanimal object Even if the actual Landanimal object is not created. This makes we call an object when running with any "identity" it has, this is the so-called "Dynamic Binding" or "Running Binding". Deepening polymorphism Java uses dynamic binding to make the polygon possible, it refers to the mechanism of the method or object of Java to select a call at runtime. Overload The special polymorphic mechanism in Java is constituted, which is manifested in two or more methods of a class, but different parameter lists, or "method signature"

The signature of a method refers to the name and number of methods and the number of parameters. Each method of the class has a unique signature associated with it. Class can have multiple names the same method as long as their parameter list is unique For example, we can define two names GetHello to the Animal class, with one of the methods to get the usual call, and use another call when the animal is frightened or smashed. We will give Each method is the only signature: public string getHello (); public string getHello (INT MOOD); now let us modify the example program to put some of the concepts we discussed into practice: public class helloworld {public static void main (String " ] args) {dog animal1 = new DOG (); Cat Animal2 = new cat (); duck arnimal3 = new duck (); system.out.println ("a dog sign" animal1.gethello () ", When Scared Says: " Animal1.Gethello (Animal.Scared) ", IS Carnivors: " Animal1.iscarnivors () ", IS A Mammal: " Animal1.Isamammal ()); System.Out.println (" a cat Says " Animal2.gethello () ", When Comforted Says: " Animal2.gethello (Animal.Comforted) ", IS Carnivors: " Animal2.iscarnivors () ", IS A Mammal: " Animal2.isamamMal ()); System.out.Println ("a Duck Says" Animal3.GetHello () ", When Scared Says:" Animal3.GetHello (Animal.Scared) ", IS Carnivorous:" Animal3.iscarnivors ) ", IS a Mammal:" Animal3.Isamammmmmal ());}} Abstract Class A nimal {public static final int SCARED = 1; public static final int COMFORTED = 2; public boolean isAMammal () {return (true);} public boolean isCarnivorous () {return (true);} abstract public String getHello (); abstract public String getHello (int mood);} interface LandAnimal {public int getNumberOfLegs (); public boolean hasATail ();} interface WaterAnimal {public boolean hasGills (); public boolean laysEggs ();} class Dog extends Animal implements LandAnimal {// Method of heavy duty parent class public string getHello () {Return ("bar");

转载请注明原文地址:https://www.9cbs.com/read-7169.html

New Post(0)