JAVA对象转型

来源:互联网 发布:编辑矢量图的软件 编辑:程序博客网 时间:2024/04/30 04:27

对象转型(casting)

1、一个基类的引用类型变量可以“指向”其子类的对象。

2、一个基类的引用不可以访问其子类对象新增加的成员(属性和方法)。

3、可以使用 引用变量 instanceof 类名 来判断该引用型变量所“指向”的对象是否属于该类或该类的子类。

4、子类的对象可以当做基类的对象来使用称作向上转型(upcasting),反之成为向下转型(downcasting)。

 

复制代码
复制代码
public class TestCasting{    public static void main(String args[]){        Animal a = new Animal("a");        Cat c = new Cat("c","cEyesColor");        Dog d = new Dog("d","dForlorColor");                System.out.println(a instanceof Animal);    //true        System.out.println(c instanceof Animal);    //true        System.out.println(d instanceof Animal);    //true        System.out.println(a instanceof Dog);        //false                a = new Dog("d2","d2ForlorColor");        //父类引用指向子类对象,向上转型        System.out.println(a.name);                //可以访问        //System.out.println(a.folorColor);   //!error   不可以访问超出Animal自身的任何属性        System.out.println(a instanceof Animal);    //是一只动物        System.out.println(a instanceof Dog);        //是一只狗,但是不能访问狗里面的属性                Dog d2 = (Dog)a;    //强制转换        System.out.println(d2.folorColor);    //将a强制转换之后,就可以访问狗里面的属性了    }}class Animal{    public String name;    public Animal(String name){        this.name = name;    }}class Dog extends Animal{    public String folorColor;    public Dog(String name,String folorColor){        super(name);        this.folorColor = folorColor;    }}class Cat extends Animal{    public String eyesColor;    public Cat(String name,String eyesColor){        super(name);        this.eyesColor = eyesColor;    }}
复制代码

 

 
复制代码

运行结果:

public class TestCasting2{    public static void main(String args[]){        TestCasting2 test2= new TestCasting2();                    Animal a = new Animal("animalName");        Dog d = new Dog("dogName","dogColor");        Cat c = new Cat("catName","catColor");                test2.f(a);    test2.f(d);     test2.f(c);        }    public void f(Animal a){        //父类引用型参数,达到了面向对象苦苦追求的可复用性,多种类型的对象,共同使用一个方法        System.out.println(a.name);                if(a instanceof Dog){            Dog d2 = (Dog)a;        //强制转换,然后就可以使用子类当中的属性了            System.out.println(d2.folorColor);        }else if(a instanceof Cat){            Cat c2 = (Cat)a;            System.out.println(c2.eyesColor);        }                            //这里有过还有一个鸟对象,只需要加一个if判断语句就ok了,不需要重写一个方法public void f2(Bird bird){}    }}class Animal{    public String name;    public Animal(String name){        this.name = name;    }}class Dog extends Animal{    public String folorColor;    public Dog(String name,String folorColor){        super(name);        this.folorColor = folorColor;    }}class Cat extends Animal{    public String eyesColor;    public Cat(String name,String eyesColor){        super(name);        this.eyesColor = eyesColor;    }}
复制代码

程序运行结果:

总结:

这边文章的基本记录就是:我们创建一个方法,方法的参数是父类的引用。而我们实际当中传的是子类的对象,这样我们就可以多个对象同时使用这一个方法了。然后我们在方法中通过强制类型转换,调用各个对象的各个属性。

通过这种对象的向上转型方法可以使我们的代码可复用性提高一些,但并不是可复用性达到了最好,详情见下一遍文章。

通过对象转型,达到了面向对象的可复用性。就好比:我要盖好一座大楼,然后要在大楼边上加一个厨房,我们只需直接盖厨房就好了。而不是将楼房的主题建筑拆掉,然后重新盖一次楼房。

转自:http://www.cnblogs.com/Gaojiecai/p/4034970.html
0 0