标签: interface

Collection方法的定义在哪里?

我知道Collection是一个包含抽象方法的接口,例如add(),addAll(),..
而Collections是一个包含静态方法的类,例如addAll(),max(),..,它扩展了Object.和Collection实现了Iterable接口.

接口,因为它只能包含抽象方法,只包含抽象方法(不具有定义).

所以,我的问题是如果List,Set,..正在实现Collection接口那么将在哪里定义add(),addAll(),remove(),contains(),iterator(),.. write等方法?

java collections interface

0
推荐指数
1
解决办法
57
查看次数

使用'generic'方法创建一个接口,但它的泛型类型是接口的实现者

在C#中是否有任何方法可以使用通用类型,它始终是接口中的实现类型?像这样的东西:

interface Foo
{
    this GetOtherThis();
}

class Bar : Foo
{
    Bar GetOtherThis();
}
Run Code Online (Sandbox Code Playgroud)

c# generics interface

0
推荐指数
1
解决办法
69
查看次数

为什么界面有用?

我确切地知道接口和抽象类之间有什么区别,但为什么接口有用呢?看这个 :

接口示例

java abstract-class interface

0
推荐指数
1
解决办法
1447
查看次数

转换为重新实现接口的基接口实现

我有一个IFace由类实现的接口Animal.我无法修改IFace或Animal.我希望扩展Animal我的子类Human并重新实现它IFace.但是,我希望能够Human在类型变量中存储一个实例,IFace以便访问该变量Animal直接使用实现 - 就像变量的类型一样Animal.

https://dotnetfiddle.net/6JjNfR

using System;

interface IFace
{
    void Talk();
}
class Animal : IFace
{
    public void Talk()
    {
        Console.WriteLine("Oink");
    }
}
class Human : Animal, IFace
{
    public new void Talk()
    {
        Console.WriteLine("Hi");
    }
}

public class Program
{
    public static void Main()
    {
        var human = new Human();
        human.Talk();

        var face = (IFace)human;
        face.Talk(); …
Run Code Online (Sandbox Code Playgroud)

c# casting interface

0
推荐指数
1
解决办法
377
查看次数

多次实现事件的通用接口

我有一个通用接口,看起来像这样:

public interface IObjectProvider<out T>
{
    event Action<T> ValueOccured;
}
Run Code Online (Sandbox Code Playgroud)

现在我想创建一个类,它实现这个接口两次,如下所示:

public class GlobalReceiver : IObjectProvider<Foo>, IObjectProvider<Bar>
{
    public event Action<Foo> IObjectProvider<Foo>.ValueOccured;

    public event Action<Bar> IObjectProvider<Bar>.ValueOccured;
}
Run Code Online (Sandbox Code Playgroud)

并且无法让它工作,因为对于这个编译器说我需要使用正确的语法,如果我进行非显式实现我(如预期的那样)会得到一个错误:

会员已经宣布.

我怎么能弄清楚这个问题?

.net c# generics inheritance interface

0
推荐指数
1
解决办法
381
查看次数

通用论证

我想用一个接受类型的所有子类型的方法创建一个Java接口:

interface A{};

interface B{
  void method(A a);
}
Run Code Online (Sandbox Code Playgroud)

我想要实现的是使方法(A a)的实现接受A的所有子类型(如返回类型多态,但带有参数).

这甚至可能吗?

java polymorphism interface

0
推荐指数
1
解决办法
45
查看次数

如何调用这些接口方法?

只是想知道如何为我的学习目的打电话.我知道在不使用IIm或IIn.Display时通过对象创建来调用它(即只使用public void Display();)但是,我不知道如何调用它.

public interface IIn
{
    void Display();
}
public interface IIM : IIn
{
    void Display();
}
public class temp : IIM
{
    void IIM.Display()
    {
        Console.WriteLine("Displaying 1");
    }

     void IIn.Display()
     {
         Console.WriteLine("Displaying 2 ");
     }

    static void Main()
    {
        temp t = new temp();

        Console.ReadLine();
    }
Run Code Online (Sandbox Code Playgroud)

c# oop interface c#-3.0 c#-4.0

0
推荐指数
1
解决办法
48
查看次数

C#确保接口实现继承自特定的基类

我有一个界面

public interface IInputReciever {
    void OnRecieveInput(InputInfo Info);
}
Run Code Online (Sandbox Code Playgroud)

和一个接口扩展类

public static class IInputRecieverExtensions {
    public static void SubscribeToInput(this IInputReciever Reciever) { 
       //Use member of X
    }
    public static void UnsubscribeFromInput(this IInputReciever Reciever) {
       //Use member of X
    }
}
Run Code Online (Sandbox Code Playgroud)

假设我有两个A和B类; 但是,虽然只有B继承自X,但都实现了我的界面.

public class B : X, IInputReciever {
   //Contains members of B and X
}

public class A : IInputReciever {
   //Contains members of A only
}
Run Code Online (Sandbox Code Playgroud)

在我的接口扩展中,我想使用X的成员,但是这不会编译,因为实现者不是X的子类,也不能保证是一个.

我可以以某种方式强制我的实现者从X派生以使这个场景工作 - 或者有更好的方法来解决这个问题吗?

c# inheritance extension-methods interface

0
推荐指数
1
解决办法
156
查看次数

检查对象是否实现特定的通用接口

我有多个类(简化用于解释目的):

public class A : BaseClass,
    IHandleEvent<Event1>,
    IHandleEvent<Event2>
{
}

public class B : BaseClass,
    IHandleEvent<Event3>,
    IHandleEvent<Event4>
{
}

public class C : BaseClass,
    IHandleEvent<Event2>,
    IHandleEvent<Event3>
{
}
Run Code Online (Sandbox Code Playgroud)

在我的"BaseClass"中,我有一个方法,我想检查Child-class是否实现IHandleEvent了特定事件.

public void MyMethod()
{
    ...
    var event = ...;
    ...
    // If this class doesn't implement an IHandleEvent of the given event, return
    ...
}
Run Code Online (Sandbox Code Playgroud)

从这个SO答案我知道如何检查对象是否实现了通用接口(implements IHandleEvent<>),如下所示:

if (this.GetType().GetInterfaces().Any(x =>
    x.IsGenericType && x.GenericTypeDefinition() == typeof(IHandleEvent<>)))
{
    ... // Some log-text
    return;
}
Run Code Online (Sandbox Code Playgroud)

但是,我不知道如何检查对象是否实现了SPECIFIC通用接口(implements IHandleEvent<Event1>).那么,如何在if中检查?

c# generics implementation types interface

0
推荐指数
1
解决办法
1060
查看次数

扩展现有接口设计

我有几个实现一些接口的类,而不是修改它,我想用另一个接口扩展它.

当我在考虑设计扩展接口时,我不确定如何包含对原始接口的引用的最佳实践是什么?

Id引用或将其作为扩展接口的属性包含在内?

我在下面创建了一个示例来说明需要:

public interface IMovie
{
    string UniqueId { get; set; }

    string Name { get; set; }

    string Ratnig { get; set; }
}

//Option #1
public interface IMovieWithSubTitles
{
    string UniqueId { get; set; }

    string SubTitles { get; set; }
}

//Option #2
public interface IMovieWithSubTitles
{
    IMovie Movie { get; set; }

    string SubTitles { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

.net c# interface

0
推荐指数
1
解决办法
42
查看次数