Home > Java > javaTutorial > What is object casting in Java? (with code)

What is object casting in Java? (with code)

云罗郡主
Release: 2018-10-15 13:50:44
forward
2204 people have browsed it

This article brings you what is object transformation in Java? (Attached is the code), which has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Upward transformation: Subclass objects are converted to parent classes, and the parent class can be an interface. Formula: Father f = new Son();Father is the parent class or interface, and son is the subclass.

Downward transformation: The parent class object is converted into a subclass. Formula: Son s = (Son)f;

We set the formal parameters to the parent class Animal type. When executing test.f(c), the memory situation is as follows :

c is passed in as the Cat type, and Animal a is used as the formal parameter, which is equivalent to executing Animal a = new Cat(). At this time, a and c point to the Cat object at the same time. , but at this time a cannot access the data members extended by the Cat class, so then a can be forcibly converted to the Cat type. If there is no such transformation mechanism, we will have to write two functions f(Cat c) and f(Dog d) for cats and dogs respectively. But in fact, the scalability of the program above is not the best. We can also use dynamic binding (i.e. polymorphism) to further improve scalability. The three premises of the polymorphic mechanism are: (1) inheritance is required, (2) rewriting is required, that is, the subclass redefines certain methods in the parent class, and (3) upward transformation is required, using parent class references to point to subclass object. Let's look at an example:

class Animal {    private String name;    /**
     * 在Animal类自定义的构造方法
     * @param name     */
    Animal(String name) {        this.name = name;
    }    /**
     * 在Animal类里面自定义一个方法enjoy     */
    public void enjoy() {
        System.out.println("动物的叫声……");
    }
}class Cat extends Animal {    private String eyesColor;    /**
     * 在子类Cat里面定义Cat类的构造方法
     * @param n
     * @param c     */
    Cat(String n, String c) {        /**
         * 在构造方法的实现里面首先使用super调用父类Animal的构造方法Animal(String name)。
         * 把子类对象里面的父类对象先造出来。         */
        super(n);
        eyesColor = c;
    }    /**
     * 子类Cat对从父类Animal继承下来的enjoy方法不满意,在这里重写了enjoy方法。     */
    public void enjoy() {
        System.out.println("我养的猫高兴地叫了一声……");
    }
}/**
 * 子类Dog从父类Animal继承下来,Dog类拥有了Animal类所有的属性和方法。
 * @author gacl
 * */class Dog extends Animal {    /**
     * 在子类Dog里面定义自己的私有成员变量     */
    private String furColor;    /**
     * 在子类Dog里面定义Dog类的构造方法
     * @param n
     * @param c     */
    Dog(String n, String c) {        /**
         * 在构造方法的实现里面首先使用super调用父类Animal的构造方法Animal(String name)。
         * 把子类对象里面的父类对象先造出来。         */
        super(n);
        furColor = c;
    }    /**
     * 子类Dog对从父类Animal继承下来的enjoy方法不满意,在这里重写了enjoy方法。     */
    public void enjoy() {
        System.out.println("我养的狗高兴地叫了一声……");
    }
}/**
 * 子类Bird从父类Animal继承下来,Bird类拥有Animal类所有的属性和方法
 * @author gacl
 * */class Bird extends Animal {    /**
     * 在子类Bird里面定义Bird类的构造方法     */
    Bird() {        /**
         * 在构造方法的实现里面首先使用super调用父类Animal的构造方法Animal(String name)。
         * 把子类对象里面的父类对象先造出来。         */
        super("bird");
    }    /**
     * 子类Bird对从父类Animal继承下来的enjoy方法不满意,在这里重写了enjoy方法。     */
    public void enjoy() {
        System.out.println("我养的鸟高兴地叫了一声……");
    }
}/**
 * 定义一个类Lady(女士)
 * @author gacl
 * */class Lady {    /**
     * 定义Lady类的私有成员变量name和pet     */
    private String name;    private Animal pet;    /**
     * 在Lady类里面定义自己的构造方法Lady(),
     * 这个构造方法有两个参数,分别为String类型的name和Animal类型的pet,
     * 这里的第二个参数设置成Animal类型可以给我们的程序带来最大的灵活性,
     * 因为作为养宠物来说,可以养猫,养狗,养鸟,只要是你喜欢的都可以养,
     * 因此把它设置为父类对象的引用最为灵活。
     * 因为这个Animal类型的参数是父类对象的引用类型,因此当我们传参数的时候,
     * 可以把这个父类的子类对象传过去,即传Dog、Cat和Bird等都可以。
     * @param name
     * @param pet     */
    Lady(String name, Animal pet) {        this.name = name;        this.pet = pet;
    }    /**
     * 在Lady类里面自定义一个方法myPetEnjoy()
     * 方法体内是让Lady对象养的宠物自己调用自己的enjoy()方法发出自己的叫声。     */
    public void myPetEnjoy() {
        pet.enjoy();
    }
}public class Jerque {    public static void main(String args[]) {        /**
         * 在堆内存里面new了一只蓝猫对象出来,这个蓝猫对象里面包含有一个父类对象Animal。         */
        Cat c = new Cat("Catname", "blue");        /**
         * 在堆内存里面new了一只黑狗对象出来,这个黑狗对象里面包含有一个父类对象Animal。         */
        Dog d = new Dog("Dogname", "black");        /**
         * 在堆内存里面new了一只小鸟对象出来,这个小鸟对象里面包含有一个父类对象Animal。         */
        Bird b = new Bird();        /**
         * 在堆内存里面new出来3个小姑娘,名字分别是l1,l2,l3。
    * l1养了一只宠物是c(Cat),l2养了一只宠物是d(Dog),l3养了一只宠物是b(Bird)。
         * 注意:调用Lady类的构造方法时,传递过来的c,d,b是当成Animal来传递的,
         * 因此使用c,d,b这三个引用对象只能访问父类Animal里面的enjoy()方法。         */
        Lady l1 = new Lady("l1", c);
        Lady l2 = new Lady("l2", d);
        Lady l3 = new Lady("l3", b);        /**
         * 这三个小姑娘都调用myPetEnjoy()方法使自己养的宠物高兴地叫起来。         */
        l1.myPetEnjoy();
        l2.myPetEnjoy();
        l3.myPetEnjoy();
    }
}
Copy after login

In the above example, we found that if we want to add a new animal, we only need to define the corresponding class to inherit Animal , without touching any code at all, because the core object-oriented thing - polymorphism is used here. Different from the previous example, although we have always emphasized that when using a parent class reference to point to a subclass object, the parent class cannot access the subclass's own members, but methods are different from data members. Which method to call is determined at runtime. Whatever object is new, the method of the corresponding object is called. It depends on the actual object new, not the reference to the object. Therefore, when different animal types are passed in, mypetEnjoy() will execute different methods.

The above is what is object transformation in Java? (With code) full introduction, if you want to know more about Java video tutorial, please pay attention to the PHP Chinese website.

The above is the detailed content of What is object casting in Java? (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template