interface

toString(), equals(), and hashCode() in an interface

白昼怎懂夜的黑 提交于 2019-12-28 11:48:09
问题 So, I have an interface with a bunch of methods that need to be implemented, the method names are irrelevant. The objects that implement this interface are often put into collections, and also have a special toString() format that I want them to use. So, I thought it would be convenient to put hashCode(), equals(), and toString() into the interface, to make sure that I remember to override the default method for these. But when I added these methods to the interface, the IDE/Compiler doesn't

What's the difference between IComparable & IEquatable interfaces?

痴心易碎 提交于 2019-12-28 08:02:10
问题 both the interfaces seem to compare objects for equality, so what's the major differences between them? 回答1: IEquatable tests whether two objects are equal. IComparable imposes a total ordering on the objects being compared. For example, IEquatable would tell you that 5 is not equal to 7. IComparable would tell you that 5 comes before 7. 回答2: IEquatable<T> for equality. IComparable<T> for ordering. 回答3: In addition to Greg D's answer: You might implement IComparable without implementing

Implementing the same interface at different generic instantiations

浪尽此生 提交于 2019-12-28 06:29:05
问题 In C#, I can implement a generic interface twice on one class, using two different type-parameters: interface IFoo<T> { void Foo(T x); } class Bar : IFoo<int>, IFoo<float> { public void Foo(int x) { } public void Foo(float y) { } } I would like to do the same thing in F#: type IFoo<'a> = abstract member Foo : 'a -> unit type Bar() = interface IFoo<int> with [<OverloadID("int")>] member this.Foo x = () interface IFoo<float> with [<OverloadID("float")>] member this.Foo x = () But it gives a

Why do most system architects insist on first programming to an interface?

倖福魔咒の 提交于 2019-12-28 05:14:09
问题 Almost every Java book I read talks about using the interface as a way to share state and behaviour between objects that when first "constructed" did not seem to share a relationship. However, whenever I see architects design an application, the first thing they do is start programming to an interface. How come? How do you know all the relationships between objects that will occur within that interface? If you already know those relationships, then why not just extend an abstract class? 回答1:

What exactly is “interface based programming”?

丶灬走出姿态 提交于 2019-12-28 05:05:43
问题 I often hear/read about interfaced based programming but I am not exactly clear on what that really means. Is interfaced based programming an actual stand alone topic that actually has books written about it? If so, can anyone recommend any good ones? I came across interface based programming as I was reading about how good APIs are designed and would like to learn more about it. Right now I am not clear how to properly go about designing an API around interfaces. Any info is greatly

Are GUIDs necessary to use interfaces in Delphi?

给你一囗甜甜゛ 提交于 2019-12-28 04:13:24
问题 The official documentation says they are optional. I know COM interop requires a unique identifier for each interface but every interface example I see has a GUID whether it's used with COM or not? Is there any benefit to including a GUID if its not going to be used with COM? 回答1: I've noticed that some methods such as Supports (to determine if a class conforms to a specific interface) require that you define a GUID before you can use them. This page confirms it with the following information

Extend interface defined in .d.ts file

蹲街弑〆低调 提交于 2019-12-28 03:01:27
问题 In my TypeScript project, I use DefinitelyTyped definitions for external js dependencies. Sometimes it might happen that these definitions are outdated. It might also happen than some libraries can add new methods at runtime, like express-validator in which you can define custom validator functions. Therefore I would like to extend those .d.ts definitions adding new methods and/or properties. So if I have my DefinitelyTyped defininiton in express-validator.d.ts : declare module

Implementing TypeScript interface with bare function signature plus other fields

北慕城南 提交于 2019-12-27 23:37:23
问题 How do I write a class that implements this TypeScript interface (and keeps the TypeScript compiler happy): interface MyInterface { (): string; text2(content: string); } I saw this related answer: How to make a class implement a call signature in Typescript? But that only works if the interface only has the bare function signature. It doesn't work if you have additional members (such as function text2) to be implemented. 回答1: A class cannot implement everything that is available in a

Why are all fields in an interface implicitly static and final?

我只是一个虾纸丫 提交于 2019-12-27 18:20:42
问题 I am just trying to understand why all fields defined in an Interface are implicitly static and final . The idea of keeping fields static makes sense to me as you can't have objects of an interface but why they are final (implicitly)? Any one knows why Java designers went with making the fields in an interface static and final ? 回答1: An interface can't have behavior or state because it is intended to specify only an interaction contract, no implementation details. No behavior is enforced by

How to write junit tests for interfaces?

帅比萌擦擦* 提交于 2019-12-27 16:49:27
问题 What is the best way to write junit tests for interfaces so they can be used for the concrete implementing classes? e.g. You have this interface and implementing classes: public interface MyInterface { /** Return the given value. */ public boolean myMethod(boolean retVal); } public class MyClass1 implements MyInterface { public boolean myMethod(boolean retVal) { return retVal; } } public class MyClass2 implements MyInterface { public boolean myMethod(boolean retVal) { return retVal; } } How