简体   繁体   English

我不明白的Java问题

[英]Java issue that i dont understand

i am sorry that i might seem dumb but im new java as i am studying it in high school:对不起,我可能看起来很笨,但我是新的 Java,因为我在高中学习它:

public class One
{
    public void display()
    {
        System.out.print ("One");
    }
}

public class Two extends One
{
    public void display ()
    {
        System.out.print("Two");
    }
}

What is printed after the following code segment is executed?执行以下代码段后打印什么?

 One object1= new Two(); object1.display();
  1. OneTwo一二

  2. TwoOne二一

  3. Two

  4. One

  5. No output无输出

Can anyone please help me with answering the question above, i have read a lot about objects and classes and how they operate and how subclasses work but i cannot figure this question, plx help.谁能帮我回答上面的问题,我已经阅读了很多关于对象和类以及它们如何操作以及子类如何工作的内容,但我无法解决这个问题,请帮助。

Edit: This is an AP computer Science A question i dont really have a tutor/teacher to reach out to most of java was self-studied for me, so i would consider myself as a beginner to java but i got a lot covered, thanks for the help!!编辑:这是一门 AP 计算机科学 我真的没有导师/老师可以接触到大多数 java 的问题是我自学的,所以我认为自己是 java 的初学者,但我得到了很多内容,谢谢寻求帮助!

The good answer is 3. "Two".好的答案是 3。“二”。

To understand why, you have to understand polymorphism and overriding: In your case you have two classes:要了解原因,您必须了解多态性和覆盖:在您的情况下,您有两个类:

+------------------+
|        Two       |
+------------------+
          |
        extend
          |
          v
+------------------+
|        One       |
+------------------+

All public and protected field from One are visible and overridable by Two. One 中的所有公共和受保护字段都可以被 Two 看到和覆盖。 In your case, One.display() is overrided by Two.display() .在您的情况下, One.display()Two.display() () 覆盖。

If the code followed the Java conventions, there should be the annotation @Override on top of the Two.display() method to explicit this behavior to other developers and to the Compiler who's check if the method already exists in the parent object.如果代码遵循 Java 约定,则应该在 Two.display() 方法之上添加注释@Override ,以向其他开发人员和编译器明确此行为,以检查该方法是否已存在于父对象中。

I hope I have been helpful.我希望我对您有所帮助。

This problem actually common problem around high school studies.这个问题其实是高中学习的普遍问题。 But you need to just simple think.但是你需要简单的思考。

The Object class is superclass all of the classes in java. Object 类是 java 中所有类的超类。 You think something class withoud any extend operation.您认为某些类没有任何扩展操作。 And again imagine that, this something class has a method called method1.再想象一下,这个东西类有一个名为 method1 的方法。 (void or not void. Not important.) When you create an instance in main method, and, when you try to call method1 from this instance, if you use any IDE for java, you will see something methods called notify(), notifyAll(), toString() and etc. This methods are methods of Object classes. (无效或不无效。不重要。)当您在主方法中创建实例时,并且当您尝试从该实例调用 method1 时,如果您使用任何 Java IDE,您将看到一些名为 notify()、notifyAll 的方法()、toString() 等。这些方法是 Object 类的方法。

Beside, when you write a code like this此外,当您编写这样的代码时
Object instance = new YourClass();
and after then, when you write your ide like instance.之后,当您编写类似instance. you can see Object methods.你可以看到对象方法。

This is Polymorphism这是多态性

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

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