Mic*_*ton 0 c# java constructor
我有两个Java类,我正在尝试转换为C#.一个是名为RemoteButton的抽象类,另一个是从TVRemoteMute派生的.我能够转换抽象RemoteButton类中的大多数成员.一个成员是抽象的buttonNinePressed(),在TVRemoteMute中实现,另一个是在基类中实现的虚拟成员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");
}
}
Run Code Online (Sandbox Code Playgroud)
该关键字super未在C#中使用; 调用基类的构造函数与Java不同.
将构造函数更改TVRemoteMute为:
public TVRemoteMute(EntertainmentDevice newDevice) : base(newDevice)
{
}
Run Code Online (Sandbox Code Playgroud)
实际上,如果你在构造函数体中没有做任何其他事情,我更喜欢这个,但它确实没关系:
public TVRemoteMute(EntertainmentDevice newDevice) : base(newDevice) { }
Run Code Online (Sandbox Code Playgroud)
编译后,另一个错误应该自行修复.