public class StudentFormMain {
public static void main(String[] args) {
new StudentForm();
}
}
和二等
public class StudentForm extends JFrame {
public StudentForm(){
setTitle("Admission Form");
setSize(300,250);
setVisible(true);
}
}
我的问题是
这些说法有什么区别
StudentForm studentform=new StudentForm();
和
new StudentForm()
第一个语句声明该类的 Obj 并调用构造函数,但在第二个语句中仅调用构造函数。
注意:结果相同
super(""); set the title of the frame
因为它正在调用父类(super class)构造函数。
我们还可以使用setTitle("")
方法设置标题
请区分方法。
最佳答案
代码:
StudentForm studentform = new StudentForm();
构造一个新的StudentForm
实例,并将对该实例的引用分配给变量studentform
。以便稍后可以调用 StudentForm
实例的方法,例如 studentform.addContainerListener(xxx)
。
但是代码new StudentForm()
只是调用构造函数来创建一个新的对象实例,但不保存对该实例的引用。
关于java - 这些构造函数调用语句之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21621096/