简体   繁体   English

扩展JFrame-“ super”关键字如何工作?

[英]Extending JFrame - how does “super” keyword work?

I've made a class that extends JFrame , and in my classes constructor, to set the title I can either use: 我制作了一个扩展JFrame的类,并在我的类构造函数中设置了我可以使用的标题:

super("title");
setTitle("title");

or just create a JFrame object ie: 或只是创建一个JFrame对象,即:

JFrame f = new JFrame("title);

How does the compiler differentiate from all of these to conclude the same thing? 编译器如何与所有这些区别以得出相同的结论? I know super is used to call overridden methods in the superclass, but how does it specifically know to call the setTitle method? 我知道super用于调用超类中的重写方法,但是如何特别知道调用setTitle方法呢?

My idea is that super calls the constructor of JFrame class, and there is only one constructor which takes a string parameter, so it's basically doing: JFrame f = new JFrame("title); , without explicitly creating the object. 我的想法是super调用JFrame类的构造函数,并且只有一个构造函数带有字符串参数,因此它基本上是在做: JFrame f = new JFrame("title);而没有显式创建对象。

Is this true? 这是真的? If not, can someone explain what super is exactly doing, and why setTitle does not need an object reference? 如果不是,有人可以解释super到底在做什么,为什么setTitle不需要对象引用?

Cheers 干杯

  1. new JFrame( "title" ) calls JFrame 's constructor. new JFrame( "title" )调用JFrame的构造函数。 It may call setTitle() (but it probably doesn't), or it may just set the internal variable used to hold the title. 它可能调用setTitle() (但可能不会调用),或者可能只是设置用于保存标题的内部变量。

  2. super.setTitle() is just a method call. super.setTitle()只是一个方法调用。 If you have overridden setTitle() , it'll ignore your definition and call the same method on the super class. 如果您重写了setTitle() ,它将忽略您的定义并在超类上调用相同的方法。 It's normally used for "chaining" to a super class's methods. 通常用于“链接”到超类的方法。

  3. super( "title" ) can only be called as the first line of a constructor. super( "title" )只能被称为构造函数的第一行。 It is not a method call. 这不是方法调用。 It calls a specific constructor in the super class, constructs that object, then continues with your constructor code. 它调用超类中的特定构造函数,构造该对象,然后继续构造函数代码。 This is the way all constructors work. 这就是所有构造函数的工作方式。 If you don't specify a constructor, the no-argument constructor ( super() ) is called automatically. 如果不指定构造函数,则将自动调用无参数构造函数( super() )。

If you look at the Frame Object, calling super (JFrame) means that you are passing the parameter into JFrame so JFrame jf = new JFrame("Title"); 如果查看框架对象,则调用super(JFrame)意味着您要将参数传递到JFrame中,因此JFrame jf = new JFrame("Title"); is the same as super("Title"); super("Title"); (assuming super type is JFrame) (假设超级类型是JFrame)

When you call super("title") in your super class, you tell Java "to instantiate my object, first call the constructor on the super class that takes a String argument". 当您在父类中调用super("title")时,您告诉Java“实例化我的对象,首先在具有String参数的父类上调用构造函数”。 If you don't have an explicit super call, then the no-arg constructor is called (as is always the case with the Object() constructor). 如果没有显式的超级调用,则将调用no-arg构造函数(与Object()构造函数一样)。 It turns out that that JFrame constructor calls setTitle with its argument. 事实证明,JFrame构造函数使用其参数调用setTitle

When you call setTitle("title") , this is implied, so you call the method on your object. 调用setTitle("title") ,这是隐含的,因此您可以在对象上调用方法。 If you haven't overridden it, then the super class implementation is called. 如果尚未覆盖它,则将调用超类实现。

how does it specifically know to call the setTitle method? 具体如何知道调用setTitle方法?

Calling super() by itself will call a superclass constructor. 单独调用super()将调用超类构造函数。

When you call setTitle() java looks to see if that method exists in the current class. 当您调用setTitle() Java会查看该方法是否存在于当前类中。 If it doesn't, it will look at the parent class. 如果没有,它将查看父类。

If you've overridden setTitle() , calling setTitle() from within the child class will call that class' method. 如果您重写了setTitle() ,则从子类中调用setTitle()将调用该类的方法。 If you call super.setTitle() it will call the setTitle() method in the super class. 如果调用super.setTitle() ,它将在超类中调用setTitle()方法。

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

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