【问题标题】:Interface Implementation C# [closed]接口实现 C# [关闭]
【发布时间】:2013-06-24 19:08:55
【问题描述】:

我应该编辑下面的类以实现接口,但我不知道该怎么做。不需要编辑界面吗?我怎样才能提供那个类来实现?不知道怎么填写构造函数和convertToLower()

public interface DenemeInterface
{
    string convertToLower();
}

public class Deneme : DenemeInterface
{
    public Deneme(string s)
    {
    }

    public string convertToLower()
    {
        return "";
    }
}

【问题讨论】:

  • 你有什么问题?
  • I dont know how to fill constructor and converttoLower()。你是什​​么意思?该接口定义了一个返回字符串的无参数方法。实现该接口的类必须有同名同返回类型的无参方法。
  • 为您提供了实现的骨架,您的任务是执行实际的实现。如果您需要有关作业的帮助,您需要展示它。此外,如果您需要作业方面的帮助,您应该在问题中明确说明,以便我们可以帮助您了解您需要了解的内容。
  • 我应该返回什么?我只想实现那个界面
  • @MerveKaya,好吧,看看方法的名称,我假设(只是一个假设)您需要返回一个所有字符都低的字符串。同样,通过看到 Deneme 类在其构造函数中有一个字符串参数,我认为您需要一个变量/属性来将此值存储在构造函数中,然后在 convertToLower() 中调用 return myVariable.ToLower(),其中 myVariable 是变量/property 存储构造函数中提供的值的位置。

标签: c# interface constructor implementation


【解决方案1】:

我想我终于明白你要做什么了

试试这个:

public interface DenemeInterface
{
    string convertToLower();
}
public class Deneme : DenemeInterface
{
    string a;
    public Deneme(string s)
    {
        this.a = s;
    }

    public string convertToLower()
    {
        return a.ToLower();
    }
}

你在cmetsi wrote that string a=s; inside Deneme(string s) constructor but i cant use that variable which is name "a" inside convertToLower(). How can i store the value provided in the constructor问我。

需要在构造函数外声明,否则只能在构造函数内使用。

【讨论】:

  • 他们工作非常感谢
【解决方案2】:

您已经在实现该接口。你在这里做的:

public class Deneme : DenemeInterface

它基本上是“类 Deneme 实现 DenemeInterface”。 当然,接口本身需要一个名为 convertToLower() 的方法,该方法返回一个字符串。但你已经有了。你已准备好出发。需要注意的是,接口通常以“I”开头。考虑将其更改为 IDenemeInterface。

-编辑 啊哈。我认为康拉德是对的。在这种情况下,你会想要这样的东西:

public interface IDenemeInterface
{
    string convertToLower();
}

public class Deneme : IDenemeInterface
{
    private string s;

    public Deneme(string s)
    {
        this.s = s;
    }

    public string convertToLower()
    {
        return this.s.ToLower();
    }
}

【讨论】:

    猜你喜欢
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    相关资源
    最近更新 更多