abstract

Overriding abstract property using more specified return type (covariance)

假装没事ソ 提交于 2019-12-29 09:08:11
问题 class Base {} abstract class A { abstract public List<Base> Items { get; set; } } class Derived : Base {} class B : A { private List<Derived> items; public override List<Derived> Items { get { return items; } set { items = value; } } } The compiler says that B.Items must be List of Base elements "to match overridden member" A.Items. How can i make that work? 回答1: What you've tried to accomplish initially is impossible - .NET does not support co(contra)variance for method overload. The same

Java abstract classes which throw

随声附和 提交于 2019-12-29 09:04:33
问题 If I have an abstract class with the following function - abstract class A{ void foo(String s) throws Exception{ throw new Exception("exception!"); } } And then another class that extends the abstract class and implements its own version of foo - class B extends A{ void foo(String s){ //do stuff that does *not* throw an exception } } Will this create problems? Specifically in the following test case - Collection<A> col = new Collection<A>(); B b = new B(); col.add(b); for(A a : col){ a.foo();

Java abstract classes which throw

 ̄綄美尐妖づ 提交于 2019-12-29 09:04:01
问题 If I have an abstract class with the following function - abstract class A{ void foo(String s) throws Exception{ throw new Exception("exception!"); } } And then another class that extends the abstract class and implements its own version of foo - class B extends A{ void foo(String s){ //do stuff that does *not* throw an exception } } Will this create problems? Specifically in the following test case - Collection<A> col = new Collection<A>(); B b = new B(); col.add(b); for(A a : col){ a.foo();

What are the differences between Hashmap vs Hashtable in theory?

ぐ巨炮叔叔 提交于 2019-12-29 08:11:54
问题 Are there are differences between hashmap and hashtable in theory? I don't mean in the concrete definitions given in Java (or the implementation), but in theory. Isn't a hashtable a map that uses hashing ... hence a hashmap? 回答1: According to Wikipedia, they are the same: In computing, a hash table (hash map) is a data structure used to implement an associative array (...) According to Wikibooks, it's the same: A hash table, or a hash map, is a data structure that associates keys with values.

Class is not Abstract and does not Override error in Java

僤鯓⒐⒋嵵緔 提交于 2019-12-28 02:14:05
问题 I am getting a compile time error with Java: MyClass is not abstract and does not override abstract method onClassicControllerRemovedEvent( wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerRemovedEvent) in wiiusejevents.utils.WiimoteListener) Here is the class: import wiiusej.WiiUseApiManager; import wiiusej.Wiimote; import wiiusej.wiiusejevents.physicalevents.ExpansionEvent; import wiiusej.wiiusejevents.physicalevents.IREvent; import wiiusej.wiiusejevents.physicalevents

Why would one declare a Java interface method as abstract?

China☆狼群 提交于 2019-12-27 11:39:41
问题 I used the "pull interface" refactoring feature of Eclipse today to create an interface based on an existing class. The dialog box offered to create all the new methods of the new interface as "abstract" methods. What would be the benefit of that? I thought that the fact that you were allowed to declare interface methods as abstract was a superfluous and harmless feature of the language that is not particularly encouraged. Why would Eclipse support such a style, or why would someone

C# call abstract method from static method

*爱你&永不变心* 提交于 2019-12-25 19:05:24
问题 i am writing library, that can be used for with different database engines. Abstract class has abstract DbParameter constructor and uses classes from System.Data.Common. Now i have sutch structure : public abstract class ASqlWorker{ abstract protected DbParameter DbParameterConstructor(String paramName, Object paramValue); } now, can i call abstract DbParameterConstructor from a static method of ASqlWorker? Logicaly, i could make abstractMethod static (it creates instance of an inheriter of

In an abstract class does the “this” keyword refrerence the parent or child class?

我是研究僧i 提交于 2019-12-25 05:32:13
问题 I have an abstract class Flight. Flight contains the method schedule() which calls the private method schedule(final Flight f) public void schedule() { schedule(this); } private void schedule(final Flight f) { new Timer().schedule(new TimerTask() { @Override public void run() { f.checkIn(); updateList(); } }, this.getDate()); } Lets now say I have a class SouthWestFlight that extends Flight Flight f = new SouthWestFlight(); //ignore the missing params doesn't matter for example f.schedule();

Crystal how to require implementing class operate on self, instead of all siblings

空扰寡人 提交于 2019-12-25 02:25:22
问题 Let's say I want my method to accept anything that is "number like" i.e. knows how to negate, add, subtract, multiply and divide. It needs to do these with itself and with numbers (Int32 and Float64 for my purposes) abstract struct Numberlike alias Num = (Int32 | Float64) abstract def - abstract def +(other : self) abstract def +(other : Num) abstract def -(other : self) abstract def -(other : Num) abstract def *(other : self) abstract def *(other : Num) abstract def /(other : self) abstract

Cloning an instantiated abstract class

强颜欢笑 提交于 2019-12-24 17:19:12
问题 Instantiate an abstract class. -> Duplicate it, preserving the implementation of the abstract method. public abstract class Foo{… public int myVar; public abstract void do(); }//Foo elsewhere in package: public class Gha{ … Foo testFoo = new Foo{ @Override public void do(){ Log.i("" , "this is a special Foo"); //executable process }//do }//testFoo … }//Gha Can I copy testFoo in a way that copies testFoo ’s .do() method body for the new Foo ? 回答1: You could use clone . The use of this method