####### 【返 回 目 录】#######
public class School {
public float scoreLine = 400;
public String name = "";
public static double buget = 20000.0;
public School(String name){
this.name = name;
}
// 拷贝构造函数
public School(School s){
this.name = s.name;
this.scoreLine = s.scoreLine;
}
// 拷贝构造函数: 部分拷贝
public School(School s, String name){
this.name = name;
this.scoreLine = s.scoreLine;
}
public static void spend(double money) {
School.buget -= money;
}
public void show() {
System.out.println("School: " + this.name);
System.out.println("\tScore Line: " + this.scoreLine);
System.out.println("\tBuget: " + School.buget);
}
public static void main(String[] args) {
// 静态成员使用
School nfu = new School("NFU");
School tcu = new School("TCU");
School.spend(100);
nfu.show();
School.spend(200);
nfu.show();
tcu.show();
// 拷贝构造函数使用
School nfu1 = new School(nfu);
School nfu2 = new School(nfu, "NFU2");
nfu1.show();
nfu2.show();
// 对象数组使用方法
School [] sch = new School[10];
int i = 1;
for (School sc : sch) {
sc = new School("NFU_" + (i++));
sc.show();
}
}
}
运行结果
School: NFU
Score Line: 400.0
Buget: 19900.0
School: NFU
Score Line: 400.0
Buget: 19700.0
School: TCU
Score Line: 400.0
Buget: 19700.0
School: NFU
Score Line: 400.0
Buget: 19700.0
School: NFU2
Score Line: 400.0
Buget: 19700.0
School: NFU_1
Score Line: 400.0
Buget: 19700.0
School: NFU_2
Score Line: 400.0
Buget: 19700.0
School: NFU_3
Score Line: 400.0
Buget: 19700.0
School: NFU_4
Score Line: 400.0
Buget: 19700.0
School: NFU_5
Score Line: 400.0
Buget: 19700.0
School: NFU_6
Score Line: 400.0
Buget: 19700.0
School: NFU_7
Score Line: 400.0
Buget: 19700.0
School: NFU_8
Score Line: 400.0
Buget: 19700.0
School: NFU_9
Score Line: 400.0
Buget: 19700.0
School: NFU_10
Score Line: 400.0
Buget: 19700.0
####### 【返 回 目 录】#######