JAVA中的super()函数

发布于 2024-07-10 06:09:31 字数 525 浏览 7 评论 0原文

按钮类:

class SubmitButton extends JButton implements ActionListener {
    public SubmitButton(String title){
        super(title);
        ....

我在哪里声明它:

SubmitButton submit = new SubmitButton("Submit");
submit.setBounds(530+150, 200, 100, 25);

super(title) 如何将 String title 设置为按钮的标题? 它如何与常规 JButton 的 .setText() 方法执行相同的操作?

换句话说,调用 super() 如何执行与 .setText() 相同的操作 super() 如何知道更改标题? .setText() 是 JButton 类中唯一接受字符串作为参数的方法吗?

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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

鲜肉鲜肉永远不皱 2024-07-17 06:09:31
  • SubmitButton 扩展 JButton
  • JButton 有一个构造函数,可能如下所示(简化):

    public JButton(字符串标题) { 
          设置文本(标题); 
      } 
      
  • SubmitBUtton 有一个构造函数:

    public SubmitButton(字符串标题) { 
          超级(标题); 
      } 
      

SubmitButton 构造函数正在调用超类 (JButton) 构造函数,而超类 (JButton) 构造函数又调用 setText。 现在在内部,JButton 的工作方式可能有所不同,但效果是相同的。

总体要点是 super(...) 调用超类构造函数。

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

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.

The overall point is that super(...) calls the superclass constructor.

妖妓 2024-07-17 06:09:31

JButton 是一个核心 Java Swing 类,支持多个构造函数。 这些构造函数之一允许您设置按钮标签的文本。

添加 super(title) 实际上并没有进行调用 - 父级的构造函数无论如何都会被调用,但它有助于选择调用哪个构造函数。

setText() 操作允许您在创建按钮之后但(通常)在显示按钮之前更改文本。

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.

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.

The setText() operation allows you to change text after button is created but (usually) before it is displayed.

深居我梦 2024-07-17 06:09:31

当使用构造函数 SubmitButton(String title) 初始化 SubmitButton 时,您可以使用 title 调用父类/超类构造函数。 如果你没有指定 super(title),那么 Java 编译器会自动插入代码来调用超类的默认构造函数。 那么当您创建 SubmitButton 时就不会设置标题。

此外,超类 (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

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.

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

http://www.javaworld.com/jw-10-2000/jw-1013-constructors.html?page=2
http://leepoint.net/notes-java/oop/constructors/constructor.html

一笑百媚生 2024-07-17 06:09:31

在 java 中的所有情况下,对 super() 的“调用”都会调用父类的 ctor。 与所有函数一样,当您调用构造函数时,Java 模式会匹配名称和参数类型。 JButton 声明一个带有字符串参数的 ctor,因此当您调用 super(title) 时,效果是您正在调用直接超类 (JButton) 的构造函数,该构造函数采用单字符串参数。

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.

一杯敬自由 2024-07-17 06:09:31

http:// /java.sun.com/javase/6/docs/api/javax/swing/JButton.html#JButton(java.lang.String)

(复制粘贴整个 URL;由于某种原因,字符串部分不是链接)

它调用上面链接的 JButton 类中定义的构造函数,该构造函数将按钮的文本设置为传入的字符串。 super() 是对超类构造函数的调用。

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)

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.

拥醉 2024-07-17 06:09:31

我个人通过设置标题而不是文本解决了这个问题。

static String TitleName = "某个标题";
超级(标题名称);

然后按您的按钮或在任何应该发生操作的地方。

setTitle("新标题");

这对我有用。 :-)

I personally have resolved this by setting Title rather than text.

static String TitleName = "Some Title";
super (TitleName);

Then on you button or where ever the action should take place.

setTitle("New Title");

This works for me. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文