什么 Base b2 = new Child();表示? [英] What does Base b2 = new Child(); signify?

查看:32
本文介绍了什么 Base b2 = new Child();表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 class Base {

    public void add() {
        System.out.println("Base ADD");
    }

    void subtract() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

}

class Child extends Base {

    public void add(){
        System.out.println("Child ADD");
    }

    public void subtract() {
        System.out.println("Child Subtract");
    }

}

class MainClass {

    public static void main(String args[]) {

        Base b1 = new Base();
        Base b2 = new Child();

        Child b3 = new Child();

        b1.add();

        b2.subtract();  

        b2.add();
        b3.subtract();

    }

}

我对上面的代码有些困惑.最让我困惑的一行是

I am somewhat confused with the above code. The line most confusing me is

Base b2 = new Child();

 b2.subtract();

我的理解是在编译时编译器检查天气 Base 类是否有 subtract() 方法,然后在运行时运行时多态性发生,因为对象是 孩子.

What I understand is at compile time compiler checks weather Base class has subtract() method or not, then at runtime runtime polymorphism occurs as object is of type Child.

问题是我们如何或在哪里使用这一行,即 Base b2 = new Child();

The question is how or where we can use this line i.e. Base b2 = new Child();

我们应该在什么场景下使用它?请帮忙,那太好了!

In what scenario we should use this? Please help, it would be great!

推荐答案

看两部分声明:

Base b2

声明了一个名为 b2 的类型为 Base 的变量.如果引用为空或引用 Base 的实例或 Base 的子类,您可以分配对该变量的引用.

that declares a variable called b2 of type Base. You can assign a reference to that variable if the reference is null or refers to an instance of Base or a subclass of Base.

然后

new Child()

创建一个 Child 的新实例.ChildBase 的子类,因此可以将构造函数返回的引用赋值给 b2 变量.

creates a new instance of Child. Child is a subclass of Base, so you can assign the reference returned by the constructor to the b2 variable.

现在,您只能看到"Baseb2 的成员,即使实际值指的是 Child 在执行时间处理时间.但是 Base 中的任何方法在 Child 中被覆盖 将在调用它们时使用覆盖的版本......所以当你调用

Now, you can only "see" members of Base through b2, even though the actual value refers to an instance of Child at execution time. But any methods from Base which have been overridden in Child will use the overridden version when they're called... so when you call

b2.subtract();

JVM 找出 b2 引用的对象的 实际 类型,并使用该类的 subtract 实现 - 在这种情况下, 打印子减法"的方法.

the JVM finds out the actual type of the object that b2 refers to, and uses that class's implementation of subtract - in this case, the method which prints "Child Subtract".

您特别询问可以在哪里使用这种东西,以及它如何帮助...

You've specifically asked where you can use this sort of thing, and how it helps...

你可以在任何时候使用它来声明一个更通用的类型(超类或接口)的变量,但为它分配一个值,它恰好是一个子类或接口的实现.再举一个例子:

You can use it any time you want to declare a variable of a more general type (a superclass or an interface) but assign a value to it which happens to be a subclass or an implementation of the interface. As another example:

List<String> strings = new ArrayList<String>();

以通用方式声明变量的主要优点是您保留了灵活性 - 稍后您可以从使用 ArrayList 更改为 List<;T>,代码应该仍然有效.你基本上是在说,我只需要 List 提供的成员和保证——我使用 ArrayList 的事实有点偶然."

The main advantage of declaring a variable in a general way is that you're retaining flexibility - later on you could change from using ArrayList<T> to some other implementation of List<T>, and the code should still work. You're basically saying, "I only need the members and guarantees provided by List<T> - the fact that I'm using ArrayList<T> is somewhat incidental."

一个类似的例子是决定一个方法应该返回什么类型——通常你只想声明你返回一个通用类型(或接口),即使实现知道它返回的是哪种具体类型.这隐藏了实现细节,允许您稍后更改它们.

A similar example is deciding what type a method should return - often you want to only declare that you return a general type (or interface) even though the implementation knows which concrete type it's returning. That hides the implementation details, which allows you to change them later.

这篇关于什么 Base b2 = new Child();表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆