Java self - transformation of the interface objects and inheritance

Transformation of Java objects

Example 1: a clear reference to the concept of type object type

First of all, a clear reference types and object type concept
in this case, there is an object new ADHero (), but also a reference to the ad
objects are typed, is ADHero
reference is typed, is ADHero
Typically, a reference type and object types are the same
types of problems to be discussed next conversion, referring to the reference type and object type conversion problem of inconsistency

A clear reference to the concept of type object type

package charactor;
 
public class Hero {
    public String name;
    protected float hp;
     
    public static void main(String[] args) {
         
        ADHero ad = new ADHero();
         
    }
}

Example 2: subclasses of the parent class revolutions (upcast)

The so-called transformation, means that when reference types and object types when inconsistent, only need to type conversion
type conversion sometimes succeed, sometimes fail (refer to the basic type of type conversion)

In the end whether the conversion was successful? To teach you a very simple way to distinguish
the left as the right to use , does not look plausible

Hero h = new Hero();
ADHero ad = new ADHero();
h = ad;

The right ad references the object pointed to by the type of physical attacks hero
left h type of reference is ordinary hero
to hero as a normal physical attack heroes, say make sense? Make sense, you can turn

All subclasses conversion parent class , are plausible. For example, examples around you

Apple phone inherits the phone, the Apple phone as a normal cell phone use
Yue-pure water inherited drinks, the Yue-pure water for use as a beverage

Subclasses of the parent class switch (upcast)

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();
         
        //类型转换指的是把一个引用所指向的对象的类型,转换为另一个引用的类型
         
        //把ad引用所指向的对象的类型是ADHero
        //h引用的类型是Hero
        //把ADHero当做Hero使用,一定可以
         
        h = ad;
         
    }
}

Example 3: parent class rotor (downcast)

Parent rotor class, sometimes OK, sometimes not, it must be cast.
Cast, which means that the conversion risk, your own risk.

When the line it?

1.        Hero h =new Hero();
2.        ADHero ad = new ADHero();
3.        h = ad;
4.        ad = (ADHero) h;

Line 3, the parent class is a subclass turn, will be able to
line 4, it is the parent class of the rotor, so that rotation to be strong.
h this reference points to the object is ADHero, so the fourth line, it will be converted to the ADHero ADHero, you can be a successful conversion.

When converting to do so?

1.        Hero h =new Hero();
2.        ADHero ad = new ADHero();
3.        Support s =new Support();
4.        h = s;
5.        ad = (ADHero)h;

Line 4, the parent class is a subclass turn, can be converted successfully
, line 5, h is the reference object pointed Support, to convert the type of ad ADHero reference. Semantically speaking, the physical attack hero, as a support hero to use, does not make sense, so the cast will fail and throw an exception

The following is a complete code of the key line of analysis:

14 line: ad as the Hero use, will be
after the conversion, h references to a target ad

15 lines: h reference point to a possible ad target, there may be a point to support the object
so the reference into a h AD type when there is likely to be successful, there is likely to fail
so to be cast, in other words the conversion consequences
in the end can not be converted successfully, depending on the reference point h in the end is what kind of object
in this example, h ad points to a target, so be converted into ADHero type, is possible

Line 16: The Hero used as a support subject, will be
after the conversion, h a reference point to the object support

Line 17: This time, h points to a support object, converted into ADHero type, will fail.
Failure manifestations is thrown abnormal ClassCastException type conversion

The rotor parent class (downcast)

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行
        ad = (ADHero) h; //15行
        h = s; //16行
        ad = (ADHero)h; //17行
    }
      
}

示例 4 : 没有继承关系的两个类,互相转换

没有继承关系的两个类,互相转换,一定会失败
虽然ADHero和APHero都继承了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();
 
        // 没有继承关系的类型进行互相转换一定会失败,所以会出现编译错误
        ad = (ADHero) ap;
 
    }
 
}

示例 5 : 实现类转换成接口(向上转型)

引用ad指向的对象是ADHero类型,这个类型实现了AD接口
10行: 把一个ADHero类型转换为AD接口
从语义上来讲,把一个ADHero当做AD来使用,而AD接口只有一个physicAttack方法,这就意味着转换后就有可能要调用physicAttack方法,而ADHero一定是有physicAttack方法的,所以转换是能成功的。

Converted into a class that implements the interface (upcast)

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行
          
    }
       
}

示例 6 : 接口转换成实现类(向下转型)

10行: ad引用指向ADHero, 而adi引用是接口类型:AD,实现类转换为接口,是向上转型,所以无需强制转换,并且一定能成功
12行: adi实际上是指向一个ADHero的,所以能够转换成功
14行: adi引用所指向的对象是一个ADHero,要转换为ADAPHero就会失败。

假设能够转换成功,那么就可以使用magicAttack方法,而adi引用所指向的对象ADHero是没有magicAttack方法的。

Interface into implementation class (downcast)

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行
   
        ADHero adHero = (ADHero) adi; //12行
            
        ADAPHero adapHero = (ADAPHero) adi; //14行
        adapHero.magicAttack();
    }
         
}

Example 7: instanceof

Hero the instanceof determines a reference object points, whether Hero type or subclass 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;
         
        //判断引用h1指向的对象,是否是ADHero类型
        System.out.println(h1 instanceof ADHero);
         
        //判断引用h2指向的对象,是否是APHero类型
        System.out.println(h2 instanceof APHero);
         
        //判断引用h1指向的对象,是否是Hero的子类型
        System.out.println(h1 instanceof Hero);
    }
}

Exercise : Object Transformation

(Converted as follows success?
If not, what is the line to be wrong?
Why be 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行
    }
}

Guess you like

Origin www.cnblogs.com/jeddzd/p/11497633.html