C# 3.0 这是使用界面的正确方式吗?

C# 3.0 这是使用界面的正确方式吗?,c#-3.0,C# 3.0,接口如何知道调用哪个类方法? 这是正确的代码吗?还是不 namespace IntExample { interface Iinterface { void add(); void sub(); } public partial class Form1 : Form,Iinterface { public Form1() { InitializeComponent();

接口如何知道调用哪个类方法? 这是正确的代码吗?还是不

namespace IntExample
{
    interface Iinterface
    {
       void add();
       void sub();
    }
    public partial class Form1 : Form,Iinterface
    {
        public Form1()
        {
            InitializeComponent();
        }


        public void add()
        {

            int a, b, c;
            a = Convert.ToInt32(txtnum1.Text);
            b = Convert.ToInt32(txtnum2.Text);
            c = a + b;
            txtresult.Text = c.ToString();



        }
        public void sub()
        {

            int a, b, c;
            a = Convert.ToInt32(txtnum1.Text);
            b = Convert.ToInt32(txtnum2.Text);
            c = a - b;
            txtresult.Text = c.ToString();

        }


        private void btnadd_Click(object sender, EventArgs e)
        {
            add();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sub();
        }



        class cl2 : Form1,Iinterface
        {

            public void add()
            {

                int a, b, c;
                a = Convert.ToInt32(txtnum1.Text);
                b = Convert.ToInt32(txtnum2.Text);
                c = a + b;
                txtresult.Text = c.ToString();



            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
接口不“知道”要调用哪个类方法。它只是定义了可用的方法

您发布的代码不会编译,因为
cl2
没有实现
sub
方法,但无论如何它几乎没有意义

我不知道你想做什么,所以我会举一个有效使用接口的例子

您可以有几个表单实现该接口,然后在主表单中,您可以根据索引或名称选择要显示哪些表单

因此,要存储所有可以使用通用列表的表单:

List<Iinterface> forms = new List<Iinterface>();
然后,您可以显示特定表单并从接口调用方法:

forms.Add(new Form1());
forms.Add(new Form2());
forms.Add(new Form3());
//...
//find by index:
forms[index].Show();
forms[index].add();

//find by name:
string name="form 2";
Iinterface form = forms.Find(f => f.Name == name);
if (form != null)
{
    form.Show();
    form.add();
}

你能告诉我如何用c#创建日志文件吗?这与这个问题完全无关,请问一个新问题。