可以在 class 的构造函数中使用 "new" 来调用 Java 中的另一个构造函数吗?
Can "new" be used inside the constructor of the class to call another constructor in Java?
我知道 this(...)
用于从另一个构造函数调用 class 的一个构造函数。但是我们可以使用 new
吗?
为了更清楚地说明问题,Line-2是否有效?如果是(因为编译器没有投诉),为什么输出是 null
而不是 Hello
?
class Test0 {
String name;
public Test0(String str) {
this.name= str;
}
public Test0() {
//this("Hello"); // Line-1
new Test0("Hello"){}; // Line-2
}
String getName(){
return name;
}
}
public class Test{
public static void main(String ags[]){
Test0 t = new Test0();
System.out.println(t.getName());
}
}
它是有效的,但它在该构造函数中创建了 Test0
的完全 单独的 实例(更具体地说,是 Test0
的匿名子类的实例) ,而且它没有在任何地方使用。当前实例仍将字段 name
设置为 null
。
public Test0() {
// this creates a different instance in addition to the current instance
new Test0("Hello"){};
}
请注意,如果您使用无参数构造函数调用 new
运算符,您将得到 WhosebugError
.
第 2 行是有效语句。这就是编译器没有显示任何错误的原因。但是你正在创建一个匿名对象。退出构造函数后它很快就会消失。因此,该值仍然为 null,因为没有为其分配任何值。
new Test0("Hello"){};
上一行将创建 Test0 的匿名实例 class 并将 Hello 的值分配给 name 变量。但是由于您没有引用创建的匿名实例,它会从紧接着的行中消失。因此,您仍然没有为调用特定代码段的实例的名称变量赋值。因此名字是null
记忆中好像是
您要执行的操作由您注释掉的代码完成:
public Test0()
{
this("Hello");
}
因为您创建了一个名称为 "hello" 的 Test0 的新实例,但从未使用它。
public Test() {
new Test0("hello") // nothing is referencing this new object
}
您只是在另一个构造函数中创建一个对象,但它不会影响第一个构造函数调用所创建的实例。
您可以执行此操作,但此new
用法的结果将在构造函数结束时消失。特别是,t.name
将是 null
.
使用this("Hello")
.
名称是实例变量。实例变量是对象特定的。
使用 new Test0("Hello"); 您正在创建 Test0 的新实例。
如果你想要 t.getName() return "Hello" [我的意思是字段值独立于对象],将 name
字段更改为静态:
static String name;
您可以通过以下代码使用 new 关键字显示输出。由于您在这里使用了 public Test0(){new Test("Hello){};"
{}
大括号并不重要..所以当调用 test0()
构造函数时。 ..在这个构造函数中 test0(args);
在第一个构造函数中被称为 bt 你没有显示输出..你的 "Hello" 将在哪里显示..所以只需编辑
`
public test0(String str)
{
this.name=str;
System.out.println(str);
}`
你会得到你想要的输出..看我下面的代码..
class test01{
public test01(String str)
{System.out.println(str);
}
public test01(){
new test01("Print");
}
}public class Const {
public static void main(String args[])
{test01 t = new test01();
//System.out.println(t.getName());
}}
此代码的输出将为您提供所需的字符串
我知道 this(...)
用于从另一个构造函数调用 class 的一个构造函数。但是我们可以使用 new
吗?
为了更清楚地说明问题,Line-2是否有效?如果是(因为编译器没有投诉),为什么输出是 null
而不是 Hello
?
class Test0 {
String name;
public Test0(String str) {
this.name= str;
}
public Test0() {
//this("Hello"); // Line-1
new Test0("Hello"){}; // Line-2
}
String getName(){
return name;
}
}
public class Test{
public static void main(String ags[]){
Test0 t = new Test0();
System.out.println(t.getName());
}
}
它是有效的,但它在该构造函数中创建了 Test0
的完全 单独的 实例(更具体地说,是 Test0
的匿名子类的实例) ,而且它没有在任何地方使用。当前实例仍将字段 name
设置为 null
。
public Test0() {
// this creates a different instance in addition to the current instance
new Test0("Hello"){};
}
请注意,如果您使用无参数构造函数调用 new
运算符,您将得到 WhosebugError
.
第 2 行是有效语句。这就是编译器没有显示任何错误的原因。但是你正在创建一个匿名对象。退出构造函数后它很快就会消失。因此,该值仍然为 null,因为没有为其分配任何值。
new Test0("Hello"){};
上一行将创建 Test0 的匿名实例 class 并将 Hello 的值分配给 name 变量。但是由于您没有引用创建的匿名实例,它会从紧接着的行中消失。因此,您仍然没有为调用特定代码段的实例的名称变量赋值。因此名字是null
记忆中好像是
您要执行的操作由您注释掉的代码完成:
public Test0()
{
this("Hello");
}
因为您创建了一个名称为 "hello" 的 Test0 的新实例,但从未使用它。
public Test() {
new Test0("hello") // nothing is referencing this new object
}
您只是在另一个构造函数中创建一个对象,但它不会影响第一个构造函数调用所创建的实例。
您可以执行此操作,但此new
用法的结果将在构造函数结束时消失。特别是,t.name
将是 null
.
使用this("Hello")
.
名称是实例变量。实例变量是对象特定的。
使用 new Test0("Hello"); 您正在创建 Test0 的新实例。
如果你想要 t.getName() return "Hello" [我的意思是字段值独立于对象],将 name
字段更改为静态:
static String name;
您可以通过以下代码使用 new 关键字显示输出。由于您在这里使用了 public Test0(){new Test("Hello){};"
{}
大括号并不重要..所以当调用 test0()
构造函数时。 ..在这个构造函数中 test0(args);
在第一个构造函数中被称为 bt 你没有显示输出..你的 "Hello" 将在哪里显示..所以只需编辑
`
public test0(String str)
{
this.name=str;
System.out.println(str);
}`
你会得到你想要的输出..看我下面的代码..
class test01{
public test01(String str)
{System.out.println(str);
}
public test01(){
new test01("Print");
}
}public class Const {
public static void main(String args[])
{test01 t = new test01();
//System.out.println(t.getName());
}}
此代码的输出将为您提供所需的字符串