方法覆盖C#中的错误 [英] Method Overriding Error in C#

查看:69
本文介绍了方法覆盖C#中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi There。



我正在学习C#编程。当我练习方法覆盖概念时,我得到以下错误。



请任何机构都可以帮我解决这个问题,我将不胜感激。



我的代码:



使用System;

using System.Collections.Generic;

使用System.Linq;

使用System.Text;

使用System.Threading.Tasks;





命名空间PolyMorphism

{

public class PolyMorphism1

{

public int getPolyTotalCost(int intQty)

{

return intQty;

}

public virtual int getPolyTotalCost(int intQty,int intProduct)

{

return intQty * intProduct;

}







}

public class PolyMorphism2:PolyMorphism1

{

public override int  getPolyTotalCost (int
intQty,int intProduct,int intPerProDiscount)//我收到此行的错误...."找不到合适的方法来覆盖"

{

return(getPolyTotalCost(intQty,intProduct) - intPerProDiscount);

}

}





}





使用System;

using System.Collections.Generic;

使用System.Linq;

使用System.Text;

使用System.Threading.Tasks;

使用PolyMorphism;





命名空间ConsoleApplication1

{

class Program

{

static void Main(string [] args)

{

PolyMorphism1 Poly1 = new PolyMorphism1(); 

PolyMorphism2 Poly2 = new PolyMorphism2();

Console.WriteLine("输入产品价格:"); 

int a = Convert.ToInt16(Console.ReadLine());

Console.WriteLine("输入产品数量:");

int b = Convert.ToInt16(Console.ReadLine());

Console.WriteLine(" Total is:" + Poly1.getPolyTotalCost(a,b));

Console.WriteLine(" Enter Product Discount:");

int c = Convert.ToInt16(Console.ReadLine());

Console.WriteLine("折扣后的总和);

Console.WriteLine(Poly2.getPolyTotalCost(a,b,c)); 

Console.ReadLine();

}

}

}

解决方案





命名空间PolyMorphism

{

public class PolyMorphism1

{

public int getPolyTotalCost(int intQty)

{

return intQty;

}

public virtual int getPolyTotalCost(int intQty,int intProduct)

{

return intQty * intProduct;

}







}

public class PolyMorphism2:PolyMorphism1

{

public override int  getPolyTotalCost (int
intQty,int intProduct,int intPerProDiscount)//我收到此行的错误...."找不到合适的方法来覆盖"






覆盖(C#参考)

https ://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/override



" override方法提供从基类继承的

成员的新实现。由覆盖覆盖的方法

声明被称为被重写的基本方法。被覆盖的基础 

方法必须具有相同的签名作为覆盖方法。"



- Wayne


Hi There.

i am learning C# programing. when i was practicing method overriding concept i got below error.

please any body can help me to address this issue i would be grateful.

My Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace PolyMorphism
{
public class PolyMorphism1
{
public int getPolyTotalCost(int intQty)
{
return intQty;
}
public virtual int getPolyTotalCost(int intQty , int intProduct)
{
return intQty * intProduct;
}



}
public class PolyMorphism2 : PolyMorphism1
{
public override int getPolyTotalCost(int intQty, int intProduct, int intPerProDiscount) // I am getting error this line...." No suitable method found to override "
{
return (getPolyTotalCost(intQty, intProduct) - intPerProDiscount);
}
}


}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PolyMorphism;


namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
PolyMorphism1 Poly1 = new PolyMorphism1(); 
PolyMorphism2 Poly2 = new PolyMorphism2();
Console.WriteLine("Enter Product Price:"); 
int a = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter Product Quantity:");
int b = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Total is:" + Poly1.getPolyTotalCost(a, b));
Console.WriteLine("Enter Product Discount:");
int c = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("The total after discount is");
Console.WriteLine(Poly2.getPolyTotalCost(a, b, c)); 
Console.ReadLine();
}
}
}

解决方案



namespace PolyMorphism
{
public class PolyMorphism1
{
public int getPolyTotalCost(int intQty)
{
return intQty;
}
public virtual int getPolyTotalCost(int intQty , int intProduct)
{
return intQty * intProduct;
}



}
public class PolyMorphism2 : PolyMorphism1
{
public override int getPolyTotalCost(int intQty, int intProduct, int intPerProDiscount) // I am getting error this line...." No suitable method found to override "


override (C# Reference)
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/override

"An override method provides a new implementation of a member that is 
inherited from a base class. The method that is overridden by an override 
declaration is known as the overridden base method. The overridden base 
method must have the same signature as the override method."

- Wayne


这篇关于方法覆盖C#中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆