class SecondExp
{
public int firstMethod()
{
return(100);
}
public int SecondMethod()
{
return(400);
}
}
class DerivedClass:SecondExp
{
public override int secondMethod()
{
return(999);
}}
class Test
{
public static void Main()
{
DerivedClass dc = new DerivedClass();
System.Console.WriteLine(dc.firstMethod());
}
}提示错误原因:SecondExp.cs(14,25): error CS0115: “DerivedClass.secondMethod()” : 没有找到适合的方法来重写
=======
class A
{
public int f1()
{
return(100);
}
public int f2()
{
return(400);
}
}
class B:A
{
public override int f2()
{
return(999);
}}
class Test
{
public static void Main()
{
B dc = new B();
System.Console.WriteLine(dc.f1());
}
}
错误原因:
//SecondExp2.cs(14,25): error CS0506: “B.f2()” :
//无法重写继承成员“A.f2()”,因为它未标记为 virtual、abstract 或 override
//SecondExp2.cs(7,17): (与前一个错误相关的符号位置)
这两个程序有区别吗?
为什么 提示 不一样?
{
public int firstMethod()
{
return(100);
}
public int SecondMethod()
{
return(400);
}
}
class DerivedClass:SecondExp
{
public override int secondMethod()
{
return(999);
}}
class Test
{
public static void Main()
{
DerivedClass dc = new DerivedClass();
System.Console.WriteLine(dc.firstMethod());
}
}提示错误原因:SecondExp.cs(14,25): error CS0115: “DerivedClass.secondMethod()” : 没有找到适合的方法来重写
=======
class A
{
public int f1()
{
return(100);
}
public int f2()
{
return(400);
}
}
class B:A
{
public override int f2()
{
return(999);
}}
class Test
{
public static void Main()
{
B dc = new B();
System.Console.WriteLine(dc.f1());
}
}
错误原因:
//SecondExp2.cs(14,25): error CS0506: “B.f2()” :
//无法重写继承成员“A.f2()”,因为它未标记为 virtual、abstract 或 override
//SecondExp2.cs(7,17): (与前一个错误相关的符号位置)
这两个程序有区别吗?
为什么 提示 不一样?
{
public override int secondMethod()
{
return(999);
}}
函数名字应该和父类一样的,改成SecondMethod你就发现同样的错了。
改成
public override int SecondMethod()
{
public int f1()
{
return(100);
}
public virtual int f2() //这里进行修改,需要被重写就要设置成虚函数
{
return(400);
}
}class SecondExp
{
public int firstMethod()
{
return(100);
}
public int SecondMethod()//这里进行修改,需要被重写就要设置成虚函数
{
return(400);
}
}你在Main入口函数中执行的代码不一样