c#布尔覆盖

本文关键字:覆盖 布尔 | 更新日期: 2023-09-27 18:08:25

我试图在重写时使用布尔值。除了布尔值

public Bike(String colour, int gears, Boolean bell) : base (colour)

Bike B1 = new Bike("red ", 5, No);
        //B1.Name = "Red";
        Console.WriteLine("" + B1.ToString());
        //Console.WriteLine("Name: " + B1.Name);

我想创建的句子是红色的自行车有5档,没有铃

"no"位是布尔值那么如何使用布尔值来使用单词no呢因为当我在上面的程序中使用它时"Bike B1 = new Bike("red ", 5, no);"我得到一个错误说"名称'No'不存在于当前上下文中"

c#布尔覆盖

No在c#中不是关键字。bool类型使用truefalse

我认为你的问题是你不理解数据类型。
请阅读布尔bool (c#参考)。
这表示:"bool关键字是System.Boolean的别名。它用于声明变量来存储布尔值,truefalse。"
如果你需要一个值为null的布尔变量,则使用bool?。你可以试试这样做

public Bike(String colour, int gears, Boolean bell)
                : base (colour){
 ////.......
public override ToString(){
    string yesNo=(bell==true)? "with one":"and no";
    return string.Format("The {0} bike has {1} {2} bell", colour, gears, yesNo);
}
}