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

查看:145
本文介绍了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 class有 subtract()方法与否,然后在运行时运行时多态性发生,因为对象的类型为 Child

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 。如果引用为null或者引用 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 的新实例。 Child Base 的子类,因此您可以将构造函数返回的引用分配给 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.

现在,您只能看到 Base 的成员通过 b2 ,即使实际值在执行时引用 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找出对象的实际类型$ c> b2 引用,并使用该类的的实现减去 - 在这种情况下,打印Child 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< T> 更改为 List< T> 的其他一些实现,代码应该仍然工作。你基本上是在说,我只需要 List< T> 提供的成员和保证 - 事实上我正在使用 ArrayList< T> ; 有点偶然。

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天全站免登陆