抽象方法与接口
来源:互联网 发布:机打收据软件 编辑:程序博客网 时间:2024/06/01 23:16
抽象类不能实例化,而且必须被子类继承,子类需要实现所有的方法。
接口是一个特殊的类,由抽象方法和全局常量组成(public static final)
适配器设计模式
interface Window{
public void open() ; // 打开窗口
public void close() ; // 关闭窗口
public void icon() ; // 最小化
public void unicon() ; // 最大化
}
abstract class WindowAdapter implements Window{
public void open(){}
public void close(){}
public void icon(){}
public void unicon(){}
};
class MyWindow extends WindowAdapter{
public void open(){
System.out.println("打开窗口!") ;
}
};
public class AdpaterDemo{
public static void main(String args[]){
Window win = new MyWindow() ;
win.open() ;
}
}
工厂设计模式:
interface Fruit{
public void eat() ;
}
class Apple implements Fruit{
public void eat(){
System.out.println("吃苹果。。。") ;
}
};
class Orange implements Fruit{
public void eat(){
System.out.println("吃橘子。。。") ;
}
};
class Factory{ // 工厂类
public static Fruit getFruit(String className){
Fruit f = null ;
if("apple".equals(className)){
f = new Apple() ;
}
if("orange".equals(className)){
f = new Orange() ;
}
return f ;
}
};
public class InterDemo{
public static void main(String args[]){
Fruit f = Factory.getFruit(args[0]) ;
if(f!=null){
f.eat() ;
}
}
}
代理设计模式:
interface Window{
public void open() ; // 打开窗口
public void close() ; // 关闭窗口
public void icon() ; // 最小化
public void unicon() ; // 最大化
}
abstract class WindowAdapter implements Window{
public void open(){}
public void close(){}
public void icon(){}
public void unicon(){}
};
class MyWindow extends WindowAdapter{
public void open(){
System.out.println("打开窗口!") ;
}
};
public class AdpaterDemo{
public static void main(String args[]){
Window win = new MyWindow() ;
win.open() ;
}
}
抽象类与接口的比较:(重点)
比较点
抽象类
接口
组成
抽象方法、普通方法、常量、变量、构造器、全局变量
抽象方法、全局常量
限制
只能继承一个抽象类
一个子类可以实现多个接口
关系
一个抽象类中可以包含多个接口
一个接口中可以包含多个抽象类
实例化
都是通过对象的多态性,通过子类进行对象的实例化操作
实现限制
只能单继承
可以实现多个
特征
-
表示一个标准、一种能力
- 抽象方法与接口
- 抽象方法与接口
- 抽象类与抽象方法,接口
- 抽象类与抽象方法和接口
- 关于抽象方法与接口
- 抽象类和接口 与 抽象方法和虚方法
- 抽象方法与接口的区分
- 接口与抽象方法的区别
- JavaSE0012_抽象类与方法和接口
- 黑马程序员--抽象方法与接口
- PHP面向对象:接口与抽象方法
- 抽象类与接口的构造方法
- C#类、接口、虚方法和抽象方法-抽象类与接口的区别与联系
- Java中接口的定义与实现接口抽象方法
- Java中接口的定义与实现接口抽象方法
- 接口、抽象类、抽象方法
- C#类、接口、虚方法和抽象方法-非抽象类与接口的差别
- C#类、接口、虚方法和抽象方法-接口与抽象类的区别实例
- qlikview的整体操作
- NAnt与MSBuild使用(一)
- redo 和 undo 之四
- linux串口编程 非规范模式 read()问题
- linux程序调试补充(gdb ) (2)
- 抽象方法与接口
- win7+cuda4.1+VS2008配置
- 《Linux设备驱动程序》学习2—高级字符设备驱动ioctl
- javascript 怎样才能确定参数变量的个数呢?
- C++之面向对象
- typedef
- js中的var(声明局部变量)
- AIDL和远程Service调用
- Oracle ORA-00600: [dmlsrvColLenChk_2:dty] 错误 说明