Object Transition in Java
Example 1: Define the concepts of reference type and object type
First, the concepts of reference type and object type are defined.
In this example, there is an object new ADHero(), and there is also a reference ad.
Objects are typed, ADHero
There are also types of references, ADHero
Usually, the reference type is the same as the object type.
The next issue to discuss is type conversion, which refers to the conversion when the reference type and the object type are inconsistent.
package charactor; public class Hero { public String name; protected float hp; public static void main(String[] args) { ADHero ad = new ADHero(); } }
Example 2: Subclass to parent (upward transition)
The so-called transformation refers to the need for type conversion when the reference type and the object type are inconsistent.
Type conversions sometimes succeed and sometimes fail (refer to basic types of type conversions)
Can it be translated into success? Teach you a very simple way to distinguish
Use the right side as the left side. See if it makes sense.
Hero h = new Hero(); ADHero ad = new ADHero(); h = ad;
The type of object that the ad reference on the right refers to is a physical attack hero
The type quoted by h on the left is common hero
Does it make sense to regard physical attack heroes as ordinary heroes? If you make sense, you can turn it around.
It makes sense for all subclasses to be converted to parent classes. Examples around you
Apple Mobile inherits the mobile phone and uses it as an ordinary mobile phone.
Yibao Pure Water inherits drinks and uses Yibao Pure Water as drinks.
package charactor; public class Hero { public String name; protected float hp; public static void main(String[] args) { Hero h = new Hero(); ADHero ad = new ADHero(); //Type conversion refers to the conversion of the type of object to which one reference refers to to to the type of another reference. //The type of object to which ad refers is ADHero //The type that h refers to is Hero //If ADHero is used as Hero, it must be possible. h = ad; } }
Example 3: Parent Rotor Class (Downward Transition)
The parent rotor class, sometimes line, sometimes not line, so it must be mandatory conversion.
Mandatory conversion means that the conversion is risky and the risk is borne by itself.
When will it be OK?
1. Hero h =new Hero(); 2. ADHero ad = new ADHero(); 3. h = ad; 4. ad = (ADHero) h;
Line 3 is a subclass to a parent class. It must be possible.
Line 4 is the parent rotator class, so it needs to be forced.
The reference to h refers to ADHero, so line 4 converts ADHero to ADHero, and it succeeds.
When can't we switch?
1. Hero h =new Hero(); 2. ADHero ad = new ADHero(); 3. Support s =new Support(); 4. h = s; 5. ad = (ADHero)h;
Line 4, which is a subclass to a parent class, can be converted successfully
Line 5 converts the object Support pointed to by the h reference to the type ADHero referred to by ad. Semantically speaking, the use of physical attack heroes as auxiliary heroes makes no sense, so the forced conversion fails and throws exceptions.
The following is a critical line analysis of the complete code:
Line 14: If you use ad as Hero, you can.
After transformation, the h reference points to an ad object
Line 15: The h reference may point to either an ad object or a support object
So when we convert h reference to AD type, we may succeed or fail.
Therefore, compulsory conversion should be carried out, in other words, the consequences of conversion should be borne by oneself.
Whether it can be converted to success depends on what kind of object the reference h refers to.
In this case, h points to an ad object, so converting to an ADHero type is possible.
Line 16: If you use a support object as Hero, you can
After transformation, the h reference points to a support object
Line 17: At this point, h points to a support object, so converting to ADHero type fails.
Failure is manifested by throwing an exception ClassCastException type conversion exception
package charactor; import charactor1.Support; public class Hero { public String name; protected float hp; public static void main(String[] args) { Hero h =new Hero(); ADHero ad = new ADHero(); Support s =new Support(); h = ad; //14 elements ad = (ADHero) h; //15 elements h = s; //16 elements ad = (ADHero)h; //17 elements } }
Example 4: Two classes with no inheritance relationship are converted to each other
Two classes without inheritance will fail if they are converted to each other
Although both ADHero and APero inherited Hero, they did not inherit each other.
"To use magical heroes as physical heroes" is not semantically correct either.
Two classes without inheritance relation, transform each other
package charactor; public class Hero { public String name; protected float hp; public static void main(String[] args) { ADHero ad = new ADHero(); APHero ap = new APHero(); // Conversion of types without inheritance relationships will fail, so compilation errors will occur. ad = (ADHero) ap; } }
Example 5: Implementing Class Conversion Interface (Upward Transition)
The object that refers to ad is the ADHero type, which implements the AD interface.
Line 10: Convert an ADHero type to an AD interface
Semantically speaking, an ADHero is used as an AD, and the AD interface has only one physical Attack method, which means that it is possible to invoke the physical Attack method after transformation, and the ADHero must have a physical Attack method, so the transformation can be successful.
package charactor; public class Hero { public String name; protected float hp; public static void main(String[] args) { ADHero ad = new ADHero(); AD adi = ad; //10 elements } }
Example 6: Interface conversion to implementation class (downward transition)
Line 10: The AD reference points to ADHero, while the adi reference is the interface type: AD, which implements class conversion to interface, is upward transition, so no coercion conversion is required, and it must succeed.
Line 12: adi actually points to an ADHero, so it can be converted to success
Line 14: The adi reference refers to an ADHero, which fails to convert to ADAPHero.
Assuming that the conversion is successful, the magicAttack method can be used, while the object ADHero referred to by adi does not have the magicAttack method.
package charactor; public class Hero { public String name; protected float hp; public static void main(String[] args) { ADHero ad = new ADHero(); AD adi = ad; //10 elements ADHero adHero = (ADHero) adi; //12 elements ADAPHero adapHero = (ADAPHero) adi; //14 elements adapHero.magicAttack(); } }
Example 7: instanceof
instanceof Hero determines whether a reference refers to an object of Hero type or a subclass of Hero
package charactor; public class Hero { public String name; protected float hp; public static void main(String[] args) { ADHero ad = new ADHero(); APHero ap = new APHero(); Hero h1= ad; Hero h2= ap; //Determine whether the object referenced by h1 is of ADHero type System.out.println(h1 instanceof ADHero); //Determine whether the object referenced by h2 is of APHero type System.out.println(h2 instanceof APHero); //Determine whether the object referenced by h1 is a subtype of Hero System.out.println(h1 instanceof Hero); } }
Practice: Object transformation
(Can the following conversion succeed?
If not, which line will make a mistake?
Why did it go wrong?
package charactor; public class Hero { public String name; protected float hp; public static void main(String[] args) { ADHero ad = new ADHero(); Hero h = ad; AD adi = (AD) h; APHero ap = (APHero) adi; //11 elements } }