例2接口实现 interface At int x=1: void method1 0; interface B extends Af int x=2 void method20; public class Example implements B& public void methode System, out. printIn(x="+x); System. out. printIn("A X=+A.) System.out printIn(BX=+ Bx); System. out. printIn(Example x= "+ Example x); public void me thod20t public static void main(Stringll args) Example d= new Example 0; d method10; G carre
例2 接口实现 ⚫ interface A{ ⚫ int x = 1; ⚫ void method1(); ⚫ } ⚫ interface B extends A{ ⚫ int x = 2; ⚫ void method2(); ⚫ } ⚫ public class Example implements B{ ⚫ public void method1(){ ⚫ System.out.println("x = " + x); ⚫ System.out.println("A.x = " + A.x); ⚫ System.out.println("B.x = " + B.x); ⚫ System.out.println("Example.x = " + Example.x); ⚫ } ⚫ public void method2(){ ⚫ } ⚫ public static void main(String[] args){ ⚫ Example d = new Example(); ⚫ d.method1(); ⚫ } ⚫ }
例3接口作为类型 class Worker implements Runnable public void runt System. out. print( Worker run! ) public class server public static void main(Stringll args t Runnable w= new Worker(; (new Thread(w)).start(; } G carre
例3接口作为类型 ⚫ class Worker implements Runnable{ ⚫ public void run(){ ⚫ System.out.print("Worker run!"); ⚫ } ⚫ } ⚫ public class Server{ ⚫ public static void main(String[] args){ ⚫ Runnable w = new Worker(); ⚫ (new Thread(w)).start(); ⚫ } ⚫ }
接口不应改变 修改某个接口,为其添加一个方法,这个简单的 修改可能会造成牵一发而动全身的局面:所有实 现这个接口的类都将无法工作,因为现在他们已 经不再实现这个接口了。你要么放弃对这个接口 的修改,要么连同修改所有实现这个接口的所有 类 在设计接口的最初,预测出接口的所有功能,这 可能是不太现实。如果觉得接口非改不行,可以 创建一个新的接口或者扩展这个接口,算是一种 折衷的解决方法。其他相关的类可以保持不变, 或者重新实现这个新的接口 G car
接口不应改变 ⚫ 修改某个接口,为其添加一个方法,这个简单的 修改可能会造成牵一发而动全身的局面:所有实 现这个接口的类都将无法工作,因为现在他们已 经不再实现这个接口了。你要么放弃对这个接口 的修改,要么连同修改所有实现这个接口的所有 类。 ⚫ 在设计接口的最初,预测出接口的所有功能,这 可能是不太现实。如果觉得接口非改不行,可以 创建一个新的接口或者扩展这个接口,算是一种 折衷的解决方法。其他相关的类可以保持不变, 或者重新实现这个新的接口
4.2抽象类 用的 G carre
4.2 抽象类 什么是抽象 抽象就是抽出事物的本质特性而暂时不考虑 它们的细节。过程抽象和数据抽象是常用的 两种主要抽象手段