在自定义比较两个实例是否相等那一节,我想写一个子类用来判断两个实例是否相等,测试时:
情况一:父类不含构造函数,子类可正常运行
情况二:父类含一个构造函数,子类就会报错
想问一下这个问题怎么解决呢?
父类:
public class SangonomiyaKokomi:HydroCharacter
{
public float level;
public SangonomiyaKokomi(float level)
{
this.name = "SangonomiyaKokomi";
this.id = 0222;
this.level = level;
}
子类:
public class ReloadOperator : SangonomiyaKokomi
{
public float level;
public ReloadOperator(float level):base(level)
{
base.level = level;
}
public static bool operator==(SangonomiyaKokomi s1, SangonomiyaKokomi s2)
{
if (s1.level == s2.level)
{
return true;
}
return false;
}
public static bool operator!=(SangonomiyaKokomi s1, SangonomiyaKokomi s2)
{
bool result = s1 == s2;
return !result;
}
}
这段代码在==和!=位置报错:CS0563,VS版本2022.net6.0,应该如何处理呢?
同学你好,报错内容是什么,可以参考一下:
https://www.shuzhiduo.com/A/kvJ3NPBwzg/
https://www.cnblogs.com/larissa-0464/p/13585966.html