Cat and mouse-Java object-oriented features
Let's first design a mouse-catching mini game: Both cats and mice have two attributes: Name and weight. Cats have Mouse-catching methods, and corresponding mice have escape methods. ⊙ First, the result of the game is that the cat caught the mouse or the mouse ran away. For these two cases, we use weight to distinguish them. If the weight of the cat is greater than or equal to the weight of the mouse, then the cat can grasp the mouse. If the weight of the cat is smaller than the weight of the mouse, the mouse can escape. For analysis, the first step is to create two classes: Cat and Mouse, and declare their respective methods:
// Cat. javapackage Testprogramming // The packaging statement. Save the Cat class in the TestProgramming package and then explain its function. Public class Cat {public String name; public int weight; Cat () {// Note: to avoid errors when creating Cat class instantiation objects, we 'd better write an empty constructor // Cat cat = new Cat () if no such object exists (); the statement returns an error! For Cat attributes, you can use the get/set method to obtain} Cat (String name, int weight) {this. name = name; this. weight = weight;} // Add the corresponding get/set Method public String getName () {return name;} public void setName (String name) {this. name} public int getWeight () {return weight;} publuc void setWeight (int weight) {this. weight;} // override the toString method of the Object class to get the Cat's Information public String toString () {return "Cat [name =" + name + ", weight = "+ weight +"] ";}// Finally, declare the method by which the cat catches the mouse, because the cat catches the mouse, so the return value type should be a Mouse class public Mouse catchMouse (Mouse mouse) {return mouse }}
// The composition of the Mouse class is basically the same as that of the Cat class // Mouse. javapackage Testprogrammingpublic class Mouse {public String name; public int weight; Mouse () {} Mouse (String name, int weight) {this. name = name; this. weight = weight;} public String getName () {return name;} public void setName (String name) {this. name} public int getWeight () {return weight;} publuc void setWeight (int weight) {this. weight;} public String toString () {return "Mouse [name =" + name + ", weight =" + weight + "]"} public Cat escapeCat (Cat cat) {return cat;} // if the weight of a cat is smaller than that of a mouse, the mouse can run away in the public static void inform (boolean B) {if (B = false) method) {System. out. println ("The Cat grabbed the mouse");} else {System. out. println ("when a cat catches a mouse, the mouse is overweight, and the cat loses the game ");}}
Let's write a test class and instantiate the Cat and Mouse classes.
// Test_Cat_Mouse.javapackage Testprogrammingpublic class Test_Cat_Mouse {public static void main (String [] args) {String string1, string2; boolean; // first instantiate a Cat, Mouse Cat catTom = new Cat ("Tom", 50); Mouse mouseLisa = new Mouse ("Lisa", 40); if (! (Bool = mouseLisa. getWeight () = catTom. getWeight () & (bool = mouseLisa. getWeight () <= catTom. getWeight () {catTom. catchMouse (mouseLisa); System. out. println ("cat" + catTom + "grabbed" + mouseLisa + "Mouse");} else {mouseLisa. escapeCat (catTom); System. out. println ("rat" + mouseLisa + "escaped" + "cat" + mouseLisa + "capture");} string1 = catTom. toString (); string2 = mouseLisa. toString ();}}
The above is the implementation of our mouse games. We can review that Java object-oriented programming has three features: Eccapsulation and Inheritance), polymorphism (Ploymorphism); encapsulation: the class user does not need to know how the class is implemented, but the implementation process is encapsulated, users only need to know how to use the following two types of <1> package PackageName <2> access permissions: public/default/protected/private inheritance: extended attributes and methods on the basis of existing classes implement polymorphism with the keyword extends: After different objects face the same behavior in different forms and understand the characteristics of encapsulation, we can analyze the previous example, such as encapsulation: We can store Cat, Mouse, and test classes in the same package TestProgramming ), in this case, the default and protected permissions are equivalent to the maximum public permissions, but for the minimum Permissions Private. The modified members can only be accessed in this class. For inheritance, let's take a general look, because Cat and Mouse have the same attributes name and weight, and cats and mice are animals, that is, they are both animals, it also has the same attributes. Now we can create an Animal-class Animal and define the same attributes of Cat and Mouse in the Animal class: name and weight; when Cat and Mouse inherit the Animal class, they also have the name and weight attributes. This is an improvement and optimization. The Code is as follows:
Public class Animal {public String name; public int weight; Animal () {// we will discuss several possible conditions when the super keyword in the class appears at once} Animal (String name, int weight) {this. name = name; this. weight = weight;} // you can write it here, because the purpose of creating an Animal class is to get its two attributes;} // The modified Cat class code is as follows, the Mouse class is basically the same, so write Cat for reference | public class Cat extends Animal {Cat () {| Cat () {|}|} Cat (String name, int weight) {| Cat (String name, int weight) {super (); // call the constructor without parameters for the parent class | super ("catTom", 50); // call the constructor with parameters for the parent class this. name = name; |} this. weight = weight; |}|}|}| // Both of the preceding two methods can be implemented. The usage rules of this and super must be clearly distinguished, the subsequent sections will detail the usage of this and super, as well as the method rewriting and overloading. These two aspects will be detailed in the next blog; next we will look at the polymorphism part for Polymorphism: We will still explain in this example
// Re-write a test class // Test_Cat_Mouse.javapackage Testprogrammingpublic class Test_Cat_Mouse {public static void main (String [] args) {String string1, string2, string3, string4; boolean; // keep the previous instance to distinguish Cat catTom1 = new Cat ("Tom", 50); Mouse mouseLisa1 = new Mouse ("Lisa", 40 ); // use the parent class handle to create a subclass object Animal catTom2 = new Cat ("Tom", 50); // transform upwards, that is, the subclass object is converted into the parent class object Animal mouseLisa2 = new Mouse ("Lisa", 40); // It is transformed up, that is, the subclass object is converted into the parent class object if (! (Bool = mouseLisa. getWeight () = catTom. getWeight () & (bool = mouseLisa. getWeight () <= catTom. getWeight () {catTom1.catchMouse (mouseLisa); System. out. println ("cat" + catTom + "grabbed" + mouseLisa + "Mouse");} else {mouseLisa1.escapeCat (catTom); System. out. println ("rat" + mouseLisa + "escaped" + "cat" + mouseLisa + "capture");} // if (! (Bool = mouseLisa. getWeight () <= catTom. getWeight () {catTom2.catchMouse (mouseLisa); // error System. out. println ("cat" + catTom + "grabbed" + mouseLisa + "Mouse");} else {mouseLisa2.escapeCat (catTom); // error System. out. println ("rat" + mouseLisa + "escaped" + "cat" + mouseLisa + "capture");} string1 = catTom. toString (); string2 = mouseLisa. toString (); string3 = catTom. toString () string4 = mouseLisa. toString ()}}
At this time, we will find that the Cat and Mouse objects created by the Animal handle cannot access their own methods, and the reason is that this object belongs to the parent class Animal, it cannot be referenced because it is no longer created by subclass Cat or Mouse. To solve this problem, we need to perform a downward transformation to convert the parent class object into a subclass object. The Code is as follows: animal animal = new Cat ("catTom", 50); Cat catTom = (Cat) animal; // at this time, catTom can call the catchMouse () method of the Cat class. A summary is given: the three characteristics of object-oriented encapsulation, inheritance, and polymorphism are embodied in the programming process. This is the basis and details. You need to carefully understand the differences and relationships between the three.