JAVA学习之抽象类和接口的应用
1. 为抽象类和接口实例化
在java中可以通过对象的多态性,为抽象类和接口实例化,这样再使用抽象类
和接口的时候就可以调用本子类中所覆写过的方法了。
之所以抽象类和接口不能直接实例化,是因为其内部包含了多个抽象方法,抽
像方法本身都是未实现的方法,所以无法调用。
通过对象多态性可以发现,子类发生了向上转型关系之后,所调用的全部方法
都是被覆写过得方法。
下面是通过子类为借口实例化的例子:
interface A{ //定义一个接口 public abstract void print(); //定义抽象方法 } class B implements A{ //实现接口 @Override public void print() { //覆写抽象方法 System.out.println("hello world!"); } } public class Test12 { public static void main(String[] args) { A a=new B(); //通过子类为接口实例化 a.print(); } }
2. 抽象类的应用----定义模板
例子:
abstract class Person{ private String name; //定义name属性 private int age; //定义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.getcontent()); //输出内容 } public abstract String getcontent(); //说话的内容由子类决定 } class Student extends Person{ private float score; public Student(String name,int age,float score){ super(name,age); this.score=score; } @Override public String getcontent() { return "学生信息-----》姓名:"+super.getName() +":年龄:"+super.getAge()+ ":成绩:"+this.score; } } class Worker extends Person{ private float salary; public Worker(String name,int age,float salary){ super(name,age); //调用父类中的构造方法 this.salary=salary; } @Override public String getcontent() { return "工人信息-----》姓名:"+super.getName() +":年龄:"+super.getAge()+ ":薪水:"+this.salary; } } public class Test13 { public static void main(String[] args) { Person per1=new Student("michael",18,99.0f); Person per2=new Worker("bob",40,3000.0f); per1.say(); per2.say(); } }
3. 接口的实际应用---制定标准
接口咋的实际应用就是制定标准,如果深入的话, 接口实际上还可以将方法的视图暴露给远程客户端。
接口实际应例子:
import java.util.concurrent.Callable; //接口的实际应用---制定标准 interface USB{ //定义了USB接口 public void start(); //USB设备开始工作 public void stop(); //USB设备停止工作 } class Computer{ public static void Plugin(USB usb){ usb.start(); System.out.println("=====usb设备工作====="); usb.stop(); } } class Flash implements USB{ @Override public void start() { //覆写方法 System.out.println("U盘开始工作。"); } @Override public void stop() { System.out.println("U盘停止工作"); } } class Print implements USB{ @Override public void start() { System.out.println("打印机开始工作"); } @Override public void stop() { System.out.println("打印机停止工作"); } } public class Test14 { public static void main(String[] args) { Computer.Plugin(new Flash()); Computer.Plugin(new Print()); }; }
4. 工厂设计模式
interface Fruit{ public void eat(); } class Apple implements Fruit{ @Override public void eat() { System.out.println("吃苹果!"); } } class Orange implements Fruit{ @Override public void eat() { System.out.println("吃橘子!"); } } public class Test15 { public static void main(String[] args) { Fruit f =new Apple(); f.eat(); } }
上述代码虽然没有语法错误,但是存在问题。
主方法应此时,应该就表示一个客户端,主方法的代码越少越好。此时,
直接在主方法中指定了要操作的子类,如果要更换子类,则肯定要修改
客户端。就表示跟特定的子类紧密结合在一起了。
JVM的工作原理:程序---->JVM----》操作系统。
此过渡端在程序中就称为工厂设计。
设计后:
interface Fruit{ //定义水果类 public void eat(); } class Apple implements Fruit{ @Override public void eat() { System.out.println("吃苹果!"); } } class Orange implements Fruit{ @Override public void eat() { System.out.println("吃橘子!"); } } class Factory { //定义工厂类 public static Fruit getInstance(String className){ Fruit f=null; if("Apple".equals(className)){ f=new Apple(); } if ("Orange".equals(className)){ f=new Orange(); } return f; } } public class Test15 { public static void main(String[] args) { Fruit f =Factory.getInstance("Apple"); if (f!=null){ //判断是否取得实例 f.eat(); } } }
5. 设计模式----代理设计
interface Network{ public 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("检查用户名是否合法"); } @Override public void browse() { this.check(); this.network.browse(); //调用真实的主题操作 } } public class Test16 { public static void main(String[] args) { Network net = null; net = new Proxy(new Real()); //指定代理操作 net.browse(); //客户只关心上网浏览一个操作 } }
6. 适配器设计
对于JAVA程序来说,如果一个类要实现接口,若子类不是抽象方法肯定要覆写
此接口的全部抽象方法,那么如果,此时一个接口中定义的抽象方法过多,但是
在子类中又用不到这么多抽象方法的话,则肯定很麻烦,所以此时就需要
一个中间国度,但是此过度类又不希望被直接使用,所以将此过度类定义成抽象
类最合适,即一个接口首先被一个抽象类(此抽象类通常称为适配器类),并在
此抽象类中实现若干方法(方法体为空),则以后的子类直接继承此抽象类,就
可以有选择的覆写所需要的方法。
例如:
interface Window{ //定义WINDOW接口,表示窗口操作 public void open(); //打开 public void close(); //关闭 public void activated(); //窗口活动 public void iconifiled(); //窗口最小化 public void deiconifiled(); //窗口恢复大小 } abstract class WindowAdapter implements Window{ public void open(){}; //打开 public void close(){}; //关闭 public void activated(){}; //窗口活动 public void iconifiled(){}; //窗口最小化 public void deiconifiled(){}; //窗口恢复大小 } class WindowImpl extends WindowAdapter { public void open(){ System.out.println("窗口打开。"); } public void close(){ System.out.println("窗口关闭。"); } } public class Test17 { public static void main(String[] args) { Window win=new WindowImpl(); win.open(); win.close(); } }
7. 内部类的扩展
内部类中可以定义接口,同样接口中也可以定义内部类。
abstract class A{ //定义抽象类 public abstract void printA(); //抽象方法 interface B{ //定义内部接口 public void PrintB(); //定义抽象方法 } } class X extends A{ //继承抽象类 @Override public void printA() { System.out.println("hello --->A"); } class Y implements B{ //定义内部类实现内部接口 public void PrintB(){ System.out.println("hello --->b"); } } } public class Test18 { public static void main(String[] args) { A.B b=new X().new Y(); b.PrintB(); } }
8. 抽象类和接口的区别