简体   繁体   English

无法访问子类方法,泛型是否不利? 或者我想念一些东西

[英]unable to access subclass method, is it disadvantage of Generics? or i am missing something

List<Employee> empLList = new LinkedList<>();
        empLList.add(...)
Iterator<Employee> descItr = empLList.descendingIterator();

in the above code i'm unable to access descendingIterator with following error 在上面的代码中,我无法访问以下错误的descendingIterator

cannot find symbol
  symbol:   method descendingIterator()
  location: variable empLList of type List<Employee>

to get descendingIterator i have to recast empLList to LinkedList 为了获得descendingIterator,我必须将empLList重铸为LinkedList

 Iterator<Employee> descItr = ((LinkedList) empLList).descendingIterator();

My question : In general is the above a disadvantage of using Generics, ie, everytime we need to cast the object back to subclasss to access subclass' methods or is Generics supposed to work like that. 我的问题:一般来说,上述是使用泛型的缺点,即,每次我们需要将对象强制转换回子类以访问子类的方法时,还是泛型应该像这样工作。

OR we should not use generics in case where we depend on too many of subclass' methods 否则,如果我们依赖太多子类的方法,则不应使用泛型

OR i am missing something 还是我想念一些东西

I am curious about the use of GENERICS in the example not the collections used. 我对示例中使用GENERICS而不是使用集合感到好奇。

It is not about Generics. 这与泛型无关。

Look at APIs. 查看API。 descendingIterator is method of Deque not List descendingIteratorDeque方法而不是List方法

LinkedList implements Deque LinkedList实现双端队列

My question : In general is the above a disadvantage of using Generics, ie, everytime we need to cast the object back to subclasss to access subclass' methods or is Generics supposed to work like that. 我的问题:一般来说,上述是使用泛型的缺点,即,每次我们需要将对象强制转换回子类以访问子类的方法时,还是泛型应该像这样工作。

Your snippet error is nowhere related to generics concepts. 您的代码段错误与泛型概念无关。 Logic based on casting is very very bad and should be replaced with polymorphism by correcting design. 基于转换的逻辑非常糟糕,应通过纠正设计将其替换为多态。

I am curious about the use of GENERICS in the example not the collections used. 我对示例中使用GENERICS而不是使用集合感到好奇。

Then ask right questions. 然后问正确的问题。

Change to following to make it work: 更改为以下内容以使其起作用:

Deque<Employee> empLList = new LinkedList<>();
empLList.add(...)
Iterator<Employee> descItr = empLList.descendingIterator();

This has nothing to do with generics. 这与泛型无关。 The List interface is a contract, defining the methods all of its implementations have to provide. List接口是一个约定,定义了其所有实现必须提供的方法。 Some, like LinkedList , may provide additional methods (such as the afore mentioned descendingIterator() ). 诸如LinkedList类的某些方法可能会提供其他方法(例如上述的descendingIterator() )。

Your list of employees is regarded as being any implementation of List , thus all the List methods are available for use, nothing more. 您的员工列表被视为List任何实现,因此所有List方法都可以使用,仅此而已。 If you know your implementation is LinkedList , you may cast , but this is bad practice . 如果您知道自己的实现是LinkedList ,则可以进行 强制转换 ,但这是一种不好的做法 Better keep it as LinkedList then: 最好将其保留为LinkedList然后:

LinkedList<Employee> empLList = new LinkedList<>();
empLList.add(...)
Iterator<Employee> descItr = empLList.descendingIterator();

... or (if you accept any List , but want to use the descendingIterator() ), create a new LinkedList out of it: ...或(如果您接受任何List ,但要使用descendingIterator() ),请在其中创建一个新的LinkedList

List<Employee> empLList = ... // any implementation 
empLList.add(...) 
Iterator<Employee> descItr = new LinkedList(empLList).descendingIterator();

It is not because of Generics. 这不是因为泛型。 It is because, descendingIterator() method is not available in List. 这是因为,ListingIterator()方法在List中不可用。 In this line 在这条线

  Iterator<Employee> descItr = empLList.descendingIterator();

You are calling the method on a list type referenece variable. 您正在列表类型referenece变量上调用该方法。 As a result you are getting compile error. 结果,您得到编译错误。 When it comes to the below line 当涉及到下一行时

  Iterator<Employee> descItr = ((LinkedList) empLList).descendingIterator();

You are casting your empList to LinkedList and method is available in LinkedList as it implements Deque(Implementation of descendingIterator() is provided in LinkedList) 您正在将empList强制转换为LinkedList,并且由于LinkedList实现了Deque,因此该方法在LinkedList中可用(LinkedList中提供了DescendingIterator()的实现)

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

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