简体   繁体   English

什么时候使用扩展或实现Comparable(Java)? +为什么我不能创建对象

[英]when to use extends or implements Comparable (Java) ? + why I cannot create object

I am Studying Data Structures and I was asked to write a program that allows a store manager to manipulate an inventory , using 4 classes : ListInterface , ExpandableArrayList ( a class that implements the interface) , class item ( which is the type stored in the Arraylist) , and of course, a Test class. 我正在研究数据结构,被要求编写一个程序,该程序允许商店经理使用以下四个类来操纵库存:ListInterface,ExpandableArrayList(实现接口的类),类项目(即存储在Arraylist中的类型) ),当然还有Test类。

some methods require the use of compareTo . 有些方法需要使用compareTo that is , being Comparable , but I don't know what class exactly should be Comparable ? 也就是说,是可比的 ,但是我不知道什么班级应该是可比的? and if I should write Implements or extends Comparable ? 以及我应该编写Implements还是扩展 Comparable?

these are the class headers I have right now : 这些是我现在拥有的类头文件:

public interface ListInterface<T extends Comparable <?super T >> {... }

public class ExpandableArrayList <T extends Comparable <? super T >>
implements ListInterface <T> { ...... } 

public class Item<T extends Comparable<T>> {... } 

but for some reason, I cannot create an Object in the Test class. 但是由于某种原因,我无法在Test类中创建对象。 when I type: 当我键入:

ListInterface<Item> inventoryList= new ExpandableArrayList<Item>(); 

I get the following errors : 我收到以下错误:

Test.java:9: error: type argument Item is not within bounds of type-variable T 
ListInterface<Item> inventoryList= new ExpandableArrayList<Item> () ; 
where T is a type-variable:
T extends Comparable<? super T> declared in interface ListInterface


Test.java:9: error: type argument Item is not within bounds of type-variable T 
ListInterface<Item> inventoryList= new ExpandableArrayList<Item> () ;
where T is a type-variable:
T extends Comparable<? super T> declared in class ExpandableArrayList

How Can I solve this? 我该如何解决? what exactly should be changed? 究竟应该更改什么? .. ..

thanks a lot in advance. 非常感谢。

T which is your type Item needs to implement Comparable. T是您的Item类型,需要实现Comparable。 This will allow the ExpandableArrayList class to run the compareTo method on elements of type Item using the comparison provided by that class. 这将允许ExpandableArrayList类使用该类提供的比较在Item类型的元素上运行compareTo方法。 When you implement compareTo from Comparable you have to give a way for comparing different Items, this should be based on the attributes of the Item class ideally. 当从Comparable实现compareTo时,必须提供一种比较不同Item的方法,这应该理想地基于Item类的属性。

public class Item implements Comparable<Item> {... }

This is what the class def would look like. 这就是类def的外观。

You have to write implements for interfaces and extends for classes. 您必须为接口编写implements ,并为类编写extends工具。

Inheritance tutorial 继承教程

Interfaces tutorial 接口教程

It should be like this - 应该是这样-

public interface ListInterface<T extends Comparable<? super T>> {... }

public class ExpandableArrayList<T extends Comparable<? super T>> implements ListInterface <T extends Comparable<? super T>> { ...... } 

public class Item implements Comparable<Item> {... } 

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

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