使用可以属于不同类的参数调用函数

发布于 2025-01-09 15:00:39 字数 1394 浏览 3 评论 0原文

我有两个在领域略有不同的课程。是否可以实现一个可以接受第一类和第二类对象作为参数的函数。我尝试使用接口来实现它,但是在设计类时,它给出了错误 - “Class1Top”未实现接口成员“interface Top.Property2”。 “Class1 Top.Property2”无法实现“interfaceTop.Property2”,因为它没有相应的返回类型“interfaceMiddle”。

public interface interfaceTop {
    int Property1 { get; set; }
    interfaceMiddle Property2 { get; set; }
}
public interface interfaceMiddle {
    interfaceBottom Property1 { get; set; }
}
public interface interfaceBottom {
    int Property1 { get; set; }
}

public class Class1Top :interfaceTop {
   public int Property1 { get; set; }
   public Class1Middle Property2 { get; set; }
}
public class Class1Middle : interfaceMiddle {
    public Class1Bottom Property1 { get; set; }
}
public class Class1Bottom : interfaceBottom {
    public int Property1 { get; set; }
}

public class Class2Top :interfaceTop {
    public int Property1 { get; set; }
    public Class2Middle Property2 { get; set; }
    public int Property3 { get; set; }
}
public class Class2Middle : interfaceMiddle {
    public Class2Bottom Property1 { get; set; }
}
public class Class2Bottom: interfaceBottom {
    public int Property1 { get; set; }
    public int Property2 { get; set; }
}
// I need just such a check
public class result {
   public bool Result(interfaceTop obj) {
        return obj.Property2.Property1.Property1 == 1;
   }
}

如何实现这种传输的可能性或检查的可能性 结果 public bool(顶层接口对象)

I have two classes that are slightly different in fields. Is it possible to implement a function that could accept objects of both one and the second class as a parameter. I tried to implement it using interfaces, but when designing classes, it gives an error -
"Class1Top" does not implement the interface member "interface Top.Property2". "Class1 Top.Property2"cannot implement "interfaceTop.Property2", because it does not have the corresponding return type "interfaceMiddle".

public interface interfaceTop {
    int Property1 { get; set; }
    interfaceMiddle Property2 { get; set; }
}
public interface interfaceMiddle {
    interfaceBottom Property1 { get; set; }
}
public interface interfaceBottom {
    int Property1 { get; set; }
}

public class Class1Top :interfaceTop {
   public int Property1 { get; set; }
   public Class1Middle Property2 { get; set; }
}
public class Class1Middle : interfaceMiddle {
    public Class1Bottom Property1 { get; set; }
}
public class Class1Bottom : interfaceBottom {
    public int Property1 { get; set; }
}

public class Class2Top :interfaceTop {
    public int Property1 { get; set; }
    public Class2Middle Property2 { get; set; }
    public int Property3 { get; set; }
}
public class Class2Middle : interfaceMiddle {
    public Class2Bottom Property1 { get; set; }
}
public class Class2Bottom: interfaceBottom {
    public int Property1 { get; set; }
    public int Property2 { get; set; }
}
// I need just such a check
public class result {
   public bool Result(interfaceTop obj) {
        return obj.Property2.Property1.Property1 == 1;
   }
}

How to implement such a possibility of transmission or the possibility of checking as a
result of
public bool (top interface object)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奶茶白久 2025-01-16 15:00:39

您正在寻找的是泛型

您可以做一个泛型类,可以设置泛型参数实现任意接口。查看 这个

所以你的解决方案例如可以是这样的:

public class GenericClass<T> where T: interfaceBottom,interfaceMiddle,InterfaceTop {
    public bool (T object){
//here check the type ob the interface

    }
}

What you are looking for are Generics:

You can do a generic class , and you can set that the generic parameter implements any interface. Check out this

So your solution can be for example something like this:

public class GenericClass<T> where T: interfaceBottom,interfaceMiddle,InterfaceTop {
    public bool (T object){
//here check the type ob the interface

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文