采用基类对象的派生类构造函数

时间:2011-10-08 21:50:04

标签: java android

我正在尝试扩展BitmapDrawable

public class CenterClampedBitmapDrawable extends BitmapDrawable

我想要一个构造函数,它使用BitmapDrawable并使用它来设置我的基类属性。

public CenterClampedBitmapDrawable(BitmapDrawable src) {

我无法解决如何执行此操作,如果BitmapDrawable有一个复制构造函数,那么我认为我会没事,但它没有。

我对java很新,这让我感到难过,这在C ++中是微不足道的

2 个答案:

答案 0 :(得分:0)

您必须在子类构造函数中手动执行此操作:

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

也许你可以使用一些反射实用程序,比如apache commons-beanutils(不知道这个特定的是否适用于android),但是如果属性不是太多,那么手动方法会更好。

答案 1 :(得分:0)

您必须在新类中实现构造函数才能复制属性。

如果您不想编写所有getter / setter调用,请在构造函数中使用Apache commons BeanUtils copyProperties。

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

相关问题