首页 > 代码库 > Java & 混型

Java & 混型

1.C++ & 混型

C++能够记住其模板参数的类型,对于混型来说,使用参数化类型更加优雅。

#include <string>#include <ctime>#include <iostream>using namespace std;template<class T> class TimeStamped : public T {  long timeStamp;public:  TimeStamped() { timeStamp = time(0); }  long getStamp() { return timeStamp; }};template<class T> class SerialNumbered : public T {  long serialNumber;  static long counter;public:  SerialNumbered() { serialNumber = counter++; }  long getSerialNumber() { return serialNumber; }};// Define and initialize the static storage:template<class T> long SerialNumbered<T>::counter = 1;class Basic {  string value;public:  void set(string val) { value =http://www.mamicode.com/ val; }  string get() { return value; }};    int main() {  TimeStamped<SerialNumbered<Basic> > mixin1, mixin2;    //这就是我想要的  mixin1.set("string1");  mixin2.set("string2");  cout << mixin1.get() << " " << mixin1.getStamp() << " " << mixin1.getSerialNumber() << endl;  cout << mixin2.get() << " " << mixin2.getStamp() << " " << mixin2.getSerialNumber() << endl;}

输出:

string1 1129840250 1string2 1129840250 2

 

二。Java & 混型

Java由于擦除会忘记基类类型。因此泛型类不能直接继承一个泛型参数。

1.使用接口产生混型

 1 interface TimeStamped {  2   long getStamp();  3 } 4  5  6 interface SerialNumbered { 7     long getSerialNumber();  8 } 9 10 11 interface Basic {12   public void set(String val);13   public String get();14 }

实现接口

 1 class TimeStampedImp implements TimeStamped { 2   private final long timeStamp; 3   public TimeStampedImp() { 4     timeStamp = new Date().getTime(); 5   } 6   public long getStamp() { return timeStamp; } 7 } 8  9 10 class SerialNumberedImp implements SerialNumbered {11   private static long counter = 1;12   private final long serialNumber = counter++;13   public long getSerialNumber() { return serialNumber; }14 }15 16 17 18 class BasicImp implements Basic {19   private String value;20   public void set(String val) { value =http://www.mamicode.com/ val; }21   public String get() { return value; }22 }

使用

 1 class Mixin extends BasicImp implements TimeStamped, SerialNumbered {    2   private TimeStamped timeStamp = new TimeStampedImp(); 3   private SerialNumbered serialNumber =  new SerialNumberedImp(); 4  5   public long getStamp() { return timeStamp.getStamp(); }     //代理 6   public long getSerialNumber() {                             //代理 7     return serialNumber.getSerialNumber(); 8   } 9 }10 11 public class Mixins {12   public static void main(String[] args) {13     Mixin mixin1 = new Mixin(); 14     Mixin mixin2 = new Mixin();15     mixin1.set("string1");16     mixin2.set("string2");17     System.out.println(mixin1.get() + " " + mixin1.getStamp() +  " " + mixin1.getSerialNumber());18     System.out.println(mixin2.get() + " " + mixin2.getStamp() +  " " + mixin2.getSerialNumber());19   }20 }

 

2.使用装饰器模式

 1 class Basic { 2   private String value; 3   public void set(String val) { value =http://www.mamicode.com/ val; } 4   public String get() { return value; } 5 } 6  7 class Decorator extends Basic { 8   protected Basic basic; 9   public Decorator(Basic basic) { this.basic = basic; }10   public void set(String val) { basic.set(val); }11   public String get() { return basic.get(); }12 }    13 14 class TimeStamped extends Decorator {15   private final long timeStamp;16   public TimeStamped(Basic basic) {17     super(basic);18     timeStamp = new Date().getTime();19   }20   public long getStamp() { return timeStamp; }21 }22 23 class SerialNumbered extends Decorator {24   private static long counter = 1;25   private final long serialNumber = counter++;26   public SerialNumbered(Basic basic) { super(basic); }27   public long getSerialNumber() { return serialNumber; }28 }    29 30 public class Decoration {31   public static void main(String[] args) {32     TimeStamped t = new TimeStamped( new Basic() );33     TimeStamped t2 = new TimeStamped( new SerialNumbered( new Basic() ));34     //! t2.getSerialNumber(); // Not available35     SerialNumbered s = new SerialNumbered(new Basic());36     SerialNumbered s2 = new SerialNumbered( new TimeStamped( new Basic() ));37     //! s2.getStamp(); // Not available38   }39 }

局限性:

     可以添加多个修饰层,但最后一层才是有效的。

 

3.动态代理