12-16面向对象之接口和抽象类的应用
来源:互联网 发布:java过滤特殊字符xss 编辑:程序博客网 时间:2024/06/07 05:54
1.抽象类和接口实例化
在java中可以通过对象的多态性,为抽象类和接口进行实例化操作,这样再使用抽象类和接口的时候,就可以使用子类的中覆写的方法来实现。
之所以抽象类和接口类不能直接实例化,是因为内部包含了各个抽象方法,抽象方法但都是未实现的方法,无法调用。通过多态性,子类发生向上转型,所调用的全部方法,都是被覆写过的方法。
//本程序是对抽象类和接口继续实例化的操作abstract class A//定义抽象类{public abstract void printA();//定义抽象方法s}interface B//定义接口{public abstract void printB();//定义抽象方法}class C extends A implements B{public void printA()//覆写抽象类中的方法{System.out.println("这是抽象类A的方法");}public void printB()//覆写接口中的方法{System.out.println("这是接口B的方法");}}public class Test06 {public static void main(String[] args) {A a = new C();//实例化子类对象,并向上传递B b = new C();a.printA();//调用抽象类的方法b.printB();//调用接口的方法}}
如果要使用抽象类和接口,只能按照以上操作。
2.抽象类的应用——定义模版
//本程序是对抽象类——定义模版的操作abstract class Person{private String name ;private int age ;public Person(String name , int age ){this.name = name ;this.age = age ;}public String getName(){return this.name ;}public int getAge(){return this.age ;}public void say()//说话是一个具体的功能{System.out.println(this.getInfo());}public abstract String getInfo();//内容又子类决定}class Student extends Person{private String school ;public Student(String name , int age , String school){super(name , age);this.school = school ;}public String getInfo()//覆写{return "姓名:" + this.getName() + ",年龄:" + this.getAge() + ",学校:" +this.school; }}class Worker extends Person{private int salary ; public Worker(String name , int age , int salary){super(name , age);this.salary = salary ;}public String getInfo()//覆写{return "姓名:" + this.getName() + ",年龄:" + this.getAge() + ",工资:" +this.salary; }}public class Test06 {public static void main(String[] args) {Student s = new Student("ss",22,"dsad");Worker wor = new Worker("dd",33,100);Person per1 = s;Person per2 = wor;per1.say();per2.say();}}
此时要举一反三生活中的模版设计,其本质核心:给出了设计中的框架,需要不同应用对象就其框架添加东西。
3.接口的实际引用——制定标准
//本程序是接口制定标准的操作interface USB{public abstract void start();public abstract void stop();}class Computer{public static void plugin(USB usb)//电脑可以使用接口{usb.start();System.out.println("电脑插上USB");usb.stop();}}class Flash implements USB{public void start(){System.out.println("U盘启动");}public void stop(){System.out.println("U盘停止");}}class Printer implements USB{public void start(){System.out.println("打印机启动");}public void stop(){System.out.println("打印机停止");}}public class Test06 {public static void main(String[] args) {Flash f = new Flash();Printer p = new Printer();Computer com = new Computer();com.plugin(f);com.plugin(p);}}
程序解读:
接口 interface 定义的是一种标准,无论是U盘还是printer都能够调用该标准使用。
在每个对象中,覆写USB标准具体内容。
扩展:钥匙是否算是一种接口呢,每个人都属于一个对象,人具备了钥匙就能够开门。
1.工厂设计模式
//本程序是工厂设计模式的操作interface Fruit {public abstract void eat();}class Apple implements Fruit {public void eat(){System.out.println("吃苹果");}}class Orange implements Fruit {public void eat(){System.out.println("吃橘子");}}public class Test06 {public static void main(String[] args) {Fruit a = new Apple();Fruit b = new Orange();a.eat();b.eat();}}
但是程序有个问题,main方法更多的是一个客户端,不负责产生苹果,只是指明苹果。此时直接在主方法中指定了要操作的子类,如果要更换子类,肯定要修改客户端。
跟特定的子类紧密的耦合在一起。
//本程序是工厂设计模式的操作interface Fruit {public abstract void eat();}class Factory{public static Fruit getFruit(String name){Fruit f = null;if ("Apple".equals(name)){f = new Apple();}if ("Orange".equals(name)){f = new Orange();}return f;}}class Apple implements Fruit {public void eat(){System.out.println("吃苹果");}}class Orange implements Fruit {public void eat(){System.out.println("吃橘子");}}public class Test06 {public static void main(String[] args) {new Factory().getFruit(args[0]).eat();}}
其中,在工厂中,为了判断是否是苹果类的equals方法的顺序很有讲究。
2.代理设计模式
生活中的代理上网服务器
//本程序是工厂设计模式的操作interface Network {public abstract void browse();}class Real implements Network{public void browse(){System.out.println("上网浏览信息");}}class Proxy implements Network{private Network network;//代理对象public Proxy(Network network){this.network = network ;}public void check(){System.out.println("用户合法");}public void browse(){this.check();this.network.browse();}}public class Test06 {public static void main(String[] args) {Network net = new Proxy(new Real());//指定代理net.browse();}}
3.适配器设计
具体思路:在接口中声明较多的方法,但是实际使用只是一部分。
//本程序是适配器设计模式的操作interface Window {public abstract void open();public abstract void close();public abstract void expand();}abstract class WindowAdapter implements Window{public void open() {};public void close() {};public void expand() {};}class Win extends WindowAdapter{public void open(){System.out.println("打开");}}public class Test06 {public static void main(String[] args) {Window x = new Win();x.open();}}
这种设计思路在java的图形界面使用非常多。
4.内部类的扩展
之前讲解了内部类的概念,实际在抽象类中可以包含接口
//本程序是内部类扩展的操作abstract class A{public abstract void printA();interface B{public abstract void printB();}}class Exp extends A{public void printA(){System.out.println("A打印方法");}class X implements B{public void printB(){System.out.println("B打印方法");}}}public class Test06 {public static void main(String[] args) {Exp.X aa= new Exp().new X();A.B a = aa;a.printB();}}
反之,在一个接口中定义一个抽象类。
从实际开发角度,这种设计不常见,代码结构混乱。
0 0
- 12-16面向对象之接口和抽象类的应用
- (42)抽象类和抽象方法的应用(43)接口与面向对象中多态的应用
- 面向对象之抽象和接口
- 面向对象(高级)抽象类与接口的应用
- Java面向对象高级--抽象类与接口的应用
- 面向对象(高级)抽象类与接口的应用
- 面向对象(高级篇之抽象类与接口的应用)
- 黑马程序员--面向对象之三:抽象类和接口
- JAVA面向对象之抽象类和接口
- java--面向对象之抽象类和接口综合案例
- 面向对象之抽象类、接口
- 面向对象与类的+抽象类和接口小结
- c#接口和抽象类的区别 ---- 面向对象概念
- javaSE-面向对象-接口和抽象类的区别
- 面向对象的设计原则,接口和抽象类区别
- 面向对象中接口和抽象类的区别
- 【面向对象】抽象类和接口的对比学习
- 面向对象_抽象类和接口的区别
- 互联网公司相关业务领域分析
- 一个APK反编译利器Apktool
- Windows 和 Ubuntu 10.04双…
- php用xml和json处理数据格式。
- Android NDK的使用
- 12-16面向对象之接口和抽象类的应用
- 日记 [2008年07月11日]
- J2ME技术实现:从当前屏幕跳转至其…
- J2ME RMS的学习笔记 (转载)
- java中类的定义
- 一个项目总监/经理心得
- Eclipse使用问题之一 [j2me librar…
- Eclipse使用问题之二 [修改一个变…
- Eclipse使用问题之三 (Eclips使用…