[Java]_ch8.6_匿名內部類別
public class A7 {
public static void main(String[] args) {
// 人類.showNum();
人類 亞當=new 人類();
亞當.showNum();
人類 夏娃=new 人類(111,0);
夏娃.showNum();
人類 張三=new 人類(20,30);
張三.showNum();
人類 李四=new 人類(88,99,66);
李四.showNum();
人類 王五=new 人類(32,22,11,44);
王五.showNum();
亞當.showNum();
// System.out.println("亞當.眼="+亞當.眼+" 耳="+亞當.耳+" 口="+亞當.口+" 鼻="+亞當.鼻);
// System.out.println("夏娃.眼="+夏娃.眼+" 耳="+夏娃.耳+" 口="+夏娃.口+" 鼻="+夏娃.鼻);
亞當.show("亞當");
夏娃.show("夏娃");
// 亞當.眼=4;
// 亞當.耳=-2;
// 亞當.口=1;
// 亞當.鼻=-1;
亞當.set(4, 2, 1, 1, "亞當");;
夏娃.set(8, -6, 4, 2, "夏娃");
亞當.set(100, "亞當");
亞當.set(100,200, "亞當");
亞當.set(100,200,300, "亞當");
亞當.set(100,200,300,400, "亞當");
亞當.compare(王五);
人類 derek=亞當;
derek.compare(亞當);
人類 Jerry=王五.compareR(張三);
王五.compare(Jerry);
人類 H[]=new 人類[10];
for(int i=0;i<H.length;i++) {
H[i]=new 人類(1+i,2-i,3*i,4/(i+1));
}
人類 Human[]=new 人類[H.length];
Human=H[5].compareArray(H);
Human[9].compare(Human[4]);
(new 人類() {
public void setNum(int n) {
num=n;
System.out.println("匿名內部類別補充方法setNum(int n),num="+num);
}
}).setNum(999999999);
}
static class 人類{
private int 眼;
private int 耳;
private int 口;
private int 鼻;
static int num;
public 人類() {
num++;
System.out.println("0.人類建構元()啟動,眼="+眼+" 耳="+耳+" 口="+口+" 鼻="+鼻);
}
private 人類(int m) {
num++;
眼=m;
System.out.println("1.人類建構元()啟動,眼="+眼+" 耳="+耳+" 口="+口+" 鼻="+鼻);
}
public 人類(int m,int n) {
num++;
眼=m;
耳=n;
System.out.println("2.人類建構元()啟動,眼="+眼+" 耳="+耳+" 口="+口+" 鼻="+鼻);
}
public 人類(int m,int n,int o) {
num++;
眼=m;
耳=n;
口=o;
System.out.println("3.人類建構元()啟動,眼="+眼+" 耳="+耳+" 口="+口+" 鼻="+鼻);
}
public 人類(int m,int n,int o,int p) {
num++;
眼=m;
耳=n;
口=o;
鼻=p;
System.out.println("4.人類建構元()啟動,眼="+眼+" 耳="+耳+" 口="+口+" 鼻="+鼻);
}
public void showNum() {
System.out.println("總共生成"+num+"個人類物件");
}
public void compare(人類 human) {
if(this==human)
System.out.println("同一人");
else
System.out.println("不同人");
}
public 人類 compareR(人類 human) {
int m =this.眼+this.耳+this.口+this.鼻;
int n =human.眼+human.耳+human.口+human.鼻;
if(m>n) {
System.out.println("this資料成員較大,眼="+this.眼+" 耳="+this.耳+" 口="+this.口+" 鼻="+this.鼻);
return this;
}else {
System.out.println("human資料成員較大,眼="+human.眼+" 耳="+human.耳+" 口="+human.口+" 鼻="+human.鼻);
return human;
}
}
void 吃() {
System.out.println("人類吃的方法呼叫");
}
void 睡() {
System.out.println("人類睡的方法呼叫");
}
public void show(String name) {
// String 眼="我是區域變數眼睛!";
System.out.println(name+".眼="+眼+" 耳="+耳+" 口="+口+" 鼻="+鼻);
// System.out.println(name+"this.眼="+this.眼+" 耳="+this.耳+" 口="+this.口+" 鼻="+this.鼻);
}
public void set(int m,String name) {
if(m<0 )
System.out.println("很抱歉!數值不可為負數!");
else {
this.眼=m;
}
show("1."+name);
}
public void set(int m,int n,String name) {
if(m<0 || n<0 )
System.out.println("很抱歉!數值不可為負數!");
else {
this.眼=m;
耳=n;
}
show("2."+name);
}
public void set(int m,int n,int o,String name) {
if(m<0 || n<0 || o<0 )
System.out.println("很抱歉!數值不可為負數!");
else {
this.眼=m;
耳=n;
口=o;
}
show("3."+name);
}
public void set(int m,int n,int o,int p,String name) {
if(m<0 || n<0 || o<0 || p<0)
System.out.println("很抱歉!數值不可為負數!");
else {
this.眼=m;
耳=n;
口=o;
鼻=p;
}
show("4."+name);
}
public 人類[] compareArray(人類 H[]) {
for(int i=0;i<H.length;i++) {
H[i].眼+=100;
H[i].耳+=50;
H[i].口+=20;
H[i].鼻+=10;
System.out.println("H["+i+"]物件資料成員,眼="+H[i].眼+" 耳="+H[i].耳+" 口="+H[i].口+" 鼻="+H[i].鼻);
}
return H;
}
}
}
沒有留言:
張貼留言