extends

Java wildcards and generics ? super T and ? extends T

最后都变了- 提交于 2019-12-06 12:29:46
问题 when dealing with wildcards such as setting/adding a generic item to a certain container is it suggested to use something like this? void add(List<? super T> someList,someitem){ someList.add(someItem); } and when retrieving an item it is suggested to use something like this <T> void f1(List<? extends T> obj, T item) { obj.add(item); } What is the principle behind this? and when will I know if I should use this ? 回答1: you should have a look at the explanation of PECS principle What is PECS

How to extends Sonata\\DoctrineORMAdminBundle\\Model\\ModelManager

喜你入骨 提交于 2019-12-06 01:58:48
I want some changes in ModelMangaer then I was extending ModelManager but It's not working. I don't know why ? Any one tell me why it is not working? File where I extend Sonata\DoctrineORMAdminBundle\Model\ModelManager-> <?php use Sonata\DoctrineORMAdminBundle\Model\ModelManager; class ModelManager extends ModelManager { /** * {@inheritdoc} */ public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid) { $values = $datagrid->getValues(); $values = $_GET['filter']; if ($fieldDescription->getName() == $values['_sort_by']) { if ($values['_sort_order

declare parameter subtype in Java interface, use subtypes in Java implementing methods

半腔热情 提交于 2019-12-05 20:08:37
I want to declare a method in an interface where the parameter of the method defined in implementing classes can be a subtype of a specific java class for example: interface Processor{ processRequest( Request r); } public class SpecialRequest extends Request{...} public class SpecialProcessor implements Processor{ processRequest(SpecialRequest r){...} } but I get errors in the SpecialProcessor because it doesn't properly implement the Processor interface. What can I change in the Processor interface to allow the definition in the SpecialProcessor to work? You can type Processor : public

Understanding Classes in PHP

南楼画角 提交于 2019-12-05 19:57:19
I'm officially mentally retarded. [Let me explain] I've never really given classes and their relationship any thought until today. I'm trying to figure out something that seems to be pretty obvious but since I'm stupid I can't see it. Let's say I have a Core Class that will be extended from different files. How can children classes call functions from other siblings (that is if those are considered childs at all). Sample Code: (don't kill me) class cOne { public function functionOne(){ echo "something"; } } Then on another file, I say: class cOneChildOne extends cOne { public function

What is the difference between using <? extends SomeAbstract> vs. SomeAbstract in java generics

自古美人都是妖i 提交于 2019-12-05 17:12:50
I'm moving over to java from DotNet and this idea of extends is new. I've seen some posts that fully explain using List<? extends SomeAbstract> vs. List<? super SomeAbstract> vs. List<SomeAbstract> , but I'm guessing that there is no difference between using, and not using, extends in generics. Is that true? Would the answer change if using an abstract class as the parent? class My_AbstractExtends<T extends SomeAbstract> vs. class My_Abstract<SomeAbstract> EDIT Creates child classes as follows class My_ChildExtends extends My_AbstractExtends<ConcreteChildOfSomeAbstract> vs. class My_Child

Returning Collection<? extends Type> vs Collection<Type>

旧城冷巷雨未停 提交于 2019-12-05 16:23:20
What is the difference between these two methods? Collection<Type> getTypes(); vs Collection<? extends Type> getTypes(); Does it matter if Type is a class or an interface? Especially, when designing an API, which version would be preferred and why? Collection<Type> getTypes(); Here, getTypes() must return a Collection<Type> (e.g. ArrayList<Type> or HashSet<Type> ). Collection<? extends Type> getTypes(); Here, getTypes() can return a Collection of anything that is or extends Type , (e.g. ArrayList<SubType> or HashSet<SubType> ). So anything that can be returned from the first variant can also

Java - using the 'super' keyword

╄→尐↘猪︶ㄣ 提交于 2019-12-05 11:11:22
Simple question. I made a class called Tester1 which extends another called Tester2. Tester2 contains a public string called 'ABC'. Here is Tester1: public class Tester1 extends Tester2 { public Tester1() { ABC = "Hello"; } } If I instead change line 5 to super.ABC = "Hello"; am I still doing the exact same thing? Yes. There's only one ABC variable within your object. But please don't make fields public in the first place. Fields should pretty much always be private. If you declared a variable ABC within Tester1 as well, then there'd be a difference - the field in Tester1 would hide the field

Generic super class in java

回眸只為那壹抹淺笑 提交于 2019-12-05 10:41:39
Is it possible to create something like this in java public abstract class GenericView<LAYOUTTYPE extends AbstractLayout> extends LAYOUTTYPE so that public class MyView extends GenericView<HorizontalLayout> extends GenericView and HorizontalLayout and public class MyView2 extends GenericView<VerticalLayout> extends GenericView and VerticalLayout ? The short answer - no. The type you extends must be an actual type, not a generic type parameter. It sounds like you want to accomplish multiple inheritance, inheriting from both a View and a Layout . This is not possible in Java. You can accomplish

How do I mock a method inherited from an abstract class with EasyMock?

淺唱寂寞╮ 提交于 2019-12-05 10:10:26
I'm struggling with EasyMock. I've written two small classes to illustrate my problem: public abstract class A { private AtomicReference<Integer> id = new AtomicReference<Integer>(null); public final int getId() { return id.get(); } public final boolean setId(int id) { return this.id.compareAndSet(null, id); } } public class B extends A { } Then I proceed to write a test method as follows: public class EasyMockTester extends EasyMockSupport { @Test public void test() { B b = EasyMock.createStrictMock(B.class); EasyMock.expect(b.getId()).andReturn(100); replayAll(); int id = b.getId(); System

Generics in Java

不想你离开。 提交于 2019-12-05 05:30:02
I would like to understand the following type of syntax. Example: public interface A < T extends A < T> > { } What is the logic of this interface ? This would be used as follows: class X implements A<X> { /* ... */ } In other words, you are forced to make the parameter of A the class X itself, and something like class X implements A<Unrelated> is forbidden. This construction gives the interface access to X through the generic parameter, and the type restriction makes sure that it doesn't get abused. For instance, T can now be assumed to expose all methods that A does. Note that this