【问题标题】:error CS1061: Type `BikeCostDetails.Bike' does not contain a definition for `CalculateFinalCost'错误 CS1061:类型“BikeCostDetails.Bike”不包含“CalculateFinalCost”的定义
【发布时间】:2021-06-12 00:16:47
【问题描述】:

文件 - Bike.cs

namespace BikeCostDetails
{
public class Bike
{
    private string bikeName;
    private string bikeType;
    private double bikeCost;
    private double onRoadPrice;

    public string BikeName
    {
        get
        {
            return this.bikeName;
        }
        set
        {
            this.bikeName = value;
        }
    }
    public string BikeType
    {
        get
        {
            return this.bikeType;
        }
        set
        {
            this.bikeType = value;
        }
    }
    public double BikeCost
    {
        get
        {
            return this.bikeCost;
        }
        set
        {
            this.bikeCost = value;
        }
    }
    public double OnRoadPrice
    {
        get
        {
            return this.onRoadPrice;
        }
        set
        {
            this.onRoadPrice = value;
        }
    }

    public bool CheckForBikeType(string bikeType)
    {
        bool flag = false;
        if (bikeType.Equals("naked") || bikeType.Equals("doomed"))
        {
            flag = true;
        }
        return flag;
    }

}
}

文件 2 - Program.cs

using System;
using System.Threading.Tasks;

namespace BikeCostDetails
{
public class Program
{

    public static Bike CalculateFinalCost(Bike bike)
    {

        string type = bike.BikeType;
        double cost = bike.BikeCost;
        double finalCost;
        if (type.Equals("naked"))
        {
            finalCost = cost - (cost * 10 / 100);
        }
        else
        {
            finalCost = cost + (cost * 5 / 100);
        }

        bike.OnRoadPrice = finalCost;
        return bike;
    }


    public static void Main(string[] args)
    {
        Bike b = new Bike();

        Console.WriteLine("Enter the bike name");
        string name = Console.ReadLine();
        Console.WriteLine("Enter bike type");
        string type = Console.ReadLine();
        Console.WriteLine("Enter bike cost");
        double cost = Convert.ToDouble(Console.ReadLine());

        b.BikeName = name;
        b.BikeType = type;
        b.BikeCost = cost;

        if (b.CheckForBikeType(type) == true)
        {
            
            Bike bi = b.CalculateFinalCost(b);
            Console.WriteLine("Thank you for buying " + name + ". You need to pay the amount of Rs " + bi.OnRoadPrice);
        }
        else
        {
            Console.WriteLine("Invalid Bike Type");
        }
    }
}
}

我收到此错误:

错误 CS1061 'Bike' 不包含 'CalculateFinalCost' 的定义,并且找不到接受“Bike”类型的第一个参数的可访问扩展方法 'CalculateFinalCost'(您是否缺少 using 指令或程序集参考?)

有人可以帮我解决这个错误吗?我是 C# 新手。

【问题讨论】:

    标签: c# oop


    【解决方案1】:

    Bike bi = b.CalculateFinalCost(b); 应该只是 Bike bi = CalculateFinalCost(b);b. 在方法名称之前。错误就是它所说的:Bike 类型不包含CalculateFinalCost 方法。该方法在您的 Program 类中定义。

    【讨论】:

    • 我只是回答,因为我想表达一种不同的方式来做到这一点,你的回答更适合解决方案。
    • 不应该是 Program p = new Program(); b = p.CalculateFinalCost(b);我已经解决了。感谢您的帮助。
    【解决方案2】:

    改变

    public static Bike CalculateFinalCost(Bike bike)

    public static Bike CalculateFinalCost(this Bike bike) 
    

    这将允许你使用你的方法作为类的一部分。

    【讨论】:

      【解决方案3】:

      这是最终代码。我在发布问题的同一天解决了它。首先没有注意到错误。

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text.RegularExpressions;
      using System.Threading.Tasks;
      
      namespace BikeCostDetails
      {
          public class Bike
          {
              private string bikeName;
              private string bikeType;
              private double bikeCost;
              private double onRoadPrice;
              
              public string BikeName{
                get{
                  return this.bikeName;
                }
                set{
                  this.bikeName = value;
                }
              }
              public string BikeType{
                get{
                  return this.bikeType;
                }
                set{
                  this.bikeType = value;
                }
              }
              public double BikeCost{
                get{
                  return this.bikeCost;
                }
                set{
                  this.bikeCost = value;
                }
              }
              public double OnRoadPrice{
                get{
                  return this.onRoadPrice;
                }
                set{
                  this.onRoadPrice = value;
                }
              }
              
              public bool CheckForBikeType(string bikeType){
                bool flag = false;
                if(bikeType.Equals("naked") || bikeType.Equals("doomed")){
                  flag = true;
                }
                return flag;
              }
          }
              
          
        public class Program{
          
            public Bike CalculateFinalCost(Bike bike){
              
              string type = bike.BikeType;
              double cost = bike.BikeCost;
              double finalCost = 0;
      
               if(type.Equals("naked")){
                  finalCost = cost - (cost * 10/100);
               }
               else{
                  finalCost = cost + (cost * 5/100);
               }
              
               bike.OnRoadPrice = finalCost;
               return bike;
            }
          
            
            public static void Main(string[] args){
              Bike b = new Bike();
              Program p = new Program();
              Console.WriteLine("Enter the bike name");
              string name = Console.ReadLine();
              Console.WriteLine("Enter bike type");
              string type = Console.ReadLine();
              Console.WriteLine("Enter bike cost");
              double cost = Convert.ToDouble(Console.ReadLine());
              
              b.BikeName = name;
              b.BikeType = type;
              b.BikeCost = cost;
      
              if(b.CheckForBikeType(type) == true){
                b = p.CalculateFinalCost(b);
                Console.WriteLine("Thank you for buying "+ name+". You need to pay the amount of Rs "+b.OnRoadPrice);
              }
              else{
                Console.WriteLine("Invalid Bike Type");
              }
            }
          }
          
      }
      

      【讨论】:

      • 你不应该实例化Program。您的代码 (Main) 已经在 Program 类中执行。最初的方法实际上是正确的方法,我的答案中列出了更改。
      • 我试过了,但它显示错误。所以我们必须创建一个程序类的对象或者只是使用 Program.CalculateFinalCost(b) 调用它
      猜你喜欢
      • 1970-01-01
      • 2011-08-21
      • 2023-03-20
      • 2018-04-11
      • 1970-01-01
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多