diff --git a/Study.iml b/Study.iml
new file mode 100644
index 0000000..b8db988
--- /dev/null
+++ b/Study.iml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/btp/oneP/ABtpTest.java b/src/btp/oneP/ABtpTest.java
index be23f00..36bdcce 100644
--- a/src/btp/oneP/ABtpTest.java
+++ b/src/btp/oneP/ABtpTest.java
@@ -3,6 +3,8 @@
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
import java.util.Scanner;
public class ABtpTest {
@@ -11,13 +13,17 @@ public static void main(String[] args) {
PrintStream p = System.out;
Scanner sc =new Scanner(System.in);
try{
- ArrayList as = returnArray();
+ /*ArrayList as = returnArray();
ArrayList ass = (ArrayList)as;
p.println(ass);
as = returnArrayNew();
p.println(as);
ass = (ArrayList)as;
- p.println(ass);
+ p.println(ass);*/
+
+ List myIntList = new LinkedList();//1
+ myIntList.add(new Integer(0));//2
+ Integer x = (Integer) myIntList.iterator().next();//3
}catch(Exception e){
e.printStackTrace();
}finally{
diff --git a/src/btp/oneP/ApplyTest.java b/src/btp/oneP/ApplyTest.java
new file mode 100644
index 0000000..08e3eef
--- /dev/null
+++ b/src/btp/oneP/ApplyTest.java
@@ -0,0 +1,93 @@
+package btp.oneP;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+public class ApplyTest {
+
+ public static void main(String[] args) throws NoSuchMethodException, SecurityException {
+ List shapes = new ArrayList();
+ for(int i=0;i<10;i++){
+ shapes.add(new Shape());
+ }
+ Apply.apply(shapes, Shape.class.getMethod("rotate"));
+
+ Apply.apply(shapes, Shape.class.getMethod("resize", int.class), 5);
+
+
+ List squares = new ArrayList();
+ for(int i=0;i<10;i++){
+ squares.add(new Square());
+ }
+ Apply.apply(squares, Square.class.getMethod("rotate"));
+
+ Apply.apply(squares, Square.class.getMethod("resize", int.class), 5);
+
+
+ Apply.apply(new FilledList(Shape.class,5), Shape.class.getMethod("rotate"));
+ Apply.apply(new FilledList(Square.class,18), Shape.class.getMethod("resize", int.class),15);
+
+ SimpleQueue shapeQ = new SimpleQueue();
+ for(int i=0;i<5;i++){
+ shapeQ.add(new Shape());
+ shapeQ.add(new Square());
+ }
+ Apply.apply(shapeQ, Shape.class.getMethod("rotate"));
+ }
+
+}
+
+class Apply{
+ public static > void apply(S seq,Method f,Object...args){
+ try{
+ for(T t:seq){
+ f.invoke(t, args);
+ }
+ }catch(Exception e){
+ throw new RuntimeException();
+ }
+ }
+}
+
+class Shape{
+ public void rotate(){
+ System.out.println(this+" rotate");
+ }
+ public void resize(int newSize){
+ System.out.println(this+" resize"+newSize);
+ }
+}
+
+class Square extends Shape{}
+
+class FilledList extends ArrayList{
+ public FilledList(Class extends T> type,int size){
+ try{
+ for(int i = 0;i implements Iterable{
+ private LinkedList storage = new LinkedList();
+ public void add(T t){
+ this.storage.offer(t);
+ }
+ public T get(){
+ return this.storage.poll();
+ }
+ @Override
+ public Iterator iterator() {
+ return this.storage.iterator();
+ }
+
+}
+
+
diff --git a/src/btp/oneP/ArrayOfGenerics.java b/src/btp/oneP/ArrayOfGenerics.java
new file mode 100644
index 0000000..8ab2dc6
--- /dev/null
+++ b/src/btp/oneP/ArrayOfGenerics.java
@@ -0,0 +1,31 @@
+package btp.oneP;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public class ArrayOfGenerics {
+
+ public static void main(String[] args) {
+ //编译期不能实例化泛型数组,但是可以创建泛型数组的引用
+ List[] ls;
+ List[] la = new List[10];
+ ls = (List[])la;
+ ls[0] = new ArrayList(Arrays.asList("hehe","keke","xixi"));
+ //ls[1] = new ArrayList();
+
+ Object[] objects = ls;
+ objects[1] = new ArrayList(Arrays.asList(1,2,3,4,5));
+ System.out.println(Arrays.toString(la));
+ System.out.println(la.getClass().getName());
+ System.out.println(Arrays.toString(ls));
+ System.out.println(ls.getClass().getName());
+ System.out.println(Arrays.toString(objects));
+ System.out.println(objects.getClass().getName());
+ List[] spheres = (List[])new List[10];
+ for(int i=0;i<10;i++){
+ spheres[i] = new ArrayList();
+ }
+ }
+
+}
diff --git a/src/btp/oneP/ArrayOptions.java b/src/btp/oneP/ArrayOptions.java
new file mode 100644
index 0000000..2686afc
--- /dev/null
+++ b/src/btp/oneP/ArrayOptions.java
@@ -0,0 +1,56 @@
+package btp.oneP;
+
+import java.util.Arrays;
+
+public class ArrayOptions {
+
+ public static void main(String[] args) {
+ BerylliumSphere[] a = null;
+ BerylliumSphere[] b = new BerylliumSphere[5];
+ System.out.println(a);
+ System.out.println(b);
+ System.out.println(Arrays.toString(b));
+
+ BerylliumSphere[] c = new BerylliumSphere[4];
+ for(int i=0;i myset = new HashSet(Arrays.asList(possibles));
+
+ Set myset2 = new HashSet(Arrays.asList(new Byte[]{1,2,3,4,5,6,7,8,9}));
+ //The parameterized method asList(Byte...) of type Arrays is not applicable for the arguments (Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer)
+ //Set myset3 = new HashSet(Arrays.asList(1,2,3,4,5,6,7,8,9));
+}
diff --git a/src/btp/oneP/CRGWithBasicHolder.java b/src/btp/oneP/CRGWithBasicHolder.java
new file mode 100644
index 0000000..d20f854
--- /dev/null
+++ b/src/btp/oneP/CRGWithBasicHolder.java
@@ -0,0 +1,27 @@
+package btp.oneP;
+
+public class CRGWithBasicHolder {
+
+ public static void main(String[] args) {
+ Subtype st1 = new Subtype(),st2 = new Subtype();
+ st1.set(st2);
+ Subtype st3 = st1.get();
+ st1.f();
+ }
+
+}
+
+class BasicHolder{
+ T element;
+ void set(T t){
+ element = t;
+ }
+ T get(){
+ return this.element;
+ }
+ void f(){
+ System.out.println(this.element.getClass().getName());
+ }
+}
+
+class Subtype extends BasicHolder{}
\ No newline at end of file
diff --git a/src/btp/oneP/CaptureConversion.java b/src/btp/oneP/CaptureConversion.java
new file mode 100644
index 0000000..964ae23
--- /dev/null
+++ b/src/btp/oneP/CaptureConversion.java
@@ -0,0 +1,24 @@
+package btp.oneP;
+
+public class CaptureConversion {
+
+ static void f1(Holder holder){
+ T t = holder.get();
+ System.out.println(t.getClass().getSimpleName());
+ }
+
+ static void f2(Holder> holder){
+ f1(holder);
+ }
+ public static void main(String[] args) {
+ Holder raw = new Holder(1);
+ f1(raw);
+ f2(raw);
+ Holder rawBasic = new Holder();
+ rawBasic.set(new Object());
+ f2(rawBasic);
+ Holder> wildcarded = new Holder(1.0);
+ f2(wildcarded);
+ }
+
+}
diff --git a/src/btp/oneP/CheckedList.java b/src/btp/oneP/CheckedList.java
new file mode 100644
index 0000000..cf99872
--- /dev/null
+++ b/src/btp/oneP/CheckedList.java
@@ -0,0 +1,39 @@
+package btp.oneP;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class CheckedList {
+
+ @SuppressWarnings("unchecked")
+ static void oldStyleMethod(List probablyDogs){
+ probablyDogs.add(new Cat());
+ }
+
+ public static void main(String[] args) {
+ List dogs1 = new ArrayList();
+ oldStyleMethod(dogs1);
+ System.out.println(dogs1);
+ Lists2 = Collections.checkedList(new ArrayList(), Dog.class);
+ try{
+ oldStyleMethod(s2);
+ }catch(Exception e){
+ e.printStackTrace();
+ }
+
+ //work fine
+ List pets = Collections.checkedList(new ArrayList(), Pet.class);
+ pets.add(new Dog());
+ pets.add(new Cat());
+ pets.add(new Pet());
+ oldStyleMethod(pets);
+ System.out.println(pets);
+ }
+
+}
+
+class Pet{}
+class Dog extends Pet{}
+class Cat extends Pet{}
+
diff --git a/src/btp/oneP/ClassCasting.java b/src/btp/oneP/ClassCasting.java
new file mode 100644
index 0000000..b80fa8c
--- /dev/null
+++ b/src/btp/oneP/ClassCasting.java
@@ -0,0 +1,26 @@
+package btp.oneP;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.util.List;
+
+public class ClassCasting {
+
+ public void f(String...args) throws FileNotFoundException, IOException, ClassNotFoundException{
+ ObjectInputStream in = new ObjectInputStream(
+ new FileInputStream(args[0]));
+ //List lwl = List.class.ClassCasting(in.readObject());
+ //List lw2 = (List)List.class.cast(in.readObject());
+ List lw2 = List.class.cast(in.readObject());
+ }
+
+ public static void main(String[] args) {
+ // TODO Auto-generated method stub
+
+ }
+
+}
+
+class Widget{}
diff --git a/src/btp/oneP/CollectionDataTest.java b/src/btp/oneP/CollectionDataTest.java
new file mode 100644
index 0000000..6dc1c1e
--- /dev/null
+++ b/src/btp/oneP/CollectionDataTest.java
@@ -0,0 +1,44 @@
+package btp.oneP;
+
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+public class CollectionDataTest {
+
+ public static void main(String[] args) {
+ Set set = new LinkedHashSet(new CollectionData(new Government(),7));
+ set.addAll(CollectionData.list(new Government(), 7));
+ System.out.println(set);
+ }
+
+}
+
+class CollectionData extends ArrayList {
+ public CollectionData(GeneratorN gen,int quantity){
+ for(int i=0;i CollectionData list(GeneratorN gen,int quantity){
+ return new CollectionData(gen,quantity);
+ }
+}
+
+interface GeneratorN{
+ T next();
+}
+
+class Government implements GeneratorN{
+ String[] foundation = "you are a hero not a coward".split(" ");
+ private int index;
+ @Override
+ public String next() {
+ // TODO Auto-generated method stub
+ return foundation[index++];
+ }
+
+}
+
+
diff --git a/src/btp/oneP/CollectionMethod.java b/src/btp/oneP/CollectionMethod.java
new file mode 100644
index 0000000..f4b9778
--- /dev/null
+++ b/src/btp/oneP/CollectionMethod.java
@@ -0,0 +1,53 @@
+package btp.oneP;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+public class CollectionMethod {
+
+ public static void main(String[] args) {
+ Collection c = new ArrayList();
+ c.addAll(Countries.names(6));
+ c.add("ten");
+ c.add("eleven");
+ System.out.println(c);
+
+ Object[] array = c.toArray();
+ String[] str = c.toArray(new String[0]);
+
+ System.out.println(Arrays.toString(array));
+ System.out.println(Arrays.toString(str));
+
+ System.out.println("Collections.max(c) = "+Collections.max(c));
+ System.out.println("Collections.min(c) = "+Collections.min(c));
+
+ Collection c2 = new ArrayList();
+ c2.addAll(Countries.names(6));
+ c.addAll(c2);
+ System.out.println(c);
+ c.remove(Countries.DATA[0][0]);
+ System.out.println(c);
+ c.removeAll(c2);
+ System.out.println(c);
+ c.addAll(c2);
+ System.out.println(c);
+ String val = Countries.DATA[3][0];
+ System.out.println("c.contains("+val+")="+c.contains(val));
+ System.out.println("c.containsAll(c2)="+c.containsAll(c2));
+
+ Collection c3 = ((List)c).subList(3, 5);
+ c2.retainAll(c3);
+ System.out.println(c2);
+ c2.removeAll(c3);
+ System.out.println("c2.isEmpty()="+c2.isEmpty());
+ c = new ArrayList();
+ c.addAll(Countries.names(6));
+ System.out.println(c);
+ c.clear();
+ System.out.println("after c.clear():"+c);
+ }
+
+}
diff --git a/src/btp/oneP/ComparablePet.java b/src/btp/oneP/ComparablePet.java
new file mode 100644
index 0000000..ba87d94
--- /dev/null
+++ b/src/btp/oneP/ComparablePet.java
@@ -0,0 +1,30 @@
+package btp.oneP;
+
+public class ComparablePet implements Comparable {
+
+ @Override
+ public int compareTo(ComparablePet o) {
+ return 0;
+ }
+
+}
+
+//以下是错误的,等于同时两个泛型参数
+//class Cat extends ComparablePet implements Comparable{}
+
+
+class Hamster extends ComparablePet implements Comparable{
+ //其实这就等于重写,子类Hamster重写了父类ComparablePet的方法
+ @Override
+ public int compareTo(ComparablePet o) {
+ return 0;
+ }
+}
+
+//Gecko和Hanster的效果一样
+class Gecko extends ComparablePet{
+ @Override
+ public int compareTo(ComparablePet o) {
+ return 0;
+ }
+}
\ No newline at end of file
diff --git a/src/btp/oneP/ContainerComparison.java b/src/btp/oneP/ContainerComparison.java
new file mode 100644
index 0000000..d0f8bf0
--- /dev/null
+++ b/src/btp/oneP/ContainerComparison.java
@@ -0,0 +1,43 @@
+package btp.oneP;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public class ContainerComparison {
+
+ public static void main(String[] args) {
+ BerylliumSphere[] spheres = new BerylliumSphere[10];
+ for(int i=0;i<5;i++){
+ spheres[i] = new BerylliumSphere();
+ }
+ System.out.println(Arrays.toString(spheres));
+ System.out.println(spheres[4]);
+
+ List sphereList = new ArrayList();
+ for(int i=0;i<5;i++){
+ sphereList.add(new BerylliumSphere());
+ }
+ System.out.println(sphereList);
+ System.out.println(sphereList.get(4));
+
+ int[] integers = {0,1,2,3,4,5};
+ System.out.println(integers);
+ System.out.println(integers[4]);
+
+ List intList = new ArrayList(Arrays.asList(0,1,2,3,4,5));
+ intList.add(97);
+ System.out.println(intList);
+ System.out.println(intList.get(4));
+
+ }
+
+}
+
+class BerylliumSphere{
+ private static long counter;
+ private final long id = counter++;
+ public String toString(){
+ return "Sphere"+id;
+ }
+}
diff --git a/src/btp/oneP/CountingIntegerList.java b/src/btp/oneP/CountingIntegerList.java
new file mode 100644
index 0000000..5eae6da
--- /dev/null
+++ b/src/btp/oneP/CountingIntegerList.java
@@ -0,0 +1,33 @@
+package btp.oneP;
+
+import java.util.AbstractList;
+
+public class CountingIntegerList extends AbstractList{
+
+ public static void main(String[] args) {
+ // TODO Auto-generated method stub
+ //构造函数构造对象时里面还没有值,都是null,在调用toString函数的时候,
+ //toString函数内使用Iterator遍历集合,next()时调用了get(index)
+ //所以是边赋值边打印
+ System.out.println(new CountingIntegerList(30));
+ }
+
+
+ private int size;
+ public CountingIntegerList(int size){
+ this.size = size<0?0:size;
+ }
+
+ @Override
+ public Integer get(int index) {
+ // TODO Auto-generated method stub
+ return Integer.valueOf(index);
+ }
+
+ @Override
+ public int size() {
+ // TODO Auto-generated method stub
+ return size;
+ }
+
+}
diff --git a/src/btp/oneP/CountingMapData.java b/src/btp/oneP/CountingMapData.java
new file mode 100644
index 0000000..23c8df8
--- /dev/null
+++ b/src/btp/oneP/CountingMapData.java
@@ -0,0 +1,61 @@
+package btp.oneP;
+
+import java.util.AbstractMap;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.Set;
+
+public class CountingMapData extends AbstractMap{
+
+ public static void main(String...args){
+ System.out.println(new CountingMapData(60));
+ }
+
+ private int size;
+ private static String[] chars = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".split(" ");
+ public CountingMapData(int size){
+ this.size = size<0?0:size;
+ }
+
+ private static class Entry implements Map.Entry{
+ int index;
+ Entry(int index){
+ this.index = index;
+ }
+ public boolean equals(Object o){
+ return Integer.valueOf(index).equals(o);
+ }
+ @Override
+ public Integer getKey() {
+ // TODO Auto-generated method stub
+ return index;
+ }
+
+ @Override
+ public String getValue() {
+ // TODO Auto-generated method stub
+ return chars[index%chars.length]+Integer.toString(index/chars.length);
+ }
+
+ @Override
+ public String setValue(String value) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int hashCode(){
+ return Integer.valueOf(index).hashCode();
+ }
+ }
+
+ @Override
+ public Set> entrySet() {
+ // TODO Auto-generated method stub
+ Set> entries = new LinkedHashSet>();
+ for(int i=0;i(capitals(3)));
+ System.out.println(new LinkedHashMap(capitals(3)));
+ System.out.println(new TreeMap(capitals(3)));
+ System.out.println(new Hashtable(capitals(3)));
+ System.out.println(new HashSet(names(6)));
+ System.out.println(new LinkedHashSet(names(6)));
+ System.out.println(new TreeSet(names(6)));
+ System.out.println(new ArrayList(names(6)));
+ System.out.println(new LinkedList(names(6)));
+ System.out.println(capitals().get("ALGERIA"));
+ }
+
+ public static final String[][] DATA = {
+ {"ALGERIA","Algiers"},{"CHINA","Beijing"},
+ {"Japan","Tokoy"},{"America","Washington"}
+ };
+ private static class FlyweightMap extends AbstractMap{
+ private static class Entry implements Map.Entry{
+ int index;
+ Entry(int index){
+ this.index = index;
+ }
+ public boolean equals(Object o){
+ return DATA[index][0].equals(o);
+ }
+ @Override
+ public String getKey() {
+ // TODO Auto-generated method stub
+ return DATA[index][0];
+ }
+
+ @Override
+ public String getValue() {
+ return DATA[index][1];
+ }
+
+ @Override
+ public String setValue(String value) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int hashCode(){
+ return DATA[index][0].hashCode();
+ }
+ }
+
+ static class EntrySet extends AbstractSet>{
+ private int size;
+ EntrySet(int size){
+ if(size < 0){
+ this.size = 0;
+ }else if(size > DATA.length){
+ this.size = DATA.length;
+ }else{
+ this.size = size;
+ }
+ }
+ public int size(){
+ return size;
+ }
+
+ @Override
+ public Iterator> iterator() {
+ // TODO Auto-generated method stub
+ return new Iter();
+ }
+
+ private class Iter implements Iterator>{
+ private Entry entry = new Entry(-1);
+ @Override
+ public boolean hasNext() {
+ // TODO Auto-generated method stub
+ return entry.index < size - 1;
+ }
+
+ @Override
+ public java.util.Map.Entry next() {
+ entry.index++;
+ return entry;
+ }
+
+ }
+ }
+
+ private static Set> entries = new EntrySet(DATA.length);
+ @Override
+ public Set> entrySet() {
+ return entries;
+ }
+
+ }
+
+
+
+
+
+
+ static Map select(final int size){
+ return new FlyweightMap(){
+ public Set> entrySet(){
+ return new EntrySet(size);
+ }
+ };
+ }
+
+ static Map map = new FlyweightMap();
+ public static Map capitals(){
+ return map;
+ }
+ public static Map capitals(int size){
+ return select(size);
+ }
+ static List names = new ArrayList(map.keySet());
+
+ public static List names(){
+ return names;
+ }
+ public static List names(int size){
+ return new ArrayList(select(size).keySet());
+ }
+}
diff --git a/src/btp/oneP/Covarance.java b/src/btp/oneP/Covarance.java
new file mode 100644
index 0000000..9fecfcf
--- /dev/null
+++ b/src/btp/oneP/Covarance.java
@@ -0,0 +1,90 @@
+package btp.oneP;
+
+public class Covarance {
+
+ public static void main(String[] args) {
+ // TODO Auto-generated method stub
+ BaseX bx = new BaseX();
+ DerivedX dx = new DerivedX();
+ DerivedSetter d = new DerivedSetter();
+ d.set(dx);
+ d.set(bx);
+ //----------------
+ DerivedGS dgs = new DerivedGS();
+ dgs.set(bx);
+ dgs.set(dx);
+
+ }
+
+}
+
+class BaseX{}
+class DerivedX extends BaseX{}
+
+interface OrdinaryGetter{
+ BaseX get();
+}
+
+interface DerivedGetter extends OrdinaryGetter{
+ DerivedX get();
+}
+
+class CovariantReturnTypes{
+ void test(DerivedGetter d){
+ DerivedX d2 = d.get();
+ }
+}
+
+interface GenericGetter>{
+ T get();
+}
+
+interface Getter extends GenericGetter{
+
+}
+
+class GenericsAndReturnTypes{
+ void test(Getter g){
+ Getter result = g.get();
+ GenericGetter gg = g.get();
+ }
+}
+
+
+class OrdinarySetter{
+ void set(BaseX base){
+ System.out.println("OrdinarySetter.set(BaseX)");
+ }
+}
+
+class DerivedSetter extends OrdinarySetter{
+ //这不是重写,这是重载
+ void set(DerivedX derived){
+ System.out.println("DerivedSetter.set(Derived)");
+ }
+}
+
+interface SelfBoundSetter>{
+ void set(T arg);
+}
+
+interface GSetter extends SelfBoundSetter{}
+
+class SelfBoundingAndCovariantArgument{
+ void testA(GSetter s1,GSetter s2,SelfBoundSetter sbs){
+ s1.set(s2);
+ //s1.set(sbs);
+ }
+}
+
+class GenericSetter{
+ void set(T t){
+ System.out.println("GenericSetter.set(Base)");
+ }
+}
+
+class DerivedGS extends GenericSetter{
+ void set(DerivedX derivedx){
+ System.out.println("DerivedGS.set(Derived)");
+ }
+}
\ No newline at end of file
diff --git a/src/btp/oneP/Decoration.java b/src/btp/oneP/Decoration.java
new file mode 100644
index 0000000..65f7b3c
--- /dev/null
+++ b/src/btp/oneP/Decoration.java
@@ -0,0 +1,67 @@
+package btp.oneP;
+
+import java.util.Date;
+
+public class Decoration {
+
+ public static void main(String[] args) {
+ TimeStamped t = new TimeStamped(new Basic());
+ TimeStamped t2 = new TimeStamped(new SerialNumbered(new Basic()));
+ //t2.getSerialNumber();//Not available
+ System.out.println(t2.getStamp());
+ SerialNumbered s = new SerialNumbered(new Basic());
+ SerialNumbered s2 = new SerialNumbered(new TimeStamped(new Basic()));
+ SerialNumbered s3 = new SerialNumbered(new TimeStamped(new Basic()));
+ System.out.println(s.getSerialNumber());
+ System.out.println(s2.getSerialNumber());
+ System.out.println(s3.getSerialNumber());
+ //s2.getStamp();//Not available
+ }
+
+}
+
+
+class Basic{
+ private String value;
+ public void set(String value){
+ this.value = value;
+ }
+ public String get(){
+ return value;
+ }
+}
+
+class Decorator extends Basic{
+ protected Basic basic;
+ public Decorator(Basic basic){
+ this.basic = basic;
+ }
+ public void set(String val){
+ basic.set(val);
+ }
+ public String get(){
+ return basic.get();
+ }
+}
+
+class TimeStamped extends Decorator{
+ private final long timeStamp;
+ public TimeStamped(Basic basic) {
+ super(basic);
+ timeStamp = new Date().getTime();
+ }
+ public long getStamp(){
+ return timeStamp;
+ }
+}
+
+class SerialNumbered extends Decorator{
+ private static long counter = 1;
+ private final long serialNumber = counter++;
+ public SerialNumbered(Basic basic) {
+ super(basic);
+ }
+ public long getSerialNumber(){
+ return serialNumber;
+ }
+}
\ No newline at end of file
diff --git a/src/btp/oneP/DecoratorTest.java b/src/btp/oneP/DecoratorTest.java
new file mode 100644
index 0000000..6efc792
--- /dev/null
+++ b/src/btp/oneP/DecoratorTest.java
@@ -0,0 +1,163 @@
+package btp.oneP;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class DecoratorTest {
+
+ @SuppressWarnings("static-access")
+ public static void main(String[] args) {
+ OriginalCoffee originalCoffee = new OriginalCoffee();
+ double priceMilk = 10.0;
+ Milk milkCoffee = new Milk(originalCoffee,priceMilk);
+ milkCoffee.mixing();
+ System.out.println("当前咖啡中的配料:"+milkCoffee.foodList);
+ System.out.println("当前咖啡价格:"+milkCoffee.getPrice());
+ System.out.println("---------------------------");
+ double priceMatcha = 20.0;
+ Matcha matchaCoffee = new Matcha(milkCoffee,priceMatcha);
+ matchaCoffee.mixing();
+ System.out.println("当前咖啡中的配料:"+matchaCoffee.foodList);
+ System.out.println("当前咖啡价格:"+matchaCoffee.getPrice());
+ System.out.println("---------------------------");
+ double priceChocolate = 30.0;
+ Chocolate chocolateCoffee = new Chocolate(matchaCoffee,priceChocolate);
+ chocolateCoffee.mixing();
+ System.out.println("当前咖啡中的配料:"+chocolateCoffee.foodList);
+ System.out.println("当前咖啡价格:"+chocolateCoffee.getPrice());
+ System.out.println("---------------------------");
+ }
+
+}
+
+/**
+ * 装饰品和被装饰器的共同接口
+ *
+ */
+interface CoffeeTaste{
+ //返回价格
+ double getPrice();
+ //搅拌咖啡
+ void mixing();
+}
+
+/**
+ * 原始的被装饰器
+ *
+ */
+class OriginalCoffee implements CoffeeTaste{
+ private String name;
+ public OriginalCoffee(){
+ name = "原味咖啡";
+ System.out.println("原味咖啡的价格是:50");
+ }
+ @Override
+ public double getPrice() {
+ //原味咖啡的价钱
+ return 50;
+ }
+
+ @Override
+ public void mixing() {
+ System.out.println("--搅拌咖啡");
+ }
+
+}
+
+/**
+ * 装饰品的共同父类
+ *
+ */
+abstract class SnacksAndDesserts implements CoffeeTaste{
+ public static List foodList= new ArrayList();
+ //小食和甜品的价格
+ private double additionalPrice;
+ //要加入小食和甜品的咖啡
+ CoffeeTaste coffeeTaste;
+ public SnacksAndDesserts(CoffeeTaste coffeeTaste,double additionalPrice){
+ this.coffeeTaste = coffeeTaste;
+ this.additionalPrice = additionalPrice;
+ }
+ //返回价格
+ @Override
+ public double getPrice() {
+ double priceNow = coffeeTaste.getPrice()+additionalPrice;
+ return priceNow;
+ }
+
+ abstract void add();
+}
+
+/**
+ * 牛奶类
+ *
+ */
+class Milk extends SnacksAndDesserts{
+
+ public Milk(CoffeeTaste coffeeTaste, double additionalPrice) {
+ super(coffeeTaste, additionalPrice);
+ System.out.println("O(∩_∩)O~~牛奶的价格是:"+additionalPrice);
+ }
+
+ @Override
+ void add() {
+ System.out.println("--咖啡中加入牛奶");
+ SnacksAndDesserts.foodList.add("牛奶");
+ }
+
+ @Override
+ public void mixing() {
+ add();
+ System.out.println("--牛奶加入咖啡中搅拌");
+ }
+
+
+}
+
+
+/**
+ * 抹茶类
+ */
+class Matcha extends SnacksAndDesserts{
+
+ public Matcha(CoffeeTaste coffeeTaste, double additionalPrice) {
+ super(coffeeTaste, additionalPrice);
+ System.out.println("O(∩_∩)O~~抹茶的价格是:"+additionalPrice);
+ }
+
+ @Override
+ void add() {
+ System.out.println("--咖啡中混入抹茶");
+ SnacksAndDesserts.foodList.add("抹茶");
+ }
+
+ @Override
+ public void mixing() {
+ add();
+ System.out.println("--抹茶加入咖啡中搅拌");
+ }
+}
+
+/**
+ * 巧克力类
+ */
+
+class Chocolate extends SnacksAndDesserts{
+
+ public Chocolate(CoffeeTaste coffeeTaste, double additionalPrice) {
+ super(coffeeTaste, additionalPrice);
+ System.out.println("O(∩_∩)O~~巧克力的价格是:"+additionalPrice);
+ }
+
+ @Override
+ void add() {
+ System.out.println("--咖啡中加入巧克力");
+ SnacksAndDesserts.foodList.add("巧克力");
+ }
+
+ @Override
+ public void mixing() {
+ add();
+ System.out.println("--咖啡加入咖啡中搅拌");
+ }
+}
diff --git a/src/btp/oneP/DogsAndRobots.java b/src/btp/oneP/DogsAndRobots.java
new file mode 100644
index 0000000..ecb0865
--- /dev/null
+++ b/src/btp/oneP/DogsAndRobots.java
@@ -0,0 +1,61 @@
+package btp.oneP;
+
+public class DogsAndRobots {
+
+ public static void main(String[] args) {
+ PerformingDog d = new PerformingDog();
+ Robott r = new Robott();
+ Communicate.perform(d);
+ Communicate.perform(r);
+ }
+
+}
+
+interface Performs{
+ void speak();
+ void sit();
+}
+
+class PerformingDog extends Dog implements Performs{
+
+ @Override
+ public void speak() {
+ System.out.println("Woof!");
+ }
+
+ @Override
+ public void sit() {
+ System.out.println("Sitting!");
+ }
+
+ public void reproduce(){}
+}
+
+class Robott implements Performs{
+
+ @Override
+ public void speak() {
+ System.out.println("Click!");
+ }
+
+ @Override
+ public void sit() {
+ System.out.println("Clank!");
+ }
+
+ public void oilChange(){}
+
+}
+
+class Communicate{
+ public static void perform(T performs){
+ performs.speak();
+ performs.sit();
+ }
+
+ //下面和上面的一样,不构成重载
+ /*public static void perform(Performs p){
+ p.speak();
+ p.sit();
+ }*/
+}
\ No newline at end of file
diff --git a/src/btp/oneP/DynamicProxyMixin.java b/src/btp/oneP/DynamicProxyMixin.java
new file mode 100644
index 0000000..d8385d7
--- /dev/null
+++ b/src/btp/oneP/DynamicProxyMixin.java
@@ -0,0 +1,58 @@
+package btp.oneP;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.HashMap;
+import java.util.Map;
+
+public class DynamicProxyMixin {
+
+ public static void main(String[] args) {
+ }
+
+}
+
+class MixinProxy implements InvocationHandler{
+ Map delegatesByMethod;
+ public MixinProxy(TwoTuple