Java 具有抽象getter和setter的抽象类

Java 具有抽象getter和setter的抽象类,java,Java,假设我有一个抽象类List,我想创建一个扩展这个类的类,名为MyList。将抽象getter和setter放在抽象类中,强迫MyList重写它们,这是一种糟糕的编程实践吗 我想这样做的原因类似于为列表的每个专门化设置不同的常量容量。所以我会这样做: public abstract class List { public abstract int getMaximumSize(); } public class MyList extends List { @Override

假设我有一个抽象类
List
,我想创建一个扩展这个类的类,名为
MyList
。将抽象getter和setter放在抽象类中,强迫
MyList
重写它们,这是一种糟糕的编程实践吗

我想这样做的原因类似于为
列表的每个专门化设置不同的常量容量。所以我会这样做:

public abstract class List {
    public abstract int getMaximumSize();
}

public class MyList extends List {
    @Override
    public int getMaximumSize() {
        return 10;
    }
}
public abstract class List {
    // this is the method to override
    protected abstract int getMaximumSizeInternal();
    // and this method is final. Can't mess around with that!
    public final int getMaximumSize() {
        int x = getMaximumSizeInternal();
        if (x < 0) { throw new RuntimeException ("Apologies, sub-classer is an idiot"); }
        return x;
}

public class MyList extends List {
    @Override
    protected int getMaximumSizeInternal() {
        // negative?!?!
        return -10;
    }
}

我还举了另一个例子,我在抽象类中放置了一个setter,但现在我忘记了。

我认为如果你想强制每个孩子都使用getter\setter是可以的。最后,如果它满足了您的需求,就可以了,我更愿意问是否有更好的方法来实现这种行为。

在我看来,这是一种合理的方法(模setter。不要在抽象、非抽象或任何其他类型的文件中进行setter)

但这里有一个问题——如果类的实现者是个白痴会发生什么?考虑

public abstract class List {
    public abstract int getMaximumSize();
}

public class MyList extends List {
    @Override
    public int getMaximumSize() {
        // negative?!?!
        return -10;
    }
}
如果你想避免这种情况,你可以这样做:

public abstract class List {
    public abstract int getMaximumSize();
}

public class MyList extends List {
    @Override
    public int getMaximumSize() {
        return 10;
    }
}
public abstract class List {
    // this is the method to override
    protected abstract int getMaximumSizeInternal();
    // and this method is final. Can't mess around with that!
    public final int getMaximumSize() {
        int x = getMaximumSizeInternal();
        if (x < 0) { throw new RuntimeException ("Apologies, sub-classer is an idiot"); }
        return x;
}

public class MyList extends List {
    @Override
    protected int getMaximumSizeInternal() {
        // negative?!?!
        return -10;
    }
}
公共抽象类列表{
//这是要重写的方法
受保护的抽象int getMaximumSizeInternal();
//这个方法是最终的,不能乱来!
公共最终整数getMaximumSize(){
int x=getMaximumSizeInternal();
如果(x<0){抛出新的RuntimeException(“抱歉,子类是个白痴”);}
返回x;
}
公共类MyList扩展列表{
@凌驾
受保护的整数getMaximumSizeInternal(){
//否定?!?!
返回-10;
}
}

在这种情况下,我建议使用一个界面,例如

public interface MaxList extends List // or some better name {
    public int getMaxSize();
}
然后对于
MyList

public class MyList extends ArrayList implements MaxList { 
// The above assumes you want to extend ArrayList. You may extend some other 
// List implementation or provide your own.
    @Override
    public int getMaximumSize() {
        return 10;
    }
}

注意:我假设您想要扩展
java.util.List
的实现。如果您不想扩展
MaxList
接口来扩展
java.util.List
或MyList来扩展
java.util.ArrayList

,请尽早失败。
public class MyList extends ArrayList implements MaxList { 
// The above assumes you want to extend ArrayList. You may extend some other 
// List implementation or provide your own.
    @Override
    public int getMaximumSize() {
        return 10;
    }
}