c#问题中的接口

Aru*_*run 3 c# interface

我是c#开发的新手.我只是想研究界面功能.基于我阅读的有关接口的文章和注释,我尝试编写一个示例代码,以根据我从这些注释和文章中理解的内容实现接口.

但是当我调试项目时,我遇到了构建错误

"不一致的可访问性:参数类型'StartMachine'不如方法'SwitchBoard.switchPress(StartMachine)'".

这里有什么问题 ?.或者我是否以正确的方式实现了界面?或者我的界面概念是错误的吗?

请帮忙.提前致谢.

我在下面发布了我的代码.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        StartMachine s1 = new Machine1();
        SwitchBoard switch1 = new SwitchBoard();
        switch1.switchPress(s1);
    }
}

interface StartMachine
{
    void startMachine();
}

public class Machine1 : StartMachine
{
    public void startMachine()
    {
        HttpContext.Current.Response.Write("Machine 1 Started");
    }
}

public class Machine2 : StartMachine
{
    public void startMachine()
    {
        HttpContext.Current.Response.Write("Machine 2 Started");
    }
}

public class SwitchBoard
{
    public void switchPress(StartMachine switchNum)
    {
        switchNum.startMachine();
    }
}
Run Code Online (Sandbox Code Playgroud)

Jas*_*own 8

您需要public在界面前放置一个访问修饰符.Machine1/2类是public,但您的界面默认为internal.这比公众更容易接近.

将接口设置为公共访问修改器:

public interface StartMachine {...}
Run Code Online (Sandbox Code Playgroud)

或者,您可以将您的类更改为:

internal class Machine1: StartMachine {...}
internal class Machine2: StartMachine {...}
Run Code Online (Sandbox Code Playgroud)

有关访问修饰符的更多信息,请查看MSDN.

更正 ::

本Switchboard类和switchPress方法的public.但是,该switchPress方法试图访问(默认情况下)StartMachine.startMachine(ugh,ambigious命名)方法internal.您需要更改的StartMachine接口是公开的,或更改Switchboard/ switchPress来internal.