interface

C#: Abstract classes need to implement interfaces?

前提是你 提交于 2019-12-17 10:35:19
问题 My test code in C#: namespace DSnA { public abstract class Test : IComparable { } } Results in the following compiler error: error CS0535: 'DSnA.Test' does not implement interface member 'System.IComparable.CompareTo(object)' Since the class Test is an abstract class , why does the compiler require it to implement the interface? Shouldn't this requirement only be compulsory for concrete classes? 回答1: In C#, a class that implements an interface is required to define all members of that

Why are interfaces not [Serializable]?

亡梦爱人 提交于 2019-12-17 09:35:32
问题 I would think that adding that attribute to an interface would be helpful make sure you do not create classes that use the interface and forget to make them serializable. This could be a very fundamental question, but I wanted to ask the experts. 回答1: Interfaces define a contract and do not have any state of their own. Serialization is about saving and loading state into and out of an object model. Not much point to serializing something that holds no state. To answer the practical question

How to reference a generic return type with multiple bounds

爱⌒轻易说出口 提交于 2019-12-17 09:18:28
问题 I have recently seen that one can declare a return type that is also bounded by an interface. Consider the following class and interface: public class Foo { public String getFoo() { ... } } public interface Bar { public void setBar(String bar); } I can declare a return type like this: public class FooBar { public static <T extends Foo & Bar> T getFooBar() { //some implementation that returns a Foo object, //which is forced to implement Bar } } If I call that method from somewhere, my IDE is

interface as a method parameter in Java

依然范特西╮ 提交于 2019-12-17 08:23:09
问题 I had an interview days ago and was thrown a question like this. Q: Reverse a linked list. Following code is given: public class ReverseList { interface NodeList { int getItem(); NodeList nextNode(); } void reverse(NodeList node) { } public static void main(String[] args) { } } I was confused because I did not know an interface object could be used as a method parameter. The interviewer explained a little bit but I am still not sure about this. Could somebody enlighten me? 回答1: This is in

Optional Methods in Java Interface

和自甴很熟 提交于 2019-12-17 08:16:07
问题 From my understanding if you implement an interface in java, the methods specified in that interface have to be used by the sub classes implementing the said interface. I've noticed that in some interfaces such as the Collection interface there are methods which are commented as optional, but what exactly does this mean? Its thrown me a bit as I thought all methods specified in the interface would be required? 回答1: There seems to be an awful lot of confusion in the answers here. The Java

Non Public Members for C# Interfaces [closed]

孤者浪人 提交于 2019-12-17 07:28:52
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . In C#, when you implement an interface, all members are implicitly public. Wouldn't it be better if we could specify the accessibility modifier ( protected , internal , except private of course), or should we just use an abstract class instead? 回答1: If an interface is

Non Public Members for C# Interfaces [closed]

巧了我就是萌 提交于 2019-12-17 07:28:09
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . In C#, when you implement an interface, all members are implicitly public. Wouldn't it be better if we could specify the accessibility modifier ( protected , internal , except private of course), or should we just use an abstract class instead? 回答1: If an interface is

Java generics - Make Generic to extends 2 interfaces

自作多情 提交于 2019-12-17 07:14:40
问题 How do you make this work: public class Frankenstein<T extends IHuman, IMonster>{ } Without making public interface Weirdo extends Ihuman, IMonster{ } Edit Why is this not working? public <T> void mapThis( Class<? extends MyClass<T>> key, Class<? extends T & IDisposable> value) { } I am getting compiler message marking Class<? extends T & IDisposable> as an Error. 回答1: Reimeus already pointed out that what you're asking for in your edit isn't possible. I'd just like to expand a little on why.

Java Reflection: Create an implementing class

时光怂恿深爱的人放手 提交于 2019-12-17 07:04:45
问题 Class someInterface = Class.fromName("some.package.SomeInterface"); How do I now create a new class that implements someInterface ? I need to create a new class, and pass it to a function that needs a SomeInterface as an argument. 回答1: Creating something which pretends to implement an interface on the fly actually isn't too hard. You can use java.lang.reflect.Proxy after implementing InvocationHandler to handle any method calls. Of course, you could actually generate a real class with a

Why do we need interfaces in Java? [closed]

旧时模样 提交于 2019-12-17 06:24:08
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago . In Java to implement multiple inheritance we use interfaces. Is it the only use of interfaces? If yes, what is the main use of interface in Java? Why do we need interfaces in Java? 回答1: I would say the main use is polymorphism, or the ability to perform the same operation on