Java程序的输出|设置 3
预测以下Java程序的输出:
示例 1:
Java
// filename: Test.java
class Test {
// Declaring and initializing integer variable
int x = 10;
// Main driver method
public static void main(String[] args)
{
// Creating an object of class inside main()
Test t = new Test();
// Printing the value inside the object by
// above created object
System.out.println(t.x);
}
}
Java
// filename: Test.java
// Main class
class Test {
// Declaring and initializing variables
int y = 2;
int x = y + 2;
// main driver method
public static void main(String[] args)
{
// Creating an object of class inside main() method
Test m = new Test();
// Printing the value of x and y
// using above object created
System.out.println("x = " + m.x + ", y = " + m.y);
}
}
Java
// filename: Test.java
// Main clas
public class Test {
// Declaring and initializing integer with custom value
int x = 2;
// Constructor of this class
// Parameterized constructor
Test(int i) { x = i; }
// Main driver method
public static void main(String[] args)
{
// Creating object of class in main()
Test t = new Test(5);
// Printing the value
System.out.println("x = " + t.x);
}
}
Java
// filename: Test2.java
// Class 1
// Helper class
class Test1 {
// Constructor of this class
Test1(int x)
{
// Print statement whenever this constructor is
// called
System.out.println("Constructor called " + x);
}
}
// Class 2
// Class contains an instance of Test1
// Main class
class Test2 {
// Creating instance(object) of class1 in this class
Test1 t1 = new Test1(10);
// Constructor of this class
Test2(int i) { t1 = new Test1(i); }
// Main driver method
public static void main(String[] args)
{
// Creating instance of this class inside main()
Test2 t2 = new Test2(5);
}
}
输出
10
输出说明:
在Java中,可以使用类的声明来初始化成员。当初始化值可用并且可以将初始化放在一行时,此初始化效果很好(有关更多详细信息,请参阅this)。
示例 2:
Java
// filename: Test.java
// Main class
class Test {
// Declaring and initializing variables
int y = 2;
int x = y + 2;
// main driver method
public static void main(String[] args)
{
// Creating an object of class inside main() method
Test m = new Test();
// Printing the value of x and y
// using above object created
System.out.println("x = " + m.x + ", y = " + m.y);
}
}
输出
x = 4, y = 2
输出说明:
一个非常简单的解决方案:首先将 y 初始化为 2,然后将 x 初始化为 y + 2。因此 x 的值变为 4。
Geek have you ever wondered what will happen when a member is initialized in class declaration and constructor both?
示例 3:
Java
// filename: Test.java
// Main clas
public class Test {
// Declaring and initializing integer with custom value
int x = 2;
// Constructor of this class
// Parameterized constructor
Test(int i) { x = i; }
// Main driver method
public static void main(String[] args)
{
// Creating object of class in main()
Test t = new Test(5);
// Printing the value
System.out.println("x = " + t.x);
}
}
输出
x = 5
输出说明:
在Java中使用类声明进行初始化类似于在 C++ 中使用 Initializer List 进行初始化。因此,在上面的程序中,构造函数内部分配的值会覆盖 x 之前的值 2,x 变为 5。
示例 4:
Java
// filename: Test2.java
// Class 1
// Helper class
class Test1 {
// Constructor of this class
Test1(int x)
{
// Print statement whenever this constructor is
// called
System.out.println("Constructor called " + x);
}
}
// Class 2
// Class contains an instance of Test1
// Main class
class Test2 {
// Creating instance(object) of class1 in this class
Test1 t1 = new Test1(10);
// Constructor of this class
Test2(int i) { t1 = new Test1(i); }
// Main driver method
public static void main(String[] args)
{
// Creating instance of this class inside main()
Test2 t2 = new Test2(5);
}
}
输出
Constructor called 10
Constructor called 5
输出说明:
第一个 t2 对象在 main 方法中实例化。由于局部变量的初始化顺序首先出现,然后是构造函数,首先是实例变量 (t1),在 Test2 类中分配给内存。在这一行中,创建了一个新的 Test1 对象,在类 Test1 中调用构造函数并打印“构造函数称为 10”。接下来,调用 Test2 的构造函数并再次创建类 Test1 的新对象并打印“构造函数调用 5”。