12-16面向對象之接口和抽象類的應用

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

 

反之,在一個接口中定義一個抽象類。

從實際開發角度,這種設計不常見,代碼結構混亂。


祝大家健健康康,快快樂樂。明天就不更新了需要反饋鞏固了。








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章