C#如何实现具体类不同的接口?

编程入门 行业动态 更新时间:2024-10-26 10:34:11
本文介绍了C#如何实现具体类不同的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

首先,抱歉vaque标题,但我不知道我的问题的好标题。

First, sorry for the vaque title, but I don't know a good title for my question.

在我的应用程序中,我使用标准。标准可以看作是对照。我有文本框,还有单选按钮和复选框。

In my application I use criteria. The criteria can be seen as controls. I have textboxes, but also radio buttons and check boxes.

我使用接口来实现这一点:

I use interfaces to achieve this:

ICriteria是所有条件类继承自的基接口。

ICriteria is the base interface where all criteria classes inherits from.

TextType继承自ICriteria。 RadioType也继承自ICriteria。但是,RadioType必须有选择。

TextType inherits from ICriteria. RadioType also inherits from ICriteria. But, RadioType must have choices.

两者,TextType和RadioType都有一些相同的属性。

Both, TextType and RadioType do have some properties which are the same.

我的问题是,我不知道我不知道如何实现这一点。我的意思是,我可以向ICriteria添加一些影响选择的方法和属性,但TextType也必须继承这些方法和属性。我不认为这很好,因为TextType没有选择。

My problem is, I don't know exactly how to implement this. I mean, I can add some methods and properties to ICriteria which affects choices, but then TextType has to inherit these methods and properties too. I don't think this is nice because TextType doesn't have choices.

实现这个的最佳方法是什么?

What is the best way to implement this?

推荐答案

为什么不使用接口层次结构

why not use a hierarchy of interfaces

public interface ICriteria { void SomeCriteriaMethod(); } public interface IChoices : ICriteria { void SomeChoicesMethod(); }

然后像这样使用

foreach (ICriteria criteria in criterias) { // something var choice = criteria as IChoices; if (choice != null) // do something else }

更多推荐

C#如何实现具体类不同的接口?

本文发布于:2023-11-12 09:43:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581150.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何实现   接口

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!