62,628
社区成员
发帖
与我相关
我的任务
分享
public class Book1 {
static Book1 book = new Book1();
static int a;
static int b=2;
private Book1() {
// TODO Auto-generated constructor stub
System.out.println("a="+a+",b="+b);
a++;
b++;
System.out.println("a="+a+",b="+b);
}
public static void main(String[] args) {
System.out.println("start main -- book");
Book1 book = new Book1();
Book1 book1= new Book1();
System.out.println(book1.a);
System.out.println(book1.b);
}
}
a=0,b=0
a=1,b=1
start main -- book
a=1,b=2
a=2,b=3
a=2,b=3
a=3,b=4
3
4
package com.demo;
public class Book {
static Book book = new Book();
static int a;
static int b = 0;
private Book(){
System.out.println("a="+a+",b="+b);
a++;
b++;
System.out.println("a="+a+",b="+b);
}
public static void main(String[] args) {
System.out.println("start main -- book");
Book book = new Book();
System.out.println(book.a);
System.out.println(book.b);
}
}
输出:
a=0,b=0
a=1,b=1
start main -- book
a=1,b=0
a=2,b=1
2
1
https://www.cnblogs.com/DreamDrive/p/5428622.html
我也不是很理解输出,等待大佬出现。