标签: interface-implementation

没有抽象类的实现接口?

我正在写一个库,我想要一个界面

public interface ISkeleton
{
    IEnumerable<IBone> Bones { get; }
    void Attach(IBone bone);
    void Detach(IBone bone);
}
Run Code Online (Sandbox Code Playgroud)

对于每个ISkeleton,Attach()和Detach()实现实际上应该是相同的.因此,它基本上可以是:

public abstract class Skeleton
{
    public IEnumerable<IBone> Bones { get { return _mBones; } }

    public List<IBone> _mBones = new List<IBone>();

    public void Attach(IBone bone)
    {
         bone.Transformation.ToLocal(this);
         _mBones.add();
    }

    public void Detach(IBone bone)
    {
        bone.Transformation.ToWorld(this);
         _mBones.Remove(bone);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是C#不允许多重继承.因此,在各种问题中,用户每次想要实现Skeleton时都必须记住从Skeleton继承.

我可以使用扩展方法

public static class Skeleton
{   
    public static void Attach(this ISkeleton skeleton, IBone bone)
    {
         bone.Transformation.ToLocal(skeleton);
         skeleton.Bones.add(bone);
    }

    public static void Detach(this ISkeleton …
Run Code Online (Sandbox Code Playgroud)

.net c# abstract-class interface-implementation

4
推荐指数
1
解决办法
295
查看次数

是否应该有可能方法在实现中缺少它们的参数?

我在Delphi XE2(RAD Studio)中遇到了一些常见的IDE错误,但问题本身并不是我关注的问题.这是其中一个错误导致我偶然发现其他错误的结果.

不知何故,自动完成决定销毁一些表单的方法,所以过去是......

procedure TForm1.Button1Click(Sender: TObject);
Run Code Online (Sandbox Code Playgroud)

在实施中变得像......

procedure TForm1.Buproced(Sendure :);
Run Code Online (Sandbox Code Playgroud)

(不确切,但在某种程度上是这样的)

所以,我不得不手动修复这些方法.但是,我不小心修了其中一个......

procedure TForm1.Button1Click;
Run Code Online (Sandbox Code Playgroud)

虽然应该是...

procedure TForm1.Button1Click(Sender: TObject);
Run Code Online (Sandbox Code Playgroud)

但它仍然编译并运行良好.

要测试,启动一个新的VCL Forms Application并只删除一个TButton控件,OnClick为其创建一个事件处理程序,并将其过程更改为...

procedure TForm1.Button1Click;
var
  B: TButton;
begin
  B:= TButton(Sender);
  B.Caption:= 'Something';
end;
Run Code Online (Sandbox Code Playgroud)

这应该是可能的吗?或者它可能是IDE和/或编译器错误?

delphi parameters interface-implementation delphi-xe2

4
推荐指数
1
解决办法
182
查看次数

实现"继承"(实现)通用接口的接口?

interface ITurtle
{        
    void Fight();
    void EatPizza();
}

interface ILeonardo : ITurtle
{
    void UseKatana();
}

interface IRaphael : ITurtle
{
    void UseSai();
}

interface IDonatello : ITurtle
{
    void UseBo();
}

interface IMichelangelo : ITurtle
{
    void UseNunchuku();
}
Run Code Online (Sandbox Code Playgroud)

如果我想创建一个可以完成所有4个的大乌龟怎么办?我想编码:

class GrandTurtle : IMichelangelo, IDonatello, IRaphael, ILeonardo
{
    // Implementation not shown for brevity.
}
Run Code Online (Sandbox Code Playgroud)

这是否可能,因为现在,似乎我必须实施Fight(),EatPizza()每次4次.但我认为这两个常用功能将解决,只需要实施一次,对吧?

我可以创建4个中间接口而不继承ITurtle,然后GrandTurtle实现ITurtle.这解决了接口继承问题,但现在它在语义上看起来是错误的,因为它ITurtle看起来像第5个兄弟,它不是.另外,我希望能够创建特定于龟的类,例如class BostonLeonardo : ILeonardo.

我从许多地方读过,似乎是一场无休止的争论 - 有人说"接口内的继承"很精细,那些说不是 - 我要么不理解他们的解释,要么他们只是说这是不好的做法而不解释为什么.

c# inheritance interface interface-implementation

4
推荐指数
1
解决办法
406
查看次数

从加载的程序集中查找实现接口的对象 - 如何比较类型?

我有一个类将加载目录中的所有程序集,然后获取所有类型,看看它们是否实现了一个接口.我无法进行类型比较.在调试器中,如果总是未通过比较,我会看到我的类型已加载(我感兴趣的那个).如果我在本地使用相同的比较代码,则没有问题我得到了预期的结果.我可以在类型接口上进行sting比较,但我更愿意知道我做错了什么.

测试:

    // Fails
    [Fact]
    public void FindISerialPortTest()
    {
        var path = Directory.GetCurrentDirectory();
        var results = FindImplementers.GetInterfaceImplementor<ISerialPort>(path);
        results.Length.Should().Be(1);
        results[0].Should().BeAssignableTo<SerialPortWrapper>();
    }

    //Passes
    [Fact]
    public void DoesTypeImplementInterfaceTest()
    {
        var myType = typeof(SerialPortWrapper);
        var myInterface = typeof(ISerialPort);
        FindImplementers.DoesTypeImplementInterface(myType, myInterface).Should().Be(true);

    }
Run Code Online (Sandbox Code Playgroud)

班级:

    public class FindImplementers
{

    public static T[] GetInterfaceImplementor<T>(string directory)
    {
        if (String.IsNullOrEmpty(directory)) { return null; } //sanity check

        DirectoryInfo info = new DirectoryInfo(directory);
        if (!info.Exists) { return null; } //make sure directory exists

        var implementors = new List<T>();

        foreach (FileInfo file in …
Run Code Online (Sandbox Code Playgroud)

c# reflection types interface interface-implementation

4
推荐指数
1
解决办法
2442
查看次数

在事务数据库中撤消

我不知道如何使用事务数据库实现用户友好界面的撤销属性.

一方面,建议用户拥有,因为它是说多(无限)撤销的可能性在这里的答案.可能有助于此问题的模式是MementoCommand.

但是,使用包含触发器,不断增长的序列号和不可逆转的过程的复杂数据库,很难想象撤消操作如何在不同于事务边界的点处起作用.换句话说,撤消到最后一次提交的事务只是回滚时,但是如何回到不同的时刻呢?

更新(基于到目前为止的答案):我不一定希望撤消在修改已经提交时有效,我将专注于具有开放事务的正在运行的应用程序.每当用户点击保存时,它意味着提交,但在保存之前 - 在同一事务期间 - 撤消应该有效.我知道使用数据库作为持久层只是一个实现细节,用户不应该为此烦恼.但是,如果我们认为"在数据库和GUI中撤消的想法是根本不同的东西",并且我们不对数据库使用撤销,那么无限撤消只是一个流行语.我知道"回滚是......不是用户撤消".

那么如何在同一个事务中给出"由于任何更改而产生的级联效果",实现客户端级撤销?

undo interface-implementation transactional-database

3
推荐指数
1
解决办法
2578
查看次数

C#继承了实现接口的protected方法

我在C#中有这个类/接口定义

public class FooBase {
    ...
    protected bool Bar() { ... }
    ...
}

public interface IBar {
    bool Bar();
}
Run Code Online (Sandbox Code Playgroud)

现在我想创建一个派生自FooBase实现IBar的类Foo1:

public class Foo1 : FooBase, IBar {
}
Run Code Online (Sandbox Code Playgroud)

是否存在一些类声明魔法,编译器将继承的受保护方法作为接口的可公开访问的实现?

当然是一种Foo1方法

bool IBar.Bar()
{
    return base.Bar();
}
Run Code Online (Sandbox Code Playgroud)

作品.我只是好奇是否有捷径;)

省略此方法会导致编译器错误:Foo1未实现接口成员IBar.Bar().FooBase.Bar()是静态的,不是公共的,或者返回类型错误.

说明:我将代码继承(类层次结构)和功能实现(接口)分开.因此,对于实现相同接口的类,访问共享(继承)代码非常方便.

c# inheritance class interface-implementation

3
推荐指数
1
解决办法
3537
查看次数

类实现两个定义相同方法的接口

//inteface i11 
 public interface i11
    {
        void m11();
    }
//interface i22
    public interface i22
    {
         void m11();
    }

//class ab implements interfaces i11 and i22
    public class ab : i11, i22
    {
        public void m11()
        {
            Console.WriteLine("class");
        }
    }
Run Code Online (Sandbox Code Playgroud)

现在在Main()方法中我们创建了一个类ab的对象,接口方法将被调用,请解释所有.

c# oop interface-implementation

3
推荐指数
1
解决办法
5047
查看次数

接口实现的"Go To Definition"等效快捷方式?

可能重复:
如何快速找到接口方法的实现?
转到具体类型的定义

在您希望快速跳转到函数实现之前,编码到接口很有用.

在好日子里,F12(转到定义)会直接带你到那里,但是现在它只是带你到接口定义,而不是实现.

我感谢可能有多个实现,但有一种比手动查找文件更快的方法,或者Ctrl-Shift-F搜索所有引用,即使它也包括每个调用."查找所有引用"给出了相同的结果.

只是想知道是否有人有一个方便的方法/宏?

c# intellisense visual-studio interface-implementation

3
推荐指数
1
解决办法
3644
查看次数

在C#接口实现中覆盖equals

我有一个实现接口的类,例如:

interface IInterface
{
   string PropA { get; }
   string PropB { get; }
}

class AClass : IInterface
{
   string PropA { get; protected set; }
   string PropB { get; protected set; }
}
Run Code Online (Sandbox Code Playgroud)

平等基于PropA和PropB确定.覆盖AClass的Equals方法时,我是否应该尝试将obj强制转换为AClass,如下所示:

public override bool Equals(object obj)
{
   AClass other = obj as AClass;
   return other != null 
       && AClass.PropA == other.PropA 
       && AClass.PropB == PropB;
}
Run Code Online (Sandbox Code Playgroud)

或者我应该尝试将obj强制转换为IInterface,如下所示:

public override bool Equals(object obj)
{
   IInterface other = obj as IInterface;
   return other != null 
       && AClass.PropA …
Run Code Online (Sandbox Code Playgroud)

c# equals interface-implementation

3
推荐指数
1
解决办法
3224
查看次数

为什么接口方法在实现接口的类中不能是静态的?

让我们假设我有这样的代码:

public interface JustAnInterface {
    void doSomething();
}

public class JustAnInterfaceImplementation implements JustAnInterface {

    @Override
    public static void doSomething() {

    }
}
Run Code Online (Sandbox Code Playgroud)

为什么static doSomething()方法显示错误" 方法不覆盖其超类方法 "?

java inheritance static interface-implementation

3
推荐指数
1
解决办法
775
查看次数