简体   繁体   English

通过不可变继承重用方法

[英]reuse methods through immutable inheritance

If I have an AddressBook to store Items : 如果我有一个AddressBook来存储Items

public class AddressBook {

    public void add(Item item) {
       ...
    }

}

And an immutable IPAddress 还有一个不变的IPAddress

public final class IPAddress {}

And an immutable PhysicalAddress 还有一个不变的PhysicalAddress

public final class PhysicalAddress {}

And the potential parent class Item 而潜在的父类Item

public final class Item {}

Since immutable objects wouldn't be able to extend the immutable class Item , how could I reuse the add method in AddressBook class for either an IPAddress or a PhysicalAddress ? 由于不可变对象无法扩展不可变类Item ,因此如何在AddressBook类中为IPAddressPhysicalAddress重用add方法?

First of all you need to be aware that a "final class" in Java does not mean immutable, it means "un-inheritable". 首先,您需要意识到Java中的“最终类”并不意味着不可变,而是“不可继承”。 Immutability using "final" keyword only works for variables (and only after you set their initial value). 使用“ final”关键字的不可变性仅适用于变量(且仅在设置其初始值之后)。

If you want Item to be un-inheritable (final class) and you also want IPAddress and PhysicalAdress to be final as well, you can use interfaces. 如果希望Item是不可继承的(最终类),并且还希望IPAddress和PhysicalAdress也是最终的,则可以使用接口。 (Edit: it's not a requirement for those two classes to be final, just wanted to note that you can keep them final if you actually need to, although as others have commented you should really be sure that you need it to be final). (编辑:这两个类不是最终的要求,只是想指出的是,您可以根据需要将它们保持为最终状态,尽管正如其他人评论的那样,您应该确保将其最终确定)。

You could change Item to be an interface and make IPAddress and PhysicalAddress implement that interface. 您可以将Item更改为接口,并使IPAddress和PhysicalAddress实现该接口。

This way AdressBook actually adds objects of type "ItemInterface" (bad mnemonic but you get the idea) and as long as you manage to abstract common operations for all Items, you can use the interfaces instead of inheritance. 这样,AdressBook实际上会添加类型为“ ItemInterface”的对象(助记符不好,但您会明白),只要您设法对所有Items进行通用操作抽象,就可以使用接口代替继承。

eg 例如

public interface ItemInterface{
    public Object getItemValue();
    public void setItemValue(Object value);
}

public final class IPAddress implements ItemInterface{
    @Override
    public Object getItemValue(){
       ...
    }
    @Override
    public void setItemValue(Object value){
       ...
    }
}

and then you can do: 然后您可以执行以下操作:

public class AddressBook {

    public void add(ItemInterface item) {
        itemsList.add(item);
        // or whatever other structure you use to store items
    }
}

In general as you develop more and more complex code it becomes more useful to program to an interface than try to keep using inheritance. 通常,随着开发越来越复杂的代码,对接口进行编程比尝试继续使用继承变得更加有用。

This way, whenever you need to add another type of entry to your AddressBook, you can make it implement ItemInterface and ideally you won't have to change a line inside AddressBook because everything is an ItemInterface. 这样,每当需要在地址簿中添加另一种类型的条目时,都可以使其实现ItemInterface,并且理想情况下,您不必在AddressBook中更改一行,因为所有内容都是ItemInterface。

I think, that you probably mixed up immutable and final class 我认为,您可能混合了不可变的课程和期末课程

Immutable object means, that it cannot be changed, after it's creation. 不可变对象表示创建后无法更改。 see https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html 参见https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html

True is, that final class cannot be inherted. 诚然,不能继承最终班级。 If you really want inherit class it cannot be final. 如果您真的想要继承类,那么它不能是最终的。

But the point is, that immutable class does NOT have to be final. 但关键是,不可变的类不必是最终的。

Marking a class as final means that it cannot be extended. 将一个类标记为final表示无法扩展。 That basically means that you think the class is perfect and no one will ever need (or be able) to extend it or override any of its behaviour in a more specific subclass. 从根本上讲,这意味着您认为该类是完美的,并且没有人将需要(或能够)扩展它或覆盖更具体的子类中的任何行为。

Generally speaking, you should avoid using final classes unless you have a very good reason to use them. 一般来说,除非有充分的理由使用final类,否则应避免使用final类。

Because you cannot extend a final class, it can never be used in anywhere in an inheritance hierarchy except at the very bottom. 因为您不能扩展最终类,所以永远不能在继承层次结构的任何地方使用它,除非在最底层。

Are you sure you actually wanted to make your classes final? 您确定您确实要使课程最终结业吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM