【发布时间】: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# 新手。
【问题讨论】: