Intensive upward re-transformation series "Java programming ideas" and downcast

Foreword

Today re-read a bit upward transition and transformation down, some new experience, to understand the benefits of the transition upward, and how downcast. Here to others.

Upcast

Upward transition is used to show the relationship between the new class and the base classes. In conventional, the transition from the base class to derived class, in succession is moved upward in FIG. So called upward transition. Since the upward transition is from a more specialized type of conversion to the more common type, it is always safe. That is, the derived class is a superset of the base class. It may contain more than the base class method. But he must have the base class method contained.
We look at an example.

class Car {
    public void run() {
        System.out.println("这是父类run()方法");
    }
}

public class Benz extends Car {
    public void run() {
        System.out.println("这是Benz的run()方法");

    }

    public void price() {
        System.out.println("Benz:800000$");
    }

    public static void main(String[] args) {
        Car car = new Benz();
        car.run();
       //car.price();程序报错
    }
}

After running output. This is Benz's run () method.
But when we use this object to call Benz car class price this method will throw an error.
This is because we made the transition up here, car Although this object points to subclasses, but the subclass as a result of the upward transition, lost the use of the parent class does not have the "right" here is not calling price () this method.
Then the upward transition in the end what use is it, so far we do not see it not only benefits, but found that they can not use the calling subclass-specific approach after the transition upwards. Then the upward transition of the role of what in the end is it, we together look at the following code:

class Car {
    public void run() {
        System.out.println("这是父类run()方法");
    }

    public void speed() {
        System.out.println("speed:0");
    }

}

class BMW extends Car {
    public void run() {
        System.out.println("这是BMW的run()方法");
    }

    public void speed() {
        System.out.println("speed:80");
    }
}

public class Benz extends Car {
    public void run() {
        System.out.println("这是Benz的run()方法");

    }

    public void speed() {
        System.out.println("speed:100");
    }

    public void price() {
        System.out.println("Benz:800000$");
    }

    public static void main(String[] args) {
        show(new Benz());//向上转型实现
        show(new BMW());
    }

    public static void show(Car car) {//父类实例作为参数
        car.run();
        car.speed();
    }
}

The above code

    public static void main(String[] args) {
        show(new Benz());
        show(new BMW());
    }

    public static void show(Car car) {
        car.run();
        car.speed();
    }

It embodies the advantages of upward transition, which also reflects the abstract idea of ​​Java programming. If the transition is not up here to realize the function of each sub-class show, then there are several sub-categories will write how much function. code show as below:

    public static void main(String[] args) {
        show(new Benz());
        show(new BMW());
    }

    public static void show(Benz benz) {
        benz.run();
        benz.speed();
    }
    public static void show(BMW bmw) {
        bmw.run();
        bmw.speed();
    }

Imagine, if there are many sub-categories, then the transition will be much larger workload than not used up. It also shows that the upward transition further advantage is to increase the simplicity of the code.
We come with a special call one kind of static situations.

public class Animal {
    
        String name = "我是动物";
        static int age = 20;
        public void eat() {
            System.out.println("动物可以吃饭");
        }
        public static void sleep() {
            System.out.println("动物可以睡觉");
        }

        public void run(){
            System.out.println("动物可以奔跑");
        }

        public static void main(String[] args) {
            Animal am = new Dog();
            am.eat();
            am.sleep();
            am.run();
            //am.watchdog();这里会报错
            System.out.println(am.name);
            System.out.println(am.age);
        }

}
class Dog extends Animal {
    String name = "小狗";
    static int age = 60;
    public void eat() {
        System.out.println("小狗可以吃饭");
    }
    public static void sleep() {
        System.out.println("小狗可以睡觉");
    }
    public void watchdog() {
        System.out.println("小狗可以看门");
    }

}

operation result:

However, looking at the code block, the direct call Dog Watchdog () method will be given.

This is the upward transition because we have done here, am Although this object points to subclasses, but the subclass as a result of the upward transition, lost the use of the parent class does not have the "right" here is not calling watchdog () this method.
And the results where you can also see that sleep is the parent class reference "Animal" sleep method, because sleep method Animal is a static method, can be summarized as follows:
If you are accessing member variables, the compiler then is to see the parent class, running also look at the parent class.
If the access method, the compiler will see the parent class, then run to see subclasses.
If the method is static, compile and run all see the parent class.

Downcast

Look at the example of a mistake

public class Animal {
    public void eat(){
        System.out.println("Animal eat()");
    }
}

public class Dog extends Animal {
    @Override
    public void eat(){
        System.out.println("Dog eat");
    }
}

public class Test {
    public static void main(String[] args) {
        //向下转型
        Animal animal = new Animal();
        ((Dog)animal).eat();
    }
}

Operating results:
Exception in the Thread "main" java.lang.ClassCastException: com.hello.test.Animal CAN BE Cast not to com.hello.test.Dog
AT com.hello.test.Test.main (Test.java:7)
from the above examples point of view, Java does not seem to support downcast really so? In actual fact, Java also supports downcast, downcast but is conditional - only a reference to the parent class subclass object references in order to be downcast subclass object. In other words, before the downcast, must first the transition.

public class Animal {
    public void eat(){
        System.out.println("Animal eat()");
    }
}

public class Dog extends Animal {
    @Override
    public void eat(){
        System.out.println("Dog eat");
    }
}

public class Test {
    public static void main(String[] args) {
        //向上转型
        Animal animal = new Dog();
        //向下转型
        ((Dog)animal).eat();
    }
}

Operating results:
Dog EAT

Guess you like

Origin www.cnblogs.com/jichi/p/12064038.html