为什么在Java中的方法调用的中间使用圆括号? [英] Why are parenthesis used in the middle of a method call in Java?

查看:462
本文介绍了为什么在Java中的方法调用的中间使用圆括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的问题是:为什么在中间使用圆括号方法调用?

  package com.zetcode; 

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class QuitButtonExample extends JFrame {

public QuitButtonExample(){

initUI();
}

private void initUI(){

JPanel panel = new JPanel();
getContentPane()。add(panel);

panel.setLayout(null);

JButton quitButton = new JButton(Quit);
quitButton.setBounds(50,60,80,30);

quitButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent event){
System.exit(0);
}
});

panel.add(quitButton);

setTitle(Quit button);
setSize(300,200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String [] args){

SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
QuitButtonExample ex = new QuitButtonExample();
ex.setVisible(true);
}
});
}
}

我是指 getContentPane ().add(panel); 语句。我知道它做什么,但不真正了解它是如何工作的。我是Java的新手,并且在类OO中的基础知识就像类字段,类方法,实例字段,实例方法,内部类,但是这一个。

解决方案

这是常见的成语,称为方法链接,看起来像。

  getContentPane()。add(panel); 

解析为

  Container c = this.getContentPane(); 
c.add(panel);

括号表示方法调用,其返回值原样使用对象用于下一个方法调用( add )。


I came across some code and cannot understand a certain aspect of it although I have done some extensive searching!

My question is: Why are parenthesis used in the middle of a method call?

    package com.zetcode;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;


    public class QuitButtonExample extends JFrame {

    public QuitButtonExample() {

        initUI();
    }

    private void initUI() {

       JPanel panel = new JPanel();
       getContentPane().add(panel);

       panel.setLayout(null);

       JButton quitButton = new JButton("Quit");
       quitButton.setBounds(50, 60, 80, 30);

       quitButton.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent event) {
               System.exit(0);
          }
       });

       panel.add(quitButton);

       setTitle("Quit button");
       setSize(300, 200);
       setLocationRelativeTo(null);
       setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                QuitButtonExample ex = new QuitButtonExample();
                ex.setVisible(true);
            }
        });
    }
}

I am referring to getContentPane().add(panel); statement. I know what it does, but doesn't really understand how it works. I'm new to Java and have the basics in OO like class fields, class methods, instance fields, instance methods, inner classes, but this one.

解决方案

This is what the common idiom, called method chaining, looks like.

getContentPane().add(panel);

parses as

Container c = this.getContentPane();
c.add(panel);

The parens indicate a method call, and its return value is used in-place as the this object for the next method call (add).

这篇关于为什么在Java中的方法调用的中间使用圆括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆