interface

Generic type parameter covariance and multiple interface implementations

自作多情 提交于 2019-12-17 21:49:10
问题 If I have a generic interface with a covariant type parameter, like this: interface IGeneric<out T> { string GetName(); } And If I define this class hierarchy: class Base {} class Derived1 : Base{} class Derived2 : Base{} Then I can implement the interface twice on a single class, like this, using explicit interface implementation: class DoubleDown: IGeneric<Derived1>, IGeneric<Derived2> { string IGeneric<Derived1>.GetName() { return "Derived1"; } string IGeneric<Derived2>.GetName() { return

Singleton and Interface implementation

半城伤御伤魂 提交于 2019-12-17 21:32:47
问题 I have an interface class Interface with pure virtual methods. class Interface { public: virtual void DoSomething() = 0; } Also, there is a class Implementation that implements the interface. class Implementation : public Interface { void DoSomething(); } In my program I need to have a Singleton (single instance) of Implementation/Interface pair. There are many pairs of Implementation/Interface classes in the program, but very few Implementation classes for one Interface class. QUESTIONS:

Java ArrayList of ? extends Interface

时光毁灭记忆、已成空白 提交于 2019-12-17 21:32:06
问题 I have a group of classes that all implement a validation interface which has the method isValid() . I want to put a group of objects--all of different classes--into an ArrayList, loop through them and call isValid() on each. Here's my code Email email = new email(); Address address = new Address(); ArrayList<? extends Validation> myValidationObjects = new ArrayList(); But when I try to do: myValidationObjects.add(email); I get: The method add(capture#2-of ? extends Validation) in the type

Varieties of @interface declarations, some with parentheses

情到浓时终转凉″ 提交于 2019-12-17 21:02:59
问题 I've noticed a variety of @interface declarations for Objective-c classes. I'd like to understand why developers declare @interface in the following ways: // in the .h file @interface MyClass : NSObject // ... @end // in the .m file (what's the purpose of the parens?) @interface MyClass () // more property declarations which seem like they can go in the .h file @end // again in the .m file (what's the purpose of private?) @interface MyClass (Private) // some method declarations @end 回答1: This

How much interfaces a class file can implement [closed]

♀尐吖头ヾ 提交于 2019-12-17 20:35:05
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . How many interfaces can a class file implement? Is there a limit on the number of interfaces used by a class file? Thanks in advance. 回答1: For all practical purposes, there is no limit on the number of interfaces

Dependency Injection - use with Data Transfer Objects (DTOs)?

寵の児 提交于 2019-12-17 20:11:06
问题 Consider the code below (which has been simplified). I have a service class that returns a list of specific DTO objects that each implement their own specific interface. In the actual code these are getting populated by iterating thru a Dataset as I'm working with legacy code. Questions: How do we create/use a DTO without newing them up or using the Service Locator anti-pattern? It doesn't make much sense to compose an empty DTO object in the Composition Root and inject it into the Service

Explicitly marking derived class as implementing interface of base class

五迷三道 提交于 2019-12-17 19:08:40
问题 interface IBase { string Name { get; } } class Base : IBase { public Base() => this.Name = "Base"; public string Name { get; } } class Derived : Base//, IBase { public Derived() => this.Name = "Derived"; public new string Name { get; } } class Program { static void Main(string[] args) { IBase o = new Derived(); Console.WriteLine(o.Name); } } In this case output will be "Base". If I explicitly state that Derived implements IBase (which is in fact already implemented by base class Base and such

Interface type cannot be statically allocated?

允我心安 提交于 2019-12-17 18:49:35
问题 I tried to put this in the header file of my view object: @property (nonatomic) UIColor color; to store the color that lines should be drawn with in this view. Xcode gives me an error on this line: Interface type cannot be statically allocated What does that mean, and what should I do? EDIT: I did add a *, and at the point of synthesis, it said: ARC forbid synthesizing a property of Objective C object with unspecified ownership or storage attribute? 回答1: Your variable is for an object type,

Unable to cast COM object of type exception

蹲街弑〆低调 提交于 2019-12-17 18:36:37
问题 I have the following code: public void Test(IMyInterface iInterface) { iInterface.CallMethod ( ); } Which works fine. However, if I change the code to be threaded: private IMyInterface myInterface; public void Test(IMyInterface iInterface) { myInterface = iInterface; new Thread ( new ThreadStart ( CallInterfaceMethod) ).Start ( ); } public void CallInterfaceMethod ( ) { myInterface.CallMethod ( ) } When i use the thread I receive the exception: Unable to cast COM object of type 'System._

How to create an object of an abstract class and interface

霸气de小男生 提交于 2019-12-17 18:35:51
问题 How do I create an object of an abstract class and interface? I know we can't instantiate an object of an abstract class directly. 回答1: You can not instantiate an abstract class or an interface - you can instantiate one of their subclasses/implementers. Examples of such a thing are typical in the use of Java Collections. List<String> stringList = new ArrayList<String>(); You are using the interface type List<T> as the type, but the instance itself is an ArrayList<T> . 回答2: To create object of