62,614
社区成员
发帖
与我相关
我的任务
分享
public class Test{
int count = 20 ;
{
count = 12;
}
public Test(){
System.out.println(count);
}
public static void main(String[] args) {
new Test();
}
}
public class Test{
int count;
public Test(){
count = 20;
count = 12;
System.out.println(count);
}
public static void main(String[] args) {
new Test();
}
}
public class Pro{
int count = age;
int age = 10;
{
count = 12;
}
public Pro(){
System.out.println(count);
}
public static void main(String[] args) {
new Pro();
}
}
public class Pro{
int count;
int age;
public Pro(){
count = age;
age = 10;
count = 12;
System.out.println(count);
}
public static void main(String[] args) {
new Pro();
}
}
public class Pro{
int count = age; // 变量-1 age还没定义,所以报错
int age = 10; // 变量-2
{
count = 12; // 初始化块-3
}
public Pro(){
System.out.println(count); // 构造器-4
}
public static void main(String[] args) {
new Pro();
}
}
public class Pro{
int count; // 变量-1
int age; // 变量-2
public Pro(){
count = age; // 构造器-3 这里因为age和count都初始化了,默认为0,正确
age = 10; // 重新赋值age为10
count = 12; // 重新赋值count为12,所以最后结果打印为12
System.out.println(count);
}
public static void main(String[] args) {
new Pro();
}
// 打了半天字。。
}
public class Test{
{
count = 12;
}
int count = 20 ;
public Test(){
System.out.println(count);
}
public static void main(String[] args) {
new Test();
}
}
这样是正确的,为什么呢