Java Learning(7)
接口
Tips:
不是类,是对类的需求描述。
如果类遵从某个特定接口,就履行某服务。
实现一个接口: 实现接口中定义的方法均为public,并且需要在实现类中注明public,以避免编译器试图改变为更弱的访问权限而带来的警告。
特性:
接口不能实例化,但却可以声明接口变量且该变量必须引用实现了对应接口的类的对象。
接口可以通过instanceof来检查某对象是否实现了某个特定的接口。
接口可以扩展已有接口。
接口中可以定义常量(public static final)。
单继承但可以实现多个接口。所以如果一个类在继承的时候只能extends一个抽象类但却可以实现多个接口。变相的实现多继承的功能,去除了多继承带来的复杂性和低效率。
多继承表明继承类可以扮演多个角色,而实现多个接口可以说实现类可以具有多个角色的功能。
对象拷贝和克隆:
变量拷贝是引用的拷贝。
在对象拷贝过程中,被拷贝对象如果有第三方引用,则拷贝者只拷贝当前对象,而被被拷贝者所引用的部分不进行拷贝(浅拷贝),仍然是公用,以引用的方式存在。但这会造成很多问题,因此需要重新定义clone方法达到对引用对象的克隆(深拷贝)。运用clone方法需要实现Cloneable接口,否则会产生checked exception。
注意:必须实现Cloneable,否则会抛出CloneNotSupportedException。
Demo:
1 class entry: 2 3 public class Entry { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 8 eggSample es = CloneSample.es; 9 es.setFrom(new factory("factory1","Shanghai PudongStrict NO.1")); 10 11 try { 12 eggSample es1 = es.deepclone(); 13 es1.getfrom().setName("factory2"); 14 15 System.out.println(es.getfrom().getName()); 16 17 eggSample es2 = es.shallowclone(); 18 es2.getfrom().setName("factory2"); 19 20 System.out.println(es.getfrom().getName()); 21 } catch (CloneNotSupportedException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } 25 } 26 } 27 28 class eggsample: 29 30 public class eggSample implements Cloneable { 31 private String color; 32 private String name; 33 private String data; 34 private factory from; 35 36 public eggSample(String color, String name, String data) { 37 this.color = color; 38 this.name = name; 39 this.data = data; 40 } 41 42 public String getColor() { 43 return this.color; 44 } 45 46 public String getName() { 47 return this.name; 48 } 49 50 public String getdata() { 51 return this.data; 52 } 53 54 public factory getfrom() { 55 return this.from; 56 } 57 58 public eggSample shallowclone() throws CloneNotSupportedException { 59 return (eggSample) super.clone(); 60 } 61 62 // deep copy 63 public eggSample deepclone() throws CloneNotSupportedException { 64 eggSample estempt = (eggSample) super.clone(); 65 estempt.from = (factory) from.clone(); 66 return estempt; 67 } 68 69 public void setInfo(String colorS, String nameS, String dataS) { 70 this.color = colorS; 71 this.name = nameS; 72 this.data = dataS; 73 } 74 75 public void setFrom(factory factoryS) { 76 this.from = factoryS; 77 } 78 79 public String toString() { 80 return "egg is coming from : " + from.toString() + "\n\tProperties:" 81 + "\n\t Color: " + this.color + "\n\t Name: " + this.name 82 + "\n\t Data: " + this.data; 83 } 84 } 85 86 class factory: 87 88 public class factory implements Cloneable{ 89 90 String factoryname; 91 String factorylocation; 92 93 public factory(String name, String location){ 94 this.factorylocation = location; 95 this.factoryname = name; 96 } 97 98 public String getName(){ 99 return this.factoryname; 100 } 101 102 public String getLocation(){ 103 return this.factorylocation; 104 } 105 106 public String toString(){ 107 return "Factory :" + this.factoryname + "\t" + this.factorylocation ; 108 } 109 110 public void setName(String name){ 111 this.factoryname = name; 112 } 113 114 public void setLocation(String Location){ 115 this.factorylocation = Location; 116 } 117 118 public factory clone() throws CloneNotSupportedException { 119 return (factory) super.clone(); 120 } 121 } 122 class clone Sample: 123 124 public class CloneSample implements Cloneable { 125 public static eggSample es = new eggSample("red", "egg1", "20150930"); 126 }
Result:
factory1
factory2
接口和回调:
特定情况下传对象优于传递函数,因为对象作为传递对象可以携带很多其他相关信息,比方法传递要灵活。
System.exit(int status) --> Runtime.exit(int status):
0 - 将虚拟机中的所有内容都停掉,内存清空,JVM停止;
1 or <>0 - 非正常退出,通常放在catch模块中用于终止当前程序;
但后期需要进一步学习关于exist本身发生的时候JVM层面上的事情。
和Return的区别,Return是返回上一层,二exist则是放回到最外层;
CallBack && Hook:
回调: 程序设计模式,指出某个特定事件发生时应该采取的动作。个人感觉Hook也是这么回事,就是在特定事件出发时候关联某些行为的设计方法。
比如说在程序执行到需要shutdown之前弹出对话框什么之类的。
内部类:
原因:某类的内部类可以访问该类中所有的作用域中的数据;其次内部类可以通过权限控制只对该类可见;还有就是在该类中需要定义一个回调函数但是不想便携大量的代码时,可以使用匿名内部类。
Demo:
public class TalkingClock {
private int interval;
private boolean beep;
public TalkingClock(int interval, boolean beep){
this.interval = interval;
this.beep = beep;
}
public void start(){
ActionListener listener = new TimePrinter();
Timer t = new Timer(interval, listener);
t.start();
}
public class TimePrinter implements ActionListener{
@Override
public void actionPerformed(ActionEvent event) {
// TODO Auto-generated method stub
Date now = new Date();
System.out.println("At the tone, the time is " + now);
if(beep)
Toolkit.getDefaultToolkit().beep();
}
}
内部类中的beep是默认从主类中读取的。
内部匿名类:直接创建实现某个接口或者继承某个父类的类的实例。ActionListener是一个接口。匿名类不能有构造器,构造器需要的参数传递给父类的构造器。
ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent envent){
.......
}
};
//format type: SuperType can be class or interface
new SuperType(constructor parameter){
innerClass's method or data.
}

浙公网安备 33010602011771号