在java中如何定義一個抽象屬性示例詳解
前言
本文主要給大家介紹的是在java中定義一個抽象屬性的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹:
Abstract關(guān)鍵字通常被用于類和方法,用來把某些行為的實(shí)現(xiàn)委托給子類。由于Java不支持抽象屬性,如果你試圖將類屬性標(biāo)記為抽象,將會得到一個編譯時錯誤。
在本教程中,我們將介紹兩種定義抽象屬性的方法,這些抽象屬性可以由子類進(jìn)行設(shè)置,而且不使用Abstract 關(guān)鍵字。
實(shí)用案例
假設(shè)我們想要實(shí)現(xiàn)一個記錄事務(wù)的日志模塊,用來記錄特定事務(wù)的信息。我們希望這個模塊是抽象的,這樣我們可以實(shí)現(xiàn)不同的日志記錄方式,例如:記錄到文件或數(shù)據(jù)庫中。
我們的引擎使用預(yù)定義的分隔符來連接日志中的信息,并存儲在一個String中。具體應(yīng)該使用哪個分隔符,這將取決于日志記錄的規(guī)則,例如可以用字符“,”對日志記錄中不同部分的信息進(jìn)行分割。
因此,分隔符看起來對我們的引擎是抽象的,需要由每個日志記錄規(guī)則明確定義。
下面我提供兩種方式,來實(shí)現(xiàn)把分隔符的定義委托給子類。
在抽象類中定義帶參數(shù)的構(gòu)造函數(shù)
在抽象類中定義動態(tài)屬性的第一種方法是:定義一個參數(shù)的構(gòu)造函數(shù)。
所以我們可以這樣實(shí)現(xiàn)這個引擎:
// TransactionManager.java
public abstract class TransactionManager {
private String separator;
public TransactionManager(String separator) {
this.separator = separator;
}
public abstract void writeTransaction(String result);
public Transaction startTransaction()
{
Transaction transaction = new Transaction(System.currentTimeMillis());
return transaction;
}
public void endTransaction(Transaction t) {
long processingTime = System.currentTimeMillis() - t.getStartTime();
StringBuilder logBuilder = new StringBuilder();
logBuilder.append(t.getStartTime());
// Notice the use of this.separator
logBuilder.append(this.separator);
logBuilder.append(processingTime);
logBuilder.append(this.separator);
logBuilder.append(t.getData());
String result = logBuilder.toString();
writeTransaction(result);
}
}
在抽象類中定義帶參數(shù)的構(gòu)造函數(shù)時,子類將會被強(qiáng)制定義自己的構(gòu)造函數(shù)并調(diào)用super() 。 這樣我們就能強(qiáng)制separator屬性依賴于已使用的日志記錄機(jī)制。
注意:我們的引擎實(shí)現(xiàn)了所有日志機(jī)制共有的靜態(tài)行為:startTransaction() , endTransaction() ,同時將動態(tài)行為writeTransaction()交給子類去實(shí)現(xiàn)。
現(xiàn)在,如果我們想要創(chuàng)建一個事務(wù)管理器,用它將日志內(nèi)容記錄到一個文件中,那么可以這樣去定義:
public class TransactionManagerFS extends TransactionManager{
// The IDE forces you to implement constructor.
public TransactionManagerFS(String separator) {
super(separator);
}
@Override
public void writeTransaction(String result) {
System.out.println("The following transaction has just finished: " );
System.out.println(result);
}
}
接下來做一個測試,看看代碼是怎樣工作的
public static void main(String[] args) throws InterruptedException {
// we pass the separator explicitly in the constructor
TransactionManager transactionManager = new TransactionManagerFS(",");
Transaction transaction = transactionManager.startTransaction();
transaction.setData("This is a test transaction !!");
Thread.sleep(1500);
transactionManager.endTransaction(transaction);
}
輸出:
The following transaction has just finished: 1502179140689,1501,This is a test transaction !!
通過getter方法傳遞分隔符
另外一種實(shí)現(xiàn)動態(tài)屬性的方法是:通過定義一個抽象的getter方法,該方法根據(jù)當(dāng)前的日志記錄機(jī)制來檢索所需的分隔符。在我們的引擎中,當(dāng)需要要使用分隔符時,可以通過調(diào)用這個getter方法得到。
接下來我們將引擎修改成這樣:
public abstract class TransactionManager {
public abstract String getSeperator();
public abstract void writeTransaction(String result);
public Transaction startTransaction()
{
Transaction transaction = new Transaction(System.currentTimeMillis());
return transaction;
}
public void endTransaction(Transaction t) {
long processingTime = System.currentTimeMillis() - t.getStartTime();
StringBuilder logBuilder = new StringBuilder();
logBuilder.append(t.getStartTime());
// Notice the use of getSeparator()
logBuilder.append(getSeperator());
logBuilder.append(processingTime);
logBuilder.append(getSeperator());
logBuilder.append(t.getData());
String result = logBuilder.toString();
writeTransaction(result);
}
}
另外修改TransactionManagerFS如下:
public class TransactionManagerFS extends TransactionManager{
@Override
public String getSeperator() {
return ",";
}
@Override
public void writeTransaction(String result) {
System.out.println("The following transaction has just finished: " );
System.out.println(result);
}
}
然后,修改main以使用新的實(shí)現(xiàn),并確保得到正確的結(jié)果。
public static void main(String[] args) throws InterruptedException {
// The separator is defined implicitly using getSeparator() method of the manager
TransactionManager transactionManager = new TransactionManagerFS();
Transaction transaction = transactionManager.startTransaction();
transaction.setData("This is a test transaction !!");
Thread.sleep(1500);
transactionManager.endTransaction(transaction);
}
輸出:
The following transaction has just finished: 1502179140689,1501,This is a test transaction !!
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
翻譯:瘋狂的技術(shù)宅
原文:http://programmergate.com/define-abstract-property-java/
本文首發(fā)微信公眾號:充實(shí)的腦洞
相關(guān)文章
Django+Django-Celery+Celery的整合實(shí)戰(zhàn)
這篇文章主要介紹了Django+Django-Celery+Celery的整合實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Python中比較特別的除法運(yùn)算和冪運(yùn)算介紹
這篇文章主要介紹了Python中比較特別的除法運(yùn)算和冪運(yùn)算介紹,“/”這個是除法運(yùn)算,那么這個“//”呢?“*”這個是乘法運(yùn)算,那么這個“**”呢?本文就講解這些運(yùn)算的不同,需要的朋友可以參考下2015-04-04
python操作mysql實(shí)現(xiàn)一個超市管理系統(tǒng)
超市管理系統(tǒng)有管理員和普通用戶兩條分支,只需掌握Python基礎(chǔ)語法,就可以完成這個項目,下面這篇文章主要給大家介紹了關(guān)于python操作mysql實(shí)現(xiàn)一個超市管理系統(tǒng)的相關(guān)資料,需要的朋友可以參考下2022-12-12
Python中的條件判斷語句基礎(chǔ)學(xué)習(xí)教程
這篇文章主要介紹了Python中的條件判斷語句基礎(chǔ)學(xué)習(xí)教程,文中使用的是Python2.x版本但條件語句部分的使用規(guī)則未在3.x中改變,需要的朋友可以參考下2016-02-02
利用一個簡單的例子窺探CPython內(nèi)核的運(yùn)行機(jī)制
這篇文章主要介紹了利用一個簡單的例子窺探CPython內(nèi)核的運(yùn)行機(jī)制,作者通過一個簡單的輸出函數(shù)深入、介紹了CPython源碼C代碼中的一些函數(shù),需要的朋友可以參考下2015-03-03

