抽象类与接口的实际应用

抽象类的常见实际应用——模板

public class AbstractCaseDemo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Person p=new Student("小明",18,99.9f);
		p.say();
		
	}

}
abstract class Person{
	private String name;
	private int age;
	public Person(String name,int age) {
		this.setAge(age);
		this.setName(name);
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = 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.setScore(score);
	}
	public float getScore() {
		return score;
	}
	public void setScore(float score) {
		this.score = score;
	}
	public String getContent() {
		return "学生信息——>姓名:"+super.getName()+"年龄:"+super.getAge()+"成绩:"+this.score;
	}
}
//结果:
//学生信息——>姓名:小明年龄:18成绩:99.9

接口的常见实际应用——工厂设计
工厂设计:在接口与具体子类之间加入一个过渡端,通过此过渡段取得接口实例。一般称这个过渡端为工厂类。
步骤:
1、创建接口和子类。
2、创建过渡端。
3、使过渡端作用于接口和接口子类之间。
4、在过渡端取得接口实例。

public class InterfaceCaseDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Fruit f=null;
		f=Factory.getInstance("apple");
		f.eat();
	}

}
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("吃橘子");
	}
}
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;
	}
}
//结果:
//吃苹果

接口的常见实际应用——代理设计
代理设计:就是指由一个代理主题来操作真实主题,真实主题执行具体的业务操作,而代理主题负责其他相关业务的处理。
步骤:
1、创建接口。
2、创建代理主题和真实主题并都实现接口。
3、代理主题操作真实主题。

public class ProxyDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		NetWork net=null;	//定义接口对象
		net=new Proxy(new Real());	//实例化代理,同时传入代理的真实操作
		net.browse();	//客户的实际操作
	}

}
interface NetWork{	//1、创建接口
	public void browse();
}
class Real implements NetWork{	//2、创建真实主题并实现接口
	public void browse() {
		System.out.println("上网浏览信息");
	}
}
class Proxy implements NetWork{	//3、创建代理主题并实现接口
	private NetWork network;
	public Proxy(NetWork network) {	//设置代理主题的真实操作
		this.network=network;	    //设置代理主题的子类
	}
	public void check() {	//代理主题自己的操作
		System.out.println("检查用户是否合法");
	}
	public void browse() {	
		this.check();		//调用代理主题具体业务的操作
		this.network.browse();	//4、调用(操作)真实主题
	}
}
//检查用户是否合法
//上网浏览信息

代理主题要做比真实主题更多的业务相关操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值