繁体   English   中英

将Java构造函数从Abstract类和super关键字转换为C#时出错?

[英]Error when converting Java Constructors from Abstract classes and super keyword to C#?

我有两个要转换为C#的Java类。 一个是名为RemoteButton的抽象类,另一个是从其派生的名为TVRemoteMute的类。 我能够转换抽象RemoteButton类中的大多数成员。 一个成员是在TVRemoteMute中实现的抽象buttonNinePressed(),另一个是在基类中实现的虚拟成员buttonFivePressed()。 我的问题是TVRemoteMute类的构造函数。 它突出显示了两个单词,即构造函数名称和方法内部的单词super。 构造函数名称错误为:“没有给出与'RemoteButton.RemoteButton(EntertainmentDevice)'所需的形式参数'newDevice'相对应的参数。“ super”关键字错误表明该名称在当前上下文中不存在。我将如何从Java到C#来实现此构造函数,以便我的类可以处理该构造函数?

public abstract class RemoteButton
{
    private EntertainmentDevice theDevice;

    public RemoteButton(EntertainmentDevice newDevice)
    {
        theDevice = newDevice;
    }

    public virtual void buttonFivePressed()
    {
        theDevice.buttonFivePressed();
    }

    public abstract void buttonNinePressed();

 }

public class TVRemoteMute : RemoteButton
{
    public TVRemoteMute(EntertainmentDevice newDevice)
    {
        super(newDevice);
    }

    public override void buttonNinePressed()
    {

        Console.WriteLine("TV was Muted");

    }
}

在C#中不使用关键字super 调用基类的构造函数不同于Java。

TVRemoteMute的构造函数更改为:

public TVRemoteMute(EntertainmentDevice newDevice) : base(newDevice)
{

}

实际上,如果您不对构造函数执行任何其他操作,则我更喜欢这样做,但这并不重要:

public TVRemoteMute(EntertainmentDevice newDevice) : base(newDevice) { }

编译后,另一个错误应会自行修复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM