70 lines
1.8 KiB
Java
Raw Permalink Normal View History

2015-09-07 11:44:36 -06:00
// generics/Mixins.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2015-06-15 17:47:35 -07:00
import java.util.*;
interface TimeStamped { long getStamp(); }
class TimeStampedImp implements TimeStamped {
private final long timeStamp;
2017-05-01 14:33:10 -06:00
TimeStampedImp() {
2015-06-15 17:47:35 -07:00
timeStamp = new Date().getTime();
}
@Override
public long getStamp() { return timeStamp; }
}
interface SerialNumbered { long getSerialNumber(); }
class SerialNumberedImp implements SerialNumbered {
private static long counter = 1;
private final long serialNumber = counter++;
@Override
public long getSerialNumber() { return serialNumber; }
}
interface Basic {
void set(String val);
String get();
}
class BasicImp implements Basic {
private String value;
@Override
public void set(String val) { value = val; }
@Override
public String get() { return value; }
}
class Mixin extends BasicImp
implements TimeStamped, SerialNumbered {
private TimeStamped timeStamp = new TimeStampedImp();
private SerialNumbered serialNumber =
new SerialNumberedImp();
@Override public long getStamp() {
return timeStamp.getStamp();
}
@Override public long getSerialNumber() {
2015-06-15 17:47:35 -07:00
return serialNumber.getSerialNumber();
}
}
public class Mixins {
public static void main(String[] args) {
Mixin mixin1 = new Mixin(), mixin2 = new Mixin();
mixin1.set("test string 1");
mixin2.set("test string 2");
System.out.println(mixin1.get() + " " +
2016-01-25 18:05:55 -08:00
mixin1.getStamp() + " " +
mixin1.getSerialNumber());
2015-06-15 17:47:35 -07:00
System.out.println(mixin2.get() + " " +
2016-01-25 18:05:55 -08:00
mixin2.getStamp() + " " +
mixin2.getSerialNumber());
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
test string 1 1611503367257 1
test string 2 1611503367258 2
2015-09-07 11:44:36 -06:00
*/