Java:从通用List / Collection派生

时间:2022-02-19 16:05:42

I have a little problem understanding the Java language

理解Java语言时遇到一些问题

public class PhonebookEntryList extends List<PhonebookEntry>
{
    public PhonebookEntryList(String filename) throws IOException
    {
        //loadListFromFilename(filename);
    }

    public void saveListToFilename(String filename) throws IOException
    {
        //Do something to save it to a file
    }
}

I can't do this, because List is a generic Type (of course). I know what that means, but at the moment I can't think of a Solution for this problem.

我不能这样做,因为List是一个通用类型(当然)。我知道这意味着什么,但目前我无法想到这个问题的解决方案。

Can you help me to solve it? Thanx!

你能帮我解决一下吗?感谢名单!

7 个解决方案

#1


No, your only problem is that you're extending an interface; you must implement it instead.

不,你唯一的问题是你正在扩展一个界面;你必须改为实现它。

public class PhonebookEntryList implements List<PhonebookEntry>

should work; or you might prefer to extend a concrete class:

应该管用;或者您可能更喜欢扩展具体类:

public class PhonebookEntryList extends ArrayList<PhonebookEntry>

or

public class PhonebookEntryList extends LinkedList<PhonebookEntry>

#2


You can't do that because List is an interface. But! You shouldn't extend or implement a List class to make a PhonebookEntryList, it's a design error.

你不能这样做,因为List是一个接口。但!您不应该扩展或实现List类来创建PhonebookEntryList,这是一个设计错误。

You should do:

你应该做:

public class PhonebookEntryList
{
    private List<PhonebookEntry> entries;

    public PhonebookEntryList(String filename) throws IOException
    {
        //loadListFromFilename(filename);
    }

    public void saveListToFilename(String filename) throws IOException
    {
        //Do something to save it to a file
    }
}

I.e. your PhonebookEntryList should contain a list instead of inheriting it.

即您的PhonebookEntryList应包含列表而不是继承它。

#3


List<T> is an interface, not a class, so you can't inherit from it. You can, however, inherit from a generic type, supplying the type argument, if you wish to create, e.g. a collection for a specific type with some behaviour specific only to that type.

List 是一个接口,而不是一个类,所以你不能从它继承。但是,如果要创建,则可以从泛型类型继承,提供类型参数,例如,特定类型的集合,其某些行为仅针对该类型。

#4


Your Problem is that you are trying to extend an interface rather than implement it.

您的问题是您正在尝试扩展接口而不是实现它。

Composition is what you want. Create a class that wrapps a List (or something that iplements that interface)

构图是你想要的。创建一个包装List的类(或者那些支持该接口的东西)

and add functionality.

并添加功能。

#5


Should I mention that List is an Interface and not a Class? Nah. I think you got the point by now.

我是否应该提到List是一个接口而不是一个类?罗。我想你现在明白了。

I would like to point out, however, that it's usually better NOT to embed the persistence mechanism within the list class. There's this thing called the Visitor pattern that works better. By placing the actual persistency code in a seperate class, the overall logical complexity of the app gets reduced (at the expense of an extra class), and your phonebook becomes liberated to be used in places where having dependencies on the persistency mechanism that looked good when you first designed the code don't look so good anymore. For example, if you wanted to make the Phonebook be an item in an ORM-referenced database.

但是,我想指出,通常最好不要将持久性机制嵌入到列表类中。有一种称为访客模式的东西效果更好。通过将实际的持久性代码放在一个单独的类中,应用程序的整体逻辑复杂性会降低(以额外的类为代价),并且您的电话簿将被释放,以便在依赖于持久性机制看起来很好的地方使用当你第一次设计代码时,看起来不再那么好了。例如,如果您想使电话簿成为ORM引用数据库中的项目。

#6


List<T> is an interface.

List 是一个接口。

If you want to extend a class you'll have to choose an implementation (ArrayList<T> maybe): extends ArrayList<PhonebookEntry>

如果你想扩展一个类,你必须选择一个实现(可能是ArrayList ):extends ArrayList

If you want to implement a List change your code to: implements List<PhonebookEntry>

如果要实现List,请将代码更改为:implements List

#7


If you look at the JavaDoc for List, you'll see (as others mentioned) that it's an interface, not a class. What you most likely want to do is look on the same JavaDoc page at "All Known Implementing Classes" and you'll see AbstractList. Extend this. Alternatively, extend one of the non-abstract List implementations.

如果你看看JavaDoc for List,你会看到(正如其他人提到的)它是一个接口,而不是一个类。您最想要做的是查看“所有已知实现类”的同一JavaDoc页面,您将看到AbstractList。扩展这个。或者,扩展其中一个非抽象List实现。

Note: Most of the time when someone starts to extend one of the Java Collection classes, you're going down the wrong route. Usually, it's better to use one of the existing collections in your class and proxy any collections-style requests that you need. Or return an unmodifiable proxy of your collection:

注意:大多数情况下,当某人开始扩展其中一个Java Collection类时,您将走错路线。通常,最好使用类中的一个现有集合并代理所需的任何集合式请求。或者返回您的收藏的不可修改的代理:

public class MyClass {
  private final List<PhonebookEntry> myList = new LinkedList<PhonebookEntry>();

  public List<PhonebookEntry> getList() {
    return Collections.unmodifiableList(myList);
  }
}

Usually, it's best to extend a class only if you intend to have different behavior than the class you are extending. Inheritance is more brittle than composition.

通常,只有当您打算使用与您要扩展的类不同的行为时,才最好扩展类。遗传比构成更脆弱。

#1


No, your only problem is that you're extending an interface; you must implement it instead.

不,你唯一的问题是你正在扩展一个界面;你必须改为实现它。

public class PhonebookEntryList implements List<PhonebookEntry>

should work; or you might prefer to extend a concrete class:

应该管用;或者您可能更喜欢扩展具体类:

public class PhonebookEntryList extends ArrayList<PhonebookEntry>

or

public class PhonebookEntryList extends LinkedList<PhonebookEntry>

#2


You can't do that because List is an interface. But! You shouldn't extend or implement a List class to make a PhonebookEntryList, it's a design error.

你不能这样做,因为List是一个接口。但!您不应该扩展或实现List类来创建PhonebookEntryList,这是一个设计错误。

You should do:

你应该做:

public class PhonebookEntryList
{
    private List<PhonebookEntry> entries;

    public PhonebookEntryList(String filename) throws IOException
    {
        //loadListFromFilename(filename);
    }

    public void saveListToFilename(String filename) throws IOException
    {
        //Do something to save it to a file
    }
}

I.e. your PhonebookEntryList should contain a list instead of inheriting it.

即您的PhonebookEntryList应包含列表而不是继承它。

#3


List<T> is an interface, not a class, so you can't inherit from it. You can, however, inherit from a generic type, supplying the type argument, if you wish to create, e.g. a collection for a specific type with some behaviour specific only to that type.

List 是一个接口,而不是一个类,所以你不能从它继承。但是,如果要创建,则可以从泛型类型继承,提供类型参数,例如,特定类型的集合,其某些行为仅针对该类型。

#4


Your Problem is that you are trying to extend an interface rather than implement it.

您的问题是您正在尝试扩展接口而不是实现它。

Composition is what you want. Create a class that wrapps a List (or something that iplements that interface)

构图是你想要的。创建一个包装List的类(或者那些支持该接口的东西)

and add functionality.

并添加功能。

#5


Should I mention that List is an Interface and not a Class? Nah. I think you got the point by now.

我是否应该提到List是一个接口而不是一个类?罗。我想你现在明白了。

I would like to point out, however, that it's usually better NOT to embed the persistence mechanism within the list class. There's this thing called the Visitor pattern that works better. By placing the actual persistency code in a seperate class, the overall logical complexity of the app gets reduced (at the expense of an extra class), and your phonebook becomes liberated to be used in places where having dependencies on the persistency mechanism that looked good when you first designed the code don't look so good anymore. For example, if you wanted to make the Phonebook be an item in an ORM-referenced database.

但是,我想指出,通常最好不要将持久性机制嵌入到列表类中。有一种称为访客模式的东西效果更好。通过将实际的持久性代码放在一个单独的类中,应用程序的整体逻辑复杂性会降低(以额外的类为代价),并且您的电话簿将被释放,以便在依赖于持久性机制看起来很好的地方使用当你第一次设计代码时,看起来不再那么好了。例如,如果您想使电话簿成为ORM引用数据库中的项目。

#6


List<T> is an interface.

List 是一个接口。

If you want to extend a class you'll have to choose an implementation (ArrayList<T> maybe): extends ArrayList<PhonebookEntry>

如果你想扩展一个类,你必须选择一个实现(可能是ArrayList ):extends ArrayList

If you want to implement a List change your code to: implements List<PhonebookEntry>

如果要实现List,请将代码更改为:implements List

#7


If you look at the JavaDoc for List, you'll see (as others mentioned) that it's an interface, not a class. What you most likely want to do is look on the same JavaDoc page at "All Known Implementing Classes" and you'll see AbstractList. Extend this. Alternatively, extend one of the non-abstract List implementations.

如果你看看JavaDoc for List,你会看到(正如其他人提到的)它是一个接口,而不是一个类。您最想要做的是查看“所有已知实现类”的同一JavaDoc页面,您将看到AbstractList。扩展这个。或者,扩展其中一个非抽象List实现。

Note: Most of the time when someone starts to extend one of the Java Collection classes, you're going down the wrong route. Usually, it's better to use one of the existing collections in your class and proxy any collections-style requests that you need. Or return an unmodifiable proxy of your collection:

注意:大多数情况下,当某人开始扩展其中一个Java Collection类时,您将走错路线。通常,最好使用类中的一个现有集合并代理所需的任何集合式请求。或者返回您的收藏的不可修改的代理:

public class MyClass {
  private final List<PhonebookEntry> myList = new LinkedList<PhonebookEntry>();

  public List<PhonebookEntry> getList() {
    return Collections.unmodifiableList(myList);
  }
}

Usually, it's best to extend a class only if you intend to have different behavior than the class you are extending. Inheritance is more brittle than composition.

通常,只有当您打算使用与您要扩展的类不同的行为时,才最好扩展类。遗传比构成更脆弱。