C Sharp - 需要对象引用的接口

发布于 2024-11-09 06:33:19 字数 1813 浏览 0 评论 0原文

由于某种原因,当我调用此类中的命名空间时,我收到一条错误,指出 它需要一个对象引用。我认为使用 IMethodResponses 类型的引用变量将允许我访问其自己创建的方法,但是 我无法完成此任务,也不能简单地实现没有引用的接口并使用其方法...

有人可以帮我解决这个问题吗?我也会发布界面本身,以防出现问题。我可能应该指出,我这样做的时间并不长。

//类实现接口:

internal sealed class Name : INameCreation, IMethodResponses
{
    public ScratchCreate create = ScratchCreate.create;
    ListCreate select = new ListCreate();
    public IMethodResponses _responses = IMethodResponses; //<--Error

    public static void ChooseName()
    {
        int response;

        Console.WriteLine("Press \'1\' to select from a list of names, or \'2\' to create your own.");
        Console.WriteLine("If you wish to quit, you may do so by pressing \'0\'");
        response = int.Parse(Console.ReadLine());

        do
        {
            Console.WriteLine("Sorry, you didn't enter any of the correct responses...");
            Console.WriteLine("Press \'1\' to select from a list of names, or \'2\' to create your own.");
            Console.WriteLine("If you wish to quit, you may do so by pressing \'0\'");
            response = int.Parse(Console.ReadLine());
        }
        while (response != 0 || response != 1 || response != 2);

        _responses.IntegerResponse(response);

    }

    public void IntegerResponse(int response)
    {
        switch (response)
        {
            case 0:
                Console.WriteLine("Goodbye!");
                break;
            case 1:

                break;
            case 2:

                break;

        }
    }

//接口:

namespace PlayerCreation
 {
     public interface INameCreation
     { 

     }
     public interface IMethodResponses 
     {
        void IntegerResponse(int response);
        void StringResponse(string response);
     } 
 }

For some reason when I call a namespace within this class, I get an error which states that
it requires an object reference. I thought that using a reference variable of the IMethodResponses type would allow me to access a method within its own creation, but
I can't accomplish this, and nor can I simply just implement the interface without a reference and use its methods...

Can someone help me out with this? I'll post the Interface itself too in case their's something wrong. I should probably note that I haven't been doing this very long.

//Class Implementing Interface:

internal sealed class Name : INameCreation, IMethodResponses
{
    public ScratchCreate create = ScratchCreate.create;
    ListCreate select = new ListCreate();
    public IMethodResponses _responses = IMethodResponses; //<--Error

    public static void ChooseName()
    {
        int response;

        Console.WriteLine("Press \'1\' to select from a list of names, or \'2\' to create your own.");
        Console.WriteLine("If you wish to quit, you may do so by pressing \'0\'");
        response = int.Parse(Console.ReadLine());

        do
        {
            Console.WriteLine("Sorry, you didn't enter any of the correct responses...");
            Console.WriteLine("Press \'1\' to select from a list of names, or \'2\' to create your own.");
            Console.WriteLine("If you wish to quit, you may do so by pressing \'0\'");
            response = int.Parse(Console.ReadLine());
        }
        while (response != 0 || response != 1 || response != 2);

        _responses.IntegerResponse(response);

    }

    public void IntegerResponse(int response)
    {
        switch (response)
        {
            case 0:
                Console.WriteLine("Goodbye!");
                break;
            case 1:

                break;
            case 2:

                break;

        }
    }

//Interface:

namespace PlayerCreation
 {
     public interface INameCreation
     { 

     }
     public interface IMethodResponses 
     {
        void IntegerResponse(int response);
        void StringResponse(string response);
     } 
 }

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

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

发布评论

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

评论(2

椵侞 2024-11-16 06:33:19

您正在使用类型而不是类型的实例分配 IMethodResponses 类型的引用变量。

为了解决这个问题,您可以简单地执行以下操作:

public Name()
{
    _responses = (IMethodResponses)this;
}

编辑:我还注意到,您的代码还有两个其他问题:
1. _responses 是非静态的,您在静态类中访问此变量,这是行不通的。
2. StringResponses 尚未实现。

如果您想将 ChooseName 保持为静态并有权访问同一实例,那么您必须实现单例。

You are assigning a reference variable of type IMethodResponses with a type, and not an instance of a type.

In order to fix this, you can simply do the following:

public Name()
{
    _responses = (IMethodResponses)this;
}

Edit: Also I just noticed, there are two other problems with your code:
1. _responses is non-static and you are accessing this variable in a static class, which will not work.
2. StringResponses has not been implemented.

If you want to maintain ChooseName as static and have access to the same instance, then you'll have to implement a singleton.

从此见与不见 2024-11-16 06:33:19

如果将 IntegerResponse 方法设置为静态,则可以使用以下方式调用它:

Name.IntegerResponse(...);

对于:

public IMethodResponses _responses = IMethodResponses;

从静态方法中,您只能访问静态成员。
将类型设置为相同类型的变量是行不通的。
_responses 中预期包含的是其类型的任何类的实例,或者实现其接口的任何类 (IMethodResponses)。由于您无法创建接口的实例,因此它是后者。

public static IMethodResponses _responses = new Name();

这可以工作,但不是必需的,因为通过在方法定义之前使用 static 关键字,可以安全地将 IntegerResponse 设为静态。

有点尴尬的是 public static void ChooseName() 的使用。该方法是静态的,不返回任何内容。它只使用Console.Writeline。您会发现,一旦开始开发真正的应用程序,就需要重做。

If you make your IntegerResponse method static you can call it with:

Name.IntegerResponse(...);

For:

public IMethodResponses _responses = IMethodResponses;

From a static method you can acess only static members.
Setting an type into a variable of the same type won't work.
What is expected to fit into _responses is an instance of any class of its type or any class that implements its interface (IMethodResponses). Since you cannot create an instance of an interface than it is the latter.

public static IMethodResponses _responses = new Name();

This will work but is not needed, since IntegerResponse can safely be made static as is by using the static keyword before the method definition.

What is a bit awkward is the use of public static void ChooseName(). This method is static and returns nothing. It just uses Console.Writeline. You will find out that as soon as you start developing a real application this will need to be redone.

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