在这种情况下,继承和多态性如何工作? [英] How does inheritance and polymorphism work in this situation?

查看:60
本文介绍了在这种情况下,继承和多态性如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是头等舱

package test;

public class Project {

public void doSomething (String stuff) {

    writeStuff();
    whichProject(stuff);

}

public void writeStuff(){

    System.out.println("This is stuff");

}


public void whichProject(String stuff){

    System.out.println("This is a random project " + stuff);

}

}

这是派生类

package test;

public class Project1 extends Project{

public void whichProject(String stuff){

    System.out.println("Coding project number one: " + stuff);

}

public static void main(String[] args) {

    Project project = new Project1();

    project.doSomething("stuff");

}

}

运行Project1时,输出结果为:

When running Project1, the output turns out to be:

This is stuff
Coding project number one: stuff

为什么在Project1中调用whichProject()而不在Project中调用whichProject()?毕竟doSomething()是Project中的方法吗?还是当基类中的某个方法在基类中的另一个方法内部时,那么即使我们在另一个方法内部,变量所引用的对象仍然确定将调用哪个方法调用?

Why does it call whichProject() in Project1 and not the one in Project? After all, isn't doSomething() a method in Project? or when there is a method in the base class inside of another method in the base class then the object to which the variable refers to still determines which method invocation will be called even though we are inside of another method?

现在,如果我们将whichProject()的修饰符更改为private,以使该类现在为

Now, if we change the modifier of whichProject() to private so that the class now is

package test;

public class Project {

public void doSomething (String stuff) {

    writeStuff();
    whichProject(stuff);

}

public void writeStuff(){

    System.out.println("This is stuff");

}


private void whichProject(String stuff){

    System.out.println("This is a random project " + stuff);

}

}

输出变为:

This is stuff
This is a random project stuff

所以现在调用Project中的whichProject()方法,而不是调用一个Project1,即使该变量引用了Project1的一个对象.在这种情况下,我完全不了解发生了什么.对于这两种情况(带有public修饰符的whichProject()和带有private修饰符的whichProject())的解释将不胜感激.

so now the whichProject() method in Project is being called and not the one Project1 even though the variable refers to an object of Project1. In this case, I do not understand at all what is happening. An explanation for both situations (whichProject() with a public modifier and whichProject() with a private modifier) would be appreciated.

推荐答案

在Java中,所有方法都是虚拟的.

In Java, all methods are virtual.

虚拟方法是可以在派生类中重写的方法,只要子类中的版本具有相同的签名(返回类型和参数)即可.

Virtual methods are methods that can be overridden in a derived class as long as the version in the child class has the same signature (return type and arguments).

因此,在Project和Project1的原始版本中,如果您有Project1,则即使从Project方法中的代码中,项目1的版本public void whichProject(String stuff)也将被称为.

So, in the original version of Project and Project1, if you have a Project1, Project1's version of public void whichProject(String stuff) will be called even from code in Project's methods.

但是,如《 Java语言规范》第8.4.8.3节,私有方法不能被覆盖:

However, as stated in section 8.4.8.3 of the Java Language Specification, private methods can't be overridden:

请注意,就这些术语的技术意义而言,私有方法不能被隐藏或覆盖.这意味着子类可以在其超类之一中声明与私有方法具有相同签名的方法,并且不要求此类方法的返回类型或throws子句与私有方法中的私有方法有任何关系.超类.

Note that a private method cannot be hidden or overridden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a private method in one of its superclasses, and there is no requirement that the return type or throws clause of such a method bear any relationship to those of the private method in the superclass.

因此,当Project的doSomthing中的代码调用whichProject时,它将调用Project的私有版本,而不是Project1的公开版本.

Hence, when code inside Project's doSomthing calls whichProject, it calls Project's private version instead of Project1's public version.

这篇关于在这种情况下,继承和多态性如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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