262
技術社區[雲棲]
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、抽象類與接口之間的關係

重要提示:在開發中,一個類永遠不要去繼承一個已經實現好的類,要麼繼承抽象類,要麼實現接口。如果抽象類和接口同時都可以使用的話,那麼優先使用接口,避免單繼承局限。
最後更新:2017-04-03 15:21:56