//静态代码块 static{}
class Demo
{
static int count;//共享数据
//构造代码块
{
//构造代码快中的代码没创建一次对象就会执行一次。
count++;
}
}
class Person
{
String name;
static String country;
//定义一个静态代码块
static{
//给静态的成员变量赋值
country = "中国";
}
public Person(String name){
this.name = name;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
//打印信息的方法
public void showInfo(){
System.out.println("姓名:"+name + " 国家:"+country);
}
}
class PersonTest
{
public static void main(String[] args)
{
Person[] personArr = new Person[10];
for(int i = 1;i<=10;i++){
Person p = new Person("李明"+i);
//给数组中添加元素
personArr[i-1] = p;
p.showInfo();
}
//需要改国籍
Person.country = "新加坡 ";
for(Person p : personArr){
p.showInfo();
}
for(int i =1 ; i<=10;i++){
Test t = new Test();
}
System.out.println("Demo.count");
}
}
class Demo
{
static int count;//共享数据
//构造代码块
{
//构造代码快中的代码没创建一次对象就会执行一次。
count++;
}
}
class Person
{
String name;
static String country;
//定义一个静态代码块
static{
//给静态的成员变量赋值
country = "中国";
}
public Person(String name){
this.name = name;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
//打印信息的方法
public void showInfo(){
System.out.println("姓名:"+name + " 国家:"+country);
}
}
class PersonTest
{
public static void main(String[] args)
{
Person[] personArr = new Person[10];
for(int i = 1;i<=10;i++){
Person p = new Person("李明"+i);
//给数组中添加元素
personArr[i-1] = p;
p.showInfo();
}
//需要改国籍
Person.country = "新加坡 ";
for(Person p : personArr){
p.showInfo();
}
for(int i =1 ; i<=10;i++){
Test t = new Test();
}
System.out.println("Demo.count");
}
}