当前位置: 技术问答>java相关
新手问题:关于new的问题?
来源: 互联网 发布时间:2015-06-27
本文导语: 请一个new的问题,关于以下两个定义的区别: 局部的code: String response2 = JOptionPane.showInputDialog(null,"Enter the site address:"); address = new JTextField(response2, 20); 1. address = new JTextField(response2, 20) 2. JTex...
请一个new的问题,关于以下两个定义的区别:
局部的code:
String response2 = JOptionPane.showInputDialog(null,"Enter the site address:");
address = new JTextField(response2, 20);
1. address = new JTextField(response2, 20)
2. JTextField address = new JTextField(response2, 20)
对于这两个定义,我有点confused,我对它们的理解是:
1.是生成一个指向JTextField内容的JTextField指针。如:我输入到的text为”109,天河路“,那它就是生成一个指向这个text的指针
2.定义一个JTextField的对象-address,存储输入的内容。引上例:即是生成一个JTextField对象,用来存放”109,天河路“
区别在于:内存损耗的多少,指针当然省点了。
如果,我的理解不对,那应是如何呢?请指教。
还有,两者在程序中都可运行正常,表面没区别。
以下是完整的程序,取自"21天学java"
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class Info extends JFrame {
private JLabel titleLabel = new JLabel("Title: ",
SwingConstants.RIGHT);
private JTextField title;
private JLabel addressLabel = new JLabel("Address: ",
SwingConstants.RIGHT);
private JTextField address;
private JLabel typeLabel = new JLabel("Type: ",
SwingConstants.RIGHT);
private JTextField type;
public Info() {
super("Site Information");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Site name
String response1 = JOptionPane.showInputDialog(null,
"Enter the site title:");
----------> title = new JTextField(response1, 50);
//运行JTextField title = new JTextField(response1, 20),一样
// Site address
String response2 = JOptionPane.showInputDialog(null,
"Enter the site address:");
----------> address = new JTextField(response2, 20);
//运行JTextField address = new JTextField(response2, 20),一样
// Site type
String[] choices = { "Personal", "Commercial", "Unknown" };
int response3 = JOptionPane.showOptionDialog(null,
"What type of site is it?",
"Site Type",
0,
JOptionPane.QUESTION_MESSAGE,
null,
choices,
choices[0]);
type = new JTextField(choices[response3], 20);
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(3, 2));
pane.add(titleLabel);
pane.add(title);
pane.add(addressLabel);
pane.add(address);
pane.add(typeLabel);
pane.add(type);
setContentPane(pane);
}
public static void main(String[] arguments) {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.err.println("Couldn't use the system "
+ "look and feel: " + e);
}
JFrame frame = new Info();
frame.pack();
frame.setVisible(true);
}
}
局部的code:
String response2 = JOptionPane.showInputDialog(null,"Enter the site address:");
address = new JTextField(response2, 20);
1. address = new JTextField(response2, 20)
2. JTextField address = new JTextField(response2, 20)
对于这两个定义,我有点confused,我对它们的理解是:
1.是生成一个指向JTextField内容的JTextField指针。如:我输入到的text为”109,天河路“,那它就是生成一个指向这个text的指针
2.定义一个JTextField的对象-address,存储输入的内容。引上例:即是生成一个JTextField对象,用来存放”109,天河路“
区别在于:内存损耗的多少,指针当然省点了。
如果,我的理解不对,那应是如何呢?请指教。
还有,两者在程序中都可运行正常,表面没区别。
以下是完整的程序,取自"21天学java"
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class Info extends JFrame {
private JLabel titleLabel = new JLabel("Title: ",
SwingConstants.RIGHT);
private JTextField title;
private JLabel addressLabel = new JLabel("Address: ",
SwingConstants.RIGHT);
private JTextField address;
private JLabel typeLabel = new JLabel("Type: ",
SwingConstants.RIGHT);
private JTextField type;
public Info() {
super("Site Information");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Site name
String response1 = JOptionPane.showInputDialog(null,
"Enter the site title:");
----------> title = new JTextField(response1, 50);
//运行JTextField title = new JTextField(response1, 20),一样
// Site address
String response2 = JOptionPane.showInputDialog(null,
"Enter the site address:");
----------> address = new JTextField(response2, 20);
//运行JTextField address = new JTextField(response2, 20),一样
// Site type
String[] choices = { "Personal", "Commercial", "Unknown" };
int response3 = JOptionPane.showOptionDialog(null,
"What type of site is it?",
"Site Type",
0,
JOptionPane.QUESTION_MESSAGE,
null,
choices,
choices[0]);
type = new JTextField(choices[response3], 20);
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(3, 2));
pane.add(titleLabel);
pane.add(title);
pane.add(addressLabel);
pane.add(address);
pane.add(typeLabel);
pane.add(type);
setContentPane(pane);
}
public static void main(String[] arguments) {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.err.println("Couldn't use the system "
+ "look and feel: " + e);
}
JFrame frame = new Info();
frame.pack();
frame.setVisible(true);
}
}
|
1. address = new JTextField(response2, 20)
2. JTextField address = new JTextField(response2, 20)
第一个只生成一个类内对象,第二个生成一个名字为address的函数内对象
在这个函数内部,以后再使用,采用的是第二个address,如果在此函数外部采用的则是第一个对象
二者不冲突,但是使用的时候最好不要采用同样的名字
|
指针?
这就好比全局和局部变量。^_^
这就好比全局和局部变量。^_^