@mSolo
2015-04-25T14:15:36.000000Z
字数 6216
阅读 1681
Java
public
、private
、protected
和 包访问权限
所有优秀的作者,包括那些编写软件的程序员,都清楚其著作的某些部分直至重新创作的时候才变得完美,有时甚至要反复重写多次。……,这正是重构的原动力之一。
- 如何把变动的事物与保持不变的事物区分开来?
- 多态通过分离做什么和怎么做,从另一角度将接口和实现分离开来。
- "实现隐藏"则通过将细节"私有化"把接口和实现分离开来。
- 多态是一项"将改变的事物与未变的事物分离开来"的重要技术。
class Glyph {
void draw() { print("Glyph.draw()"); }
Glyph() {
print("Glyph() before draw()");
draw();
print("Glyph() after draw()");
}
class RoundGlyph extends Glyph {
print int radius = 1;
RoundGlyph(int r) {
radius = r;
print("RoundGlyph.RoundGlyph(), radius = " + radius);
}
void draw() {
print("RoundGlyph.draw(), radius = " + radius);
}
}
public class PolyConstructors {
public static void main(String[] args) {
new RoundGlyph(5);
}
}
} /* output:
Glyph() before draw()
RoundGlyph.draw(), radius = 0
Glyph() after draw()
RoundGlyph.RoundGlyph(), radius = 5
*/
初始化的实际过程是:
1). 在其他任何事情发生之前,将分配给对象的存储空间初始化成二进制的零;
2). 调用基类构造器。此时,调用被覆盖后的draw()方法,由于步骤1)
的缘故,此时 radius 为 0;
3). 按照声明的顺序调用成员的初始化方法;
4). 调用导出类的构造器主体;
8.4 协变返回类型
8.5 用继承进行设计
接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法
抽象类,普通类与接口之间的一种中庸之道
interface Service {
void method1();
void method2();
}
interface ServiceFactory {
Service getService();
}
class Implementation1 implements Service {
private Implementation1() {}
public void method1() {print("Implementation1 method1");}
public void method2() {print("Implementation1 method2");}
public static ServiceFactory factory =
new ServiceFactory() {
public Service getService() {
return new Implementation1();
}
};
}
class Implementation2 implements Service {
private Implementation2() {}
public void method1() {print("Implementation2 method1");}
public void method2() {print("Implementation2 method2");}
public static ServiceFactory factory =
new ServiceFactory() {
public Service getService() {
return new Implementation2();
}
};
}
public class Factories {
public static void serviceConsumer(ServiceFactory fact) {
Service s = fact.getService();
s.method1();
s.method2();
}
public static void main(String[] args) {
serviceConsumer(Implementation1.factory);
// Implementations are completely interchangeable:
serviceConsumer(Implementation2.factory);
}
}
10.7 嵌套类
如果不需要内部类对象与其外围类之间有联系,那么可以将内部类声明为static,这通常称为嵌套类
main()
方法)10.8 为什么需要内部类
1). 内部类提供了某种进入其外围类的窗口
2). 接口解决了部分问题,而内部类有效地实现了"多重继承",即内部类允许继承多个非接口类型(类或抽象类)
3). 更多特性: 内部类可以有多个实例,可以让多个内部类以不同方式实现同一个接口或继承同一个类
package innerclasses.controller;
import java.util.*;
public class Controller {
private List<Event> eventList = new ArrayList<Event>();
public void addEvent(Event c) { eventList.add(c); }
public void run() {
while(eventList.size() > 0)
// Make a copy so you’re not modifying the list while you’re selecting the elements in it:
for(Event e : new ArrayList<Event>(eventList))
if(e.ready()) {
System.out.println(e);
e.action();
eventList.remove(e);
}
}
}
import innerclasses.controller.*;
public class GreenhouseControls extends Controller {
private boolean light = false;
public class LightOn extends Event {
public LightOn(long delayTime) { super(delayTime); }
public void action() {
// Put hardware control code here to physically turn on the light.
light = true;
}
public String toString() { return "Light is on"; }
}
public class LightOff extends Event {
public LightOff(long delayTime) { super(delayTime); }
public void action() {
// Put hardware control code here to
// physically turn off the light.
light = false;
}
public String toString() { return "Light is off"; }
}
// ...
}
import innerclasses.controller.*;
public class GreenhouseController {
public static void main(String[] args) {
GreenhouseControls gc = new GreenhouseControls();
// Instead of hard-wiring, you could parse configuration information from a text file here:
gc.addEvent(gc.new Bell(900));
Event[] eventList = {
gc.new ThermostatNight(0),
gc.new LightOn(200),
gc.new LightOff(400),
gc.new WaterOn(600),
gc.new WaterOff(800),
gc.new ThermostatDay(1400)
};
gc.addEvent(gc.new Restart(2000, eventList));
if(args.length == 1)
gc.addEvent(
new GreenhouseControls.Terminate(
new Integer(args[0])));
gc.run();
}
}
10.9 内部类的继承
class WithInner {
class Inner {}
}
public class InheritInner extends WithInner.Inner {
//! InheritInner() {} // Won’t compile
InheritInner(WithInner wi) {
wi.super();
}
public static void main(String[] args) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
}
10.10 内部类可以被覆盖吗
10.11 局部内部类
10.12 内部类标识符: $