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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 两个月的宝宝不爱吃奶怎么办 仓鼠走路画圈了怎么办 力气大但扳手腕不行怎么办 只睡了两个小时怎么办 微博账号被冻结了怎么办 微博一天多次解冻怎么办 肿瘤对化疗不敏感怎么办 2个月宝宝肺炎怎么办 小孩咳嗽2个月怎么办 两个月的小孩子气管炎怎么办? 小孩子两个月发烧38度怎么办 两个月的小孩子咳嗽怎么办 5个月宝宝吃奶少怎么办 26岁的1型糖尿病怎么办 睡前吃得太饱怎么办 胰岛素2小时>300怎么办 血清c肽测定高怎么办 体测蛋白质和骨骼肌偏高怎么办 半个月重了十斤怎么办 月经停了2个月怎么办 在练腹肌中腹痛怎么办 越练肌肉越肥怎么办 喘不过气来 心闷怎么办 被气得喘不过气怎么办 健身完头晕想吐怎么办 吃多了反胃头晕怎么办 合同未约定退货货物积压怎么办 运动内衣把胸压平怎么办 经常穿皮鞋脚臭怎么办 买衣服胸围小了怎么办 内衣下胸围太紧怎么办 穿文胸衣服要开怎么办 运动内衣的拉链老来怎么办 胸罩没干急着穿怎么办 跑步时大腿很痒怎么办 胖大腿内侧磨伤怎么办 内衣围带过松怎么办 内衣底围特别紧怎么办 全棉衣服上的油怎么办 高腰牛仔裤腰大了怎么办 新买衣服太硬怎么办