Java面向对象高级--抽象类与接口的应用

1、为抽象类和接口实例化


抽象类和接口不能直接实例化,因为其内部包含了各个抽象方法,抽象方法本身都是未实现的方法,所以无法调用。通过对象多态性,子类发生了向上转型之后,所调用的全部方法都是被覆写过了的方法。
为抽象类实例化:
abstract class A{	// 定义抽象类A
	public abstract void print() ;	// 定义抽象方法print()
};
class B extends A {	// 定义子类,继承抽象类
	public void print(){		// 覆写抽象方法
		System.out.println("Hello World!!!") ;
	}
};
public class AbstractCaseDemo01{
	public static void main(String args[]){
		A a = new B() ;		// 通过子类为抽象类实例化
		a.print() ;
	}
};
为接口实例化:
interface A{	// 定义抽象类A
	public abstract void print() ;	// 定义抽象方法print()
};
class B implements A {	// 定义子类,继承抽象类
	public void print(){		// 覆写抽象方法
		System.out.println("Hello World!!!") ;
	}
};
public class InterfaceCaseDemo01{
	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 ;
	}
	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 ;
	}
	public String getContent(){
		return	"工人信息 --> 姓名:" + super.getName() + 
				";年龄:" + super.getAge() + 
				";工资:" + this.salary ;
	}
};
public class AbstractCaseDemo02{
	public static void main(String args[]){
		Person per1 = null ;	// 声明Person对象
		Person per2 = null ;	// 声明Person对象
		per1 = new Student("张三",20,99.0f) ;	// 学生是一个人
		per2 = new Worker("李四",30,3000.0f) ;	// 工人是一个人
		per1.say() ;	// 学生说学生的话
		per2.say() ;	// 工人说工人的话
	}
};

3、接口的实际应用----指定标准

interface USB{		// 定义了USB接口
	public void start() ;	// USB设备开始工作
	public void stop() ;	// USB设备结束工作
}
class Computer{
	public static void plugin(USB 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 Print implements USB{
	public void start(){	// 覆写方法
		System.out.println("打印机开始工作。") ;
	}
	public void stop(){		// 覆写方法
		System.out.println("打印机停止工作。") ;
	}
};
public class InterfaceCaseDemo02{
	public static void main(String args[]){
		Computer.plugin(new Flash()) ;
		Computer.plugin(new Print()) ;
	}
};

4、工厂设计模式

interface Fruit{	// 定义一个水果接口
	public void eat() ;	// 吃水果
}
class Apple implements Fruit{
	public void eat(){
		System.out.println("** 吃苹果。") ;
	}
};
class Orange implements Fruit{
	public void eat(){
		System.out.println("** 吃橘子。") ;
	}
};
public class InterfaceCaseDemo03{
	public static void main(String args[]){
		Fruit f = new Apple() ;	// 实例化接口
		f.eat() ;
	}
};
此过渡端在程序中就称为工厂设计。
interface Fruit{	// 定义一个水果接口
	public 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 ;
	}
};
public class InterfaceCaseDemo05{
	public static void main(String args[]){
		Fruit f = Factory.getInstance(args[0]) ;	// 实例化接口
		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("检查用户是否合法。") ;
	}
	public void browse(){
		this.check() ;
		this.network.browse() ;	// 调用真实的主题操作
	}
};
public class ProxyDemo{
	public static void main(String args[]){
		Network net = null ;
		net  = new Proxy(new Real()) ;//  指定代理操作
		net.browse() ;	// 客户只关心上网浏览一个操作
	}
};


6、适配器设计


interface Window{		// 定义Window接口,表示窗口操作
	public void open() ;	// 打开
	public void close() ;	// 关闭
	public void activated() ;	// 窗口活动
	public void iconified() ;	// 窗口最小化
	public void deiconified();// 窗口恢复大小
}
abstract class WindowAdapter implements Window{
	public void open(){} ;	// 打开
	public void close(){} ;	// 关闭
	public void activated(){} ;	// 窗口活动
	public void iconified(){} ;	// 窗口最小化
	public void deiconified(){};// 窗口恢复大小
};
class WindowImpl extends WindowAdapter{
	public void open(){
		System.out.println("窗口打开。") ;
	}
	public void close(){
		System.out.println("窗口关闭。") ;
	}
};
public class AdapterDemo{
	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{	// 继承抽象类
	public void printA(){
		System.out.println("HELLO --> A") ;
	}
	class Y implements B{	// 定义内部类实现内部接口
		public void printB(){
			System.out.println("HELLO --> B") ;
		}
	};
};
public class InnerExtDemo01{
	public static void main(String args[]){
		A.B b = new X().new Y() ;
		b.printB() ;
	}
};

接口中定义一个抽象类
interface A{	// 定义接口
	public void printA() ;	// 抽象方法
	abstract class B{	// 定义内部抽象类
		public abstract void printB() ;	// 定义抽象方法
	}
};
class X implements A{	// 实现接口
	public void printA(){
		System.out.println("HELLO --> A") ;
	}
	class Y extends B{	// 继承抽象类
		public void printB(){
			System.out.println("HELLO --> B") ;
		}
	};
};
public class InnerExtDemo02{
	public static void main(String args[]){
		A.B b = new X().new Y() ;
		b.printB() ;
	}
};

8、抽象类与接口之间的关系


重要提示:在开发中,一个类永远不要去继承一个已经实现好的类,要么继承抽象类,要么实现接口。如果抽象类和接口同时都可以使用的话,那么优先使用接口,避免单继承局限。




  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值