Parent class to the transformation of subclasses and instanceof operator
Keynote Teacher: Wang Shaohua QQ Group No.:483773664
Learning Goals:
1) parent class and subclass conversion: down conversion
2) Mastering the use of the instanceof operator
One Problem: Realization of the host and pet play function (i) Demand:
Play the game with the dog , the dog's health value is reduced by 10, and the owner's intimacy increased by 5.
Play a swimming game with penguins, the penguin's health value decreased by 10, with the owner of the increase in intimacy 5
Two Implementation ideas
1) add catchingflydisc method to dog class to realize the function of frisbee;
2) Add Swimming method to penguin class to realize swimming function;
3) Add play (Pet Pet) method to the host, if Pet is playing a Frisbee game on behalf of dog, if pet represents Penguin play swimming game.
4) Create the test class, which creates Master, dog and Penguin objects, call the appropriate method to achieve the host and pet play function.
Three Specific implementation
Follow the steps below to step through the task.
1. Dog
/**
* Realization of the method of receiving Frisbee
*/
public void Catchingflydisc() {
System.out.println ("Dog dog " + this.getname() + "is picking up the frisbee. ");
This.sethealth (this.gethealth()-10);
This.setlove (this.getlove() +5);
}
2. Penguin
/**
* Implement swimming methods
*/
public void swimming () {
System.out.println ("Penguin" + this.getname() + "is swimming. ");
This.sethealth (this.gethealth()-10);
This.setlove (this.getlove() +5);
}
3. Master
Public void Play (Pet Pet) {
//1 determine what pet is Object
//2 turn pet into a specific object because the parent object has no way to invoke the child object's unique method
}
Two Parent class toConversion of subclasses (downward transition)
Assigns a reference to a subclass object to a parent class reference , called a downward transformation, which must be cast at this time.
A Demand
What if you assign a dog object to a pet type reference variable and want to play a Frisbee game with dog?
Two Realize
public class Test {
public static void Main (string[] args) {
Pet Pet = new Dog (" Europe and Europe", " rena");
// force type conversion to dog object
Dog Dog = (dog) pet;
Dog.catchingflydisc ();
}
}
Three Precautions
Forced type conversions cannot be arbitrarily converted,
1) The type of conversion must have an inheritance relationship
2) Convert to the real subclass type pointed to by the parent class
Pet Pet = new Dog (" Europe and Europe", " rena");
Dog Dog = (dog) pet; // correct
Penguin Penguin = (Penguin) pet; // error, exception occurs at run time
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/7E/FB/wKiom1cOX2eSI5CTAAAcZx_sdO4812.jpg " data_ue_src= "E:\My knowledge\temp\f23a9d4c-8b5d-4bd0-b5b8-2cdbe8fd9467.jpg" >
Three instanceofOperator
From the previous example, we know that the type conversion exception occurs when a downward transition is not converted to a real subclass type. So how does it work to avoid this anomaly?
Java provides the instanceof operator for type judgment
A Grammar
Object instanceof class or interface
This operator is used to determine whether an object belongs to a class or implements an interface with the result of true or Fasle
Two Test
Use the instanceof operator to avoid errors that force type conversions
public class Test {
public static void Main (string[] args) {
Pet Pet = new Dog (" Europe and Europe", " rena");
if (pet instanceof Dog) {
Dog Dog = (dog) pet;
Dog.catchingflydisc ();
}else if (pet instanceof Penguin) {
Penguin Penguin = (Penguin) pet;
Penguin. Swimming ();
}
}
Three Precautions
1) when using the instanceof operator, the type of the object must have a direct or indirect inheritance relationship with the class or interface specified by the second parameter of instanceof , or a compilation error will occur. As shown below:
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/7E/F8/wKioL1cOYB3ATGHMAAAJhrfRAI4024.png " data_ue_src= "E:\My knowledge\temp\96d6e44c-e0b7-4e19-90b1-809838fbb3b2.png" >
2) instanceof is commonly used in conjunction with forced type conversions.
Summarize:
Convert down parent class to subclass, combine instanceof operator for coercion type conversion
From for notes (Wiz)
This article is from "Programming with the Old Wangxue" blog, please be sure to keep this source http://imentors.blog.51cto.com/10946447/1763611
Conversion of parent class to subclass and instance of operator