当“没有找到合适的方法来覆盖”时,会出现什么问题。错误被抛回? [英] What is wrong when a "no suitable method found to override" error is thrown back?

查看:61
本文介绍了当“没有找到合适的方法来覆盖”时,会出现什么问题。错误被抛回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码出现问题。我创建了一个父类,并用子类重写它,但它说没有合适的方法来覆盖,我不明白为什么会发生这种情况。我在互联网上看了但无济于事。



我父类的代码是:



I am having a problem with my code. I created a parent class and overridden it with a child class but it says there is not suitable method to override and I do not understand why this is happening. I looked on the internet but to no avail.

My code for the parent class is:

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

namespace Wood_Dylan
{
   abstract class StudentClass
    {
        //Create private backing variables for properties 

        
        //creating properties
        public decimal TotalTuition { get; set; }
        public int Hours { get; set; }
        public decimal BaseRate { get; set; }
        public decimal GraduateFee { get; set; }

        //Method to calculate graduate tuition
        public virtual decimal CalcTuition(int intCreditHours, decimal decBaseRate, decimal decGraduateFee, int intExcessCredits, decimal decExcessCreditsPercentage)
        {
            //declare variables
            Hours = intCreditHours;
            BaseRate = decBaseRate;
            GraduateFee = decGraduateFee;

            TotalTuition = (Hours * BaseRate) + GraduateFee;

            return TotalTuition; 
        }
        


    }
}





子类的代码是:





The code for the child class is:

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


namespace Wood_Dylan
{
    class UndergraduateClass : StudentClass
    {

        //Create properties that are not in parent class
        public int ExcessCredits { get; set; }
        public decimal ExcessCreditsPercentage { get; set; }
        public decimal RegularTuition { get; set; }
        public decimal ExcessCreditTuition { get; set; }

       
        //Create method to calculate undergraduate tuition
        
        public override decimal CalcTuition(int intExcessCredits, decimal decExcessCreditsPercentage)
        {

           //populate properties not in Parent (student) class
            ExcessCredits = intExcessCredits;
            ExcessCreditsPercentage = decExcessCreditsPercentage;

            //calculate regular tuition
            RegularTuition = Hours * BaseRate;

            //Calc excess credit tuition
            ExcessCreditTuition = ExcessCredits * ExcessCreditsPercentage;

            //Calulate Total Tuition
            TotalTuition = RegularTuition + ExcessCreditTuition;

            return TotalTuition; 

        }

    }
}

推荐答案

参数在派生类与抽象类中的方法不同。



由于基类中的定义是

The parameters in the derived class are different from the method in the abstract class.

Since the definition in the base class is
public virtual decimal CalcTuition(int intCreditHours, decimal decBaseRate, decimal decGraduateFee, int intExcessCredits, decimal decExcessCreditsPercentage)
{



而不是以下


Instead of the following

public override decimal CalcTuition(int intExcessCredits, decimal decExcessCreditsPercentage)
{



尝试


try

public override decimal CalcTuition(int intCreditHours, decimal decBaseRate, decimal decGraduateFee, int intExcessCredits, decimal decExcessCreditsPercentage) 
{


要覆盖方法,您必须具有相同的方法签名。

在您的基类中,您有方法CalcTuition具有5个输入参数。

To override a method you must have method signature same.
In your base class you have method "CalcTuition" having 5 input parameters.
public virtual decimal CalcTuition(int intCreditHours, decimal decBaseRate, decimal decGraduateFee, int intExcessCredits, decimal decExcessCreditsPercentage)



在你的子类中你有方法CalcTuition有2输入参数ers。


While in your child class you have method "CalcTuition" having 2 input parameters.

public override decimal CalcTuition(int intExcessCredits, decimal decExcessCreditsPercentage)





要覆盖你应该有相同的方法签名即。返回类型和参数的数量/类型将相同。



To override you should have method signature same ie. return type and number/type of parameter will be same.


这篇关于当“没有找到合适的方法来覆盖”时,会出现什么问题。错误被抛回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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