interface

In Haxe, can you write a generic interface where a method type parameter is constrained by the class's type parameter?

♀尐吖头ヾ 提交于 2020-05-30 03:53:12
问题 I'm having trouble writing the generic interface below. In my class, I have a function that takes an array of < any type that extends a parent class > and traces its first element. Since I'm only reading elements from the array, I'm using it as if its a covariant compound type, and therefore I'm guaranteed to have the cast statement never fail. Now I want to abstract this out even more and write a interface that defines fn using another generic type T. I want fn to be able to accept any Array

Specify implement interface on derived class when base class implements it abtract

…衆ロ難τιáo~ 提交于 2020-05-29 05:16:11
问题 I was wondering if there are any (runtime) difference if I implement an interface on a derived class if the base abstract class implements it already abstract: public interface IFoo { void Bar(); } public abstract class Base : IFoo { public abstract void Bar(); } public class Derived : Base, IFoo // does this make any difference? { public override void Bar() { // do something } } Is there any difference writing the "implemention" IFoo on Derived for example when I check if an instance

Java Interface as argument to a method

空扰寡人 提交于 2020-05-28 06:53:09
问题 In some Java methods I see that an Interface argument is required. Since an Interface cannot be instantiated, does that mean that an object of every class that implements this interface can be passed as argument? For example in the setOnClickListener method of the ListView class in Android, setOnItemClickListener(AdapterView.OnItemClickListener listener) the OnItemClickListener interface (nested in the AdapterView abstract class) is needed as an argument. What kind of object is required to be

Force derived class to implement interface

本秂侑毒 提交于 2020-05-27 03:26:10
问题 I am here today (like yesterday) with another weird interface question. I have a class: public class InputDevice<T> where T : Button { protected List<T> buttons = new List<T>(); protected InputDevice() { //Only allow instanciation from within a derived class } } As you can see, this class cannot be instantiated. A class that derives from it might be able to be instantiated. This is a subclass: public sealed class Keyboard : InputDevice<KeyboardButton> { public Keyboard() { buttons.Add(new

Nil pointer to struct not deep equal to nil?

丶灬走出姿态 提交于 2020-05-23 17:38:44
问题 If I have a struct containing a nil pointer of type A , using reflect.DeepEqual to check if that property is nil will result in false , which strikes me as odd behaviour. type Container struct { O *Obj } type Obj struct { Message string } var c Container eq := reflect.DeepEqual(c.O, nil) fmt.Printf("O value: %v, is nil: %t", c.O, eq) // Prints: "O value: <nil>, is nil: false" Specifically, I am marshaling a JSON object into a struct, where I would like to test that a specific property is nil

When you implement two interfaces with the same method, how do you know which one is called?

瘦欲@ 提交于 2020-05-23 08:54:26
问题 If you have TheMethod() in interfaces I1 and I2, and the following class class TheClass : I1, I2 { void TheMethod() } If something instantiates TheClass , how does it know which interface it is using? 回答1: If that's how the client code is using the class, it doesn't really matter. If it needs to do something interface specific, it should declare the interface it needs, and assign the class to that e.g. I1 i = new TheClass() i.TheMethod(); Of course, using your current implementation TheClass

A suitable constructor for type 'ApplicationInventory.Services.ServerInventoryService' could not be located

丶灬走出姿态 提交于 2020-05-17 04:30:32
问题 I am trying to create a service that is accessible by both Controllers and regular functions for all API calls to a specific endpoint for my ASP.Net Core 2.2 application. Whenever I try and inject the HttpClient class an error occurs before the functions can be reached/ I have setup my startup.cs as shown in documentation and have reduced the constructor inputs to just HttpClient so I know that's where the root of the issue is. I have added the HttpClient to the configure services section fo

reflect value Interface and pointer receiver

假装没事ソ 提交于 2020-05-15 08:54:06
问题 In the mongodb driver for golang there is the following piece of code: case reflect.Struct: if z, ok := v.Interface().(Zeroer); ok { return z.IsZero() } return false Interface Zeroer is defined like this: type Zeroer interface { IsZero() bool } When I implement my struct with func (id SomeStruct) IsZero() bool { return id.ID == "" } it works. But when I implement the IsZero method with a pointer receiver: func (id *SomeStruct) IsZero() bool { return id.ID == "" } the type assertion fails and

Kotlin: Specify input-constraints in interface

孤街浪徒 提交于 2020-05-15 06:22:04
问题 Lets say I have the following interface: interface MathThing { fun mathFunction(x : Int) } Let's say the constraint I want to put onto this function is that x cannot be negative. How can I make sure that every time this (or any other arbitrary) condition isn't met on a object of type MathThing, a (custom) exception is thrown? 回答1: One way is to use a wrapper class for your function parameters. You can make an extension function so it's a little easier to pass values to the function. data

Creating Objects for the interface directly using new keyword

无人久伴 提交于 2020-05-14 14:42:49
问题 The following code compiles successfully without any compilation errors but how can we create Array(object) for Interface using new Keyword with the interface name. Objects can be created for the implementing class and can be referred by the interface like interface MyInterface{} class MyClass1 implements MyInterface{} class MyClass2 implements MyInterface{} class Test{ MyInterface[] interfaceArray=new MyClass1[7]; //Valid reason } Now the other case is creating object for the Interface