簡體   English   中英

繼承基類對象的派生類構造函數

[英]Derived class constructor taking base class object

我正在嘗試擴展BitmapDrawable

public class CenterClampedBitmapDrawable extends BitmapDrawable

我想有一個使用BitmapDrawable的構造函數,並使用它來設置我的基類屬性。

public CenterClampedBitmapDrawable(BitmapDrawable src) {

我不知道如何執行此操作,如果BitmapDrawable具有復制構造函數,則我認為可以,但是還沒有。

我對Java相當陌生,這讓我感到困惑,在C ++中這是微不足道的

您必須在子類構造函數中手動執行此操作:

this.setFoo(src.getFoo());
this.setBar(src.getBar());

也許您可以使用一些反射實用程序,例如apache commons-beanutils(不知道該特定程序是否可以在android上運行),但是如果屬性不是太多,則手動方法會更好。

您必須在新類中實現構造函數以復制屬性。

如果您不想編寫所有的getter / setter調用,請在構造函數中使用Apache commons BeanUtils copyProperties。

http://commons.apache.org/beanutils/api/org/apache/commons/beanutils/BeanUtils.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM