- 单例模式 包装类:注意源码中的缓存设计
- 枚举:底层使用的是常量属性,有序号ordinary和名字name
- 枚举:底层使用的是常量属性,有序号ordinary和名字name 模板方法设计模式:模板方法设计模式:枚举:底层使用的是常量属性,有序号ordinary和名字name枚举:底层使用的是常量属性,有序号ordinary和名字name 模板方法设计模式:模板方法设计模式:枚举:底层使用的是常量属性,有序号ordinary和名字name
- 模板方法设计模式:父类提供算法模板骨架 子类实现某些具体的步骤
/**
* 单例模式
* @author mason 1.在类中自己实例化对象
* 2.将构造方法改变为私有,防止外部调用
* 3.对外提供统一静态方法,去获取该实例
*/
public class SingletonDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayUtil.getInstance().sort();
Integer intNum = new Integer(13);
}
}
class ArrayUtil {
private static ArrayUtil arrayUtil = new ArrayUtil();
private ArrayUtil() {
};
public static ArrayUtil getInstance() {
return arrayUtil;
}
public void sort() {
System.out.println("排序方法");
}
}
/**
* 包装中的缓存设计
* 注意看源码
* @author mason
* ==:引用:判断内存地址是否一样。基本数据类型:判断值是否相同 Integer有效范围:-128,127是相同的
* equeals:判断值是否相同
* 判断引用类型其值是否相同:用equals
*/
class CacheInteger {
CacheInteger judgeValue = new CacheInteger();
Integer int1 = new Integer(123);
Integer int2 = new Integer(123);
Integer int3 = Integer.valueOf(123);
Integer int4 = Integer.valueOf(123);
Integer int5 = Integer.valueOf(253);
Integer int6 = Integer.valueOf(253);
public void printOver() {
judgeValue.judge(int1, int2);// false
judgeValue.judge(int3, int4);// true
judgeValue.judge(int5, int6);//true
}
public boolean judge(Integer int1, Integer int2) {
return int1 == int2;
}
}
抽象方法抽象类
抽象方法:要求必须被实现
抽象类:抽象类可以有抽象方法和普通方法
接口必须全部是抽象方法
模板方法设计模式:
概念:父类提供算法模板骨架 子类实现某些具体的步骤
模板类中用final修饰不可变的部分,protected abstract修饰需要被子类继承的方法
public class EnumDemo {
public static void main(String args[]) {
//枚舉名稱
System.out.println(Weekday.Monday.name());
//枚舉类型的序号
System.out.println(Weekday.Monday.ordinal());
//返回枚举类型
System.out.println(Weekday.values());
//把字符串变为枚举常亮
System.out.println(Weekday.valueOf("Tuesday"));
}
}
enum Weekday {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday;
}
/**返回值
Monday
0
[Lcom.day01.Weekday;@424c0bc4
Tuesday