简体   繁体   English

在这种情况下,继承和多态性如何工作?

[英]How does inheritance and polymorphism work in this situation?

This is the first class 这是头等舱

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);

}

}

and this is the derived class 这是派生类

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");

}

}

When running Project1, the output turns out to be: 运行Project1时,输出结果为:

This is stuff
Coding project number one: stuff

Why does it call whichProject() in Project1 and not the one in Project? 为什么它在Project1中调用whichProject()而不在Project中调用whichProject() After all, isn't doSomething() a method in Project? 毕竟, doSomething()不是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? 还是当基类中的某个方法在基类中的另一个方法内部时,那么即使我们在另一个方法内部,变量所引用的对象仍然确定将调用哪个方法调用?

Now, if we change the modifier of whichProject() to private so that the class now is 现在,如果我们将whichProject()的修饰符更改为private ,则该类现在是

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);

}

}

the output becomes: 输出变为:

This is stuff
This is a random project stuff

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. 因此,即使变量引用了Project1的对象,现在也将调用Project中的whichProject()方法,而不是一个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. (这两种情况的说明whichProject()public改性剂和whichProject()private调整值),将不胜感激。

In Java, all methods are virtual. 在Java中,所有方法都是虚拟的。

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

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 . 因此,在Project和Project1的原始版本中,如果您有Project1,则即使从Project方法中的代码也将调用Project1的public void whichProject(String stuff)版本。

However, as stated in section 8.4.8.3 of the Java Language Specification , private methods can't be overridden: 但是,如Java语言规范8.4.8.3节所述 ,私有方法不能被覆盖:

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. 这意味着子类可以在其超类之一中声明与私有方法具有相同签名的方法,并且不要求此类方法的返回类型或throws子句与私有方法中的私有方法有任何关系。超类。

Hence, when code inside Project's doSomthing calls whichProject , it calls Project's private version instead of Project1's public version. 因此,当Project的doSomthing代码调用whichProject ,它将调用Project的私有版本,而不是Project1的公共版本。

Inheritance follow the bottom up approach. 继承遵循自下而上的方法。

So writeProject() used is from super class class and whichProject(stuff) is from base class. 因此,使用的writeProject()来自超类类,而whichProject(stuff)来自基类。

you can refer this example inheritance and polymorphism 您可以参考此示例继承和多态

Hope it helps. 希望能帮助到你。

For inheritance of methods, use this model: 对于方法的继承,请使用以下模型:

  1. Collect all methods in the current class. 收集当前类中的所有方法。
  2. Add methods from super classes which are visible (ie not private ) and which you haven't seen so far. 从超类中添加方法,这些方法是可见的(即,不是private ),并且到目前为止您还没有看到。
  3. Add this list to the topmost class in the inheritance list 将此列表添加到继承列表中最顶层的类

When a method is invoked, Java knows the type of the instance. 调用方法时,Java知道实例的类型。 Imagine that this is passed to each method as an invisible method parameter. 想象一下, this作为一个不可见的方法参数传递给每个方法。 Methods aren't resolved by looking at the class in which the method is defined. 通过查看定义方法的类无法解析方法。

Instead, Java takes this , calls getClass() and then gets the list of methods as described above. 相反,Java使用this ,调用getClass() ,然后如上所述获取方法列表。 So even when the code in Project is executed, it still uses the methods from the list for Project1 . 因此,即使在执行Project的代码时,它仍然使用Project1列表中的方法。

This changes when you make the method private . 当您将方法private时,这会改变。 Private methods can't be overwritten, they simply don't show up in the list above. 私有方法不能被覆盖,它们根本不会出现在上面的列表中。 Instead, Java inserts code to call it directly without looking into the list. 相反,Java插入代码以直接调用它,而无需查看列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM