JAVA 中的 super() 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/382854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
super() function in JAVA
提问by Devoted
Button class:
按钮类:
class SubmitButton extends JButton implements ActionListener {
public SubmitButton(String title){
super(title);
....
Where I declare it:
我在哪里声明:
SubmitButton submit = new SubmitButton("Submit");
submit.setBounds(530+150, 200, 100, 25);
How does super(title) set the String title to the title of the button? How does it do the same as the method .setText() for regular JButtons?
super(title) 如何将String title 设置为按钮的标题?它如何与常规 JButton 的 .setText() 方法相同?
In other words, how did calling super() do the same thing as .setText() how does super() know to change title? is .setText() the only method in JButton Class that takes in string as parameter?
换句话说,调用 super() 如何与 .setText() 做同样的事情 super() 如何知道改变标题?.setText() 是 JButton 类中唯一将字符串作为参数的方法吗?
采纳答案by Draemon
- SubmitButton extends JButton
JButton has a constructor which might look like this (simplified):
public JButton(String title) { setText(title); }
SubmitBUtton has a constructor:
public SubmitButton(String title) { super(title); }
- SubmitButton 扩展了 JButton
JButton 有一个构造函数,它可能看起来像这样(简化):
public JButton(String title) { setText(title); }
SubmitBUtton 有一个构造函数:
public SubmitButton(String title) { super(title); }
The SubmitButton constructor is calling the superclass (JButton) constructor, which is in turn calling setText. Now internally, JButton might work differently, but the effect is the same.
SubmitButton 构造函数调用超类 (JButton) 构造函数,后者又调用 setText。现在在内部,JButton 的工作方式可能有所不同,但效果是相同的。
The overall point is that super(...) calls the superclass constructor.
总体要点是 super(...) 调用超类构造函数。
回答by cliff.meyers
http://java.sun.com/javase/6/docs/api/javax/swing/JButton.html#JButton(java.lang.String)
http://java.sun.com/javase/6/docs/api/javax/swing/JButton.html#JButton(java.lang.String)
(copy paste the entire URL; for some reason the String part is not linked)
(复制粘贴整个 URL;出于某种原因,字符串部分未链接)
It is calling the constructor defined in the JButton class linked above which sets the text of the button to the String passed in. super() is a call to a superclass constructor.
它调用上面链接的 JButton 类中定义的构造函数,它将按钮的文本设置为传入的字符串。 super() 是对超类构造函数的调用。
回答by Uri
JButton is a central Java Swing class that supports multiple constructors. One of these constructors allows you to set the text for the button label.
JButton 是一个支持多个构造函数的中央 Java Swing 类。这些构造函数之一允许您设置按钮标签的文本。
Adding super(title) didn't actually make a call - the constructor of the parent would have been called anyway, but it helped select which constructor is invoked.
添加 super(title) 实际上并没有进行调用 - 无论如何都会调用父级的构造函数,但它有助于选择调用哪个构造函数。
The setText() operation allows you to change text after button is created but (usually) before it is displayed.
setText() 操作允许您在创建按钮之后但(通常)在显示之前更改文本。
回答by kevindaub
When the SubmitButton is initialized with the constructor SubmitButton(String title), you call the parent/super class constructor with the title. If you didn't specify the super(title), then the Java compiler would automagically insert the code to call the default constructor of the super class. Then the title would not be set when you create the SubmitButton.
当使用构造函数 SubmitButton(String title) 初始化 SubmitButton 时,您将使用标题调用父/超类构造函数。如果您没有指定 super(title),那么 Java 编译器会自动插入代码以调用超类的默认构造函数。那么在您创建 SubmitButton 时将不会设置标题。
Also, the superclass (JButton) might be using the .setText(string) within its constructor and that is why it performs the same function (need to look at the actual Java source).
此外,超类 (JButton) 可能在其构造函数中使用 .setText(string) ,这就是它执行相同功能的原因(需要查看实际的 Java 源代码)。
http://www.javaworld.com/jw-10-2000/jw-1013-constructors.html?page=2
http://leepoint.net/notes-java/oop/constructors/constructor.html
http://www.javaworld.com/jw-10-2000/jw-1013-constructors.html?page=2
http://leepoint.net/notes-java/oop/constructors/constructor.html
回答by Charlie Martin
In all cases in java, that "call" to super()
invokes the parent class's ctor. Like all functions, when you call a ctor, Java pattern matches the name and parameter types. JButton
declares a ctor with string argument, so when you call super(title)
the effect is that you're invoking the constructor for the immediate superclass (JButton) that takes a single string argument.
在java中的所有情况下,“调用”super()
调用父类的构造函数。与所有函数一样,当您调用 ctor 时,Java 模式会匹配名称和参数类型。 JButton
声明一个带有字符串参数的ctor,因此当您调用时super(title)
,效果是您正在为采用单个字符串参数的直接超类(JButton)调用构造函数。
回答by Charlie Martin
I personally have resolved this by setting Title rather than text.
我个人通过设置标题而不是文本来解决这个问题。
static String TitleName = "Some Title"; super (TitleName);
static String TitleName = "一些标题"; 超级(标题名称);
Then on you button or where ever the action should take place.
然后在你的按钮或任何应该发生动作的地方。
setTitle("New Title");
setTitle("新标题");
This works for me. :-)
这对我有用。:-)