您的位置:首页 > 其它

基础加强一(枚举)

2013-06-26 23:31 78 查看
IDE (集成开发环境) Integrition development environment

jndi

jms

Eclipse

版本设置:

编译的JDK版本

运行的JDK版本

自动提示设置

Proferences-->General-->Keys-Search(Context assist) 设置就成

Integer 与Int相互拆装箱的时候 如果在-128-127时。不会相互的值不会影响。如果超出这个值的话,拆装箱对象所指向的常量池的值不同

享元模式(flyweight)

如果很多很小的对象,有很多相同的东西,把他变成一个对象,把不同的对象变成属性,变成参数传进去,称之为外部状态,这就是享元模式

枚举

枚举内部类:

如果你想在一个类中编写各个枚举类和测试调用类,那么可以将枚举类定义成调用类的内部类

枚举类原理:

import java.io.File;

public abstract class Demo30 {

public static final Demo30 D1 = new Demo30() {

public Demo30 next() {
return D2;
};
};

public static final Demo30 D2 = new Demo30() {
public Demo30 next() {

return D3;

};
};

public static final Demo30 D3 = new Demo30() {
public Demo30 next() {
return D4;
};
};

public static final Demo30 D4 = new Demo30() {
public Demo30 next() {
return D5;
};
};

public static final Demo30 D5 = new Demo30() {
public Demo30 next() {
return D1;
};
};

private Demo30() {
};

public abstract Demo30 next();

// public Demo30 next() {
//
// if (this == D1) {
// return D2;
// } else if (this == D2) {
// return D3;
// } else if (this == D3) {
// return D4;
// } else if (this == D4) {
// return D5;
// } else {
// return D1;
// }
//
// }

public String toString() {

return this == D1 ? "D2" : "D3";
}

}


枚举对象的使用:

注意:有枚举对象..必须最前面,最后一个加分号结束;

public enum Session {
request, response, context, content;

}

public enum color {

RED() {

@Override
public Demo31.color selectColor(int time) {
System.out.println("  " + time + "秒后显示" + BLAUE);
return BLAUE;
}
},
BLAUE {
@Override
public color selectColor(int time) {
System.out.println("  " + time + "秒后显示" + YELLOW);
return YELLOW;
}
},
YELLOW {
@Override
public color selectColor(int time) {
System.out.println("  " + time + "秒后显示" + WHITE);
return WHITE;
}
},
WHITE {
@Override
public color selectColor(int time) {
System.out.println("  " + time + "秒后显示" + RED);
return RED;
}
};

private void color() {
};

private void color(int time) {
};

public abstract color selectColor(int time);

public color next() {
switch (this) {
case RED:
return BLAUE;
case BLAUE:
return YELLOW;
case YELLOW:
return WHITE;
case WHITE:
return RED;
}
return RED;

}

}

public static void main(String[] args) {
color c = color.RED.selectColor(10);
}


switch enum的使用

使用心德:

enum的switch一般使用在enum中的方法通过switch(this)来统一管理传回来的对象并返回相应的值

通过switch(this){

case 对象:break;

case 对象:break;

case 对象:break;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: