您的位置:首页 > 其它

12-16面向对象之接口和抽象类的应用

2014-12-16 20:35 134 查看

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();
}
}


反之,在一个接口中定义一个抽象类。

从实际开发角度,这种设计不常见,代码结构混乱。

祝大家健健康康,快快乐乐。明天就不更新了需要反馈巩固了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐