interface

Why can't I override my interface its methods?

蓝咒 提交于 2020-05-13 19:24:07
问题 Let's say I have an interface as follows. interface CardHolder : IEnumerable<Card> { /// <summary> ... void PutCard(Card card); /// <summary> ... void PutCards(Card[] card); /// Some more methods... } I implement it as follows. public class ArrayCardHolder : CardHolder { private Card[] _cards; private int _size = 0; public ArrayCardHolder(int capacity) { _cards = new Card[capacity]; } public void PutCard(Card card) { if (IsFull()) throw new Exception("This CardHolder is full. Capacity: " +

How to create an interface method with multiple return types

拥有回忆 提交于 2020-04-17 22:04:32
问题 This is a follow up to this question: Interface method with multiple return types I have two structs which are slightly different. One is about a trade struct, the other about a transfer struct. The goal is to calculate the quantity at the end. Also the trade struct shall implement some specific function not common to the transfer struct and the other way around. At the end they all call a get() function and ultimately return the quantity (type string). I can't come to do something like that

How to create an interface method with multiple return types

风格不统一 提交于 2020-04-17 22:04:22
问题 This is a follow up to this question: Interface method with multiple return types I have two structs which are slightly different. One is about a trade struct, the other about a transfer struct. The goal is to calculate the quantity at the end. Also the trade struct shall implement some specific function not common to the transfer struct and the other way around. At the end they all call a get() function and ultimately return the quantity (type string). I can't come to do something like that

How to find all objects that implement a interface and invoke a method

可紊 提交于 2020-04-16 06:05:09
问题 I have a UserControl that have a few child UserControl 's and those UserControl 's have child UserControl's. Consider this: MainUserControl TabControl TabItem UserControl UserControl UserControl : ISomeInterface TabItem UserControl UserControl UserControl : ISomeInterface TabItem UserControl UserControl UserControl : ISomeInterface TabItem UserControl UserControl UserControl : ISomeInterface This is what i have so far, but finds no ISomeInterface : PropertyInfo[] properties = MainUserControl

Is it possible to add an interface to an existing class in Kotlin?

假装没事ソ 提交于 2020-04-10 08:06:46
问题 Sometimes it is useful to create an interface that is already implemented by existing third-party code (ie. from a library). For example, we could imagine selecting a subset of the methods of the String class and declaring a GenericString . We might then define other GenericString s that implement these methods, but not other methods of the String class. The only problem would be that the String class doesn't inherit from the GenericString class. Is it possible to add an interface to an

(Laravel) Dynamic dependency injection for interface, based on user input

喜你入骨 提交于 2020-04-07 11:36:51
问题 I am currently facing a very interesting dilemma with my architecture and implementation. I have an interface called ServiceInterface which have a method called execute() Then I have two different implementations for this interface: Service1 and Service2 , which implements the execute method properly. I have a controller called MainController and this controller has a "type-hint" for the ServiceInterface ( dependency injection ), it means that both, Service1 and Service2 , can be called as

(Laravel) Dynamic dependency injection for interface, based on user input

北战南征 提交于 2020-04-07 11:36:46
问题 I am currently facing a very interesting dilemma with my architecture and implementation. I have an interface called ServiceInterface which have a method called execute() Then I have two different implementations for this interface: Service1 and Service2 , which implements the execute method properly. I have a controller called MainController and this controller has a "type-hint" for the ServiceInterface ( dependency injection ), it means that both, Service1 and Service2 , can be called as

convert struct pointer to interface{}

梦想与她 提交于 2020-04-07 11:05:08
问题 If I have: type foo struct{ } func bar(baz interface{}) { } The above are set in stone - I can't change foo or bar. Additionally, baz must converted back to a foo struct pointer inside bar. How do I cast &foo{} to interface{} so I can use it as a parameter when calling bar? 回答1: To turn *foo into an interface{} is trivial: f := &foo{} bar(f) // every type implements interface{}. Nothing special required In order to get back to a *foo , you can either do a type assertion : func bar(baz

Ensure a type implements an interface at compile time in Go

Deadly 提交于 2020-03-30 06:19:27
问题 How can I ensure that a type implements an interface at compile time? The typical way to do this is by failure to assign to support interfaces from that type, however I have several types that are only converted dynamically. At runtime this generates very gruff error messages, without the better diagnostics given for compile time errors. It's also very inconvenient to find at run time that types I expected to support interfaces, do in fact not. 回答1: Assuming the question is about Go, e.g. var

Ensure a type implements an interface at compile time in Go

邮差的信 提交于 2020-03-30 06:19:09
问题 How can I ensure that a type implements an interface at compile time? The typical way to do this is by failure to assign to support interfaces from that type, however I have several types that are only converted dynamically. At runtime this generates very gruff error messages, without the better diagnostics given for compile time errors. It's also very inconvenient to find at run time that types I expected to support interfaces, do in fact not. 回答1: Assuming the question is about Go, e.g. var