1

我让我的程序运行,但我似乎无法输出我拥有的存储信息。当我运行程序时,所有的行都是空白的。代码还有更多内容,但我认为这是程序用于输出信息的主要代码。

    class Display
{
    public static void Main(string[] args)
    {
        LandlinePhone yourHomePhone = new LandlinePhone("VTech", "V-4321", "219-999-2345", true);
        Console.WriteLine("Manufacturer: {0}", yourHomePhone.Manufacturer);
        Console.WriteLine("Model: {0}", yourHomePhone.Model);
        Console.WriteLine("Phone Number: {0}", yourHomePhone.PhoneNumber);
        Console.WriteLine("Cordless: {0}", (yourHomePhone.HasCord ? "Yes" : "No"));
}

{
class LandlinePhone : Telephone
{
    private bool hasCord;

    public LandlinePhone()
    {
    }
    public LandlinePhone(string manufacturer, string model, string phoneNumber, bool hasCord)
            : base(manufacturer, model, phoneNumber)
    {
        hasCord = HasCord;
    }

    public bool HasCord
    {
        get
        {
            if (hasCord == true)
            {
                return true;

            }
            else
            {
                return false;
            }

        }
        set
        {
            hasCord = value;
        }
    }

    new public void display()
    {
        base.display();
        Console.WriteLine(HasCord ? "Cordless: Yes" : "Cordless: No");
    }
}


class Telephone
{
    public bool isConnected = true;
    public string lastNumberDialed;
    private string manufacturer;
    private string model;
    private string phoneNumber;



    public Telephone()
    {
    }

    public Telephone(string manufacturer, string model, string phoneNumber)
    {
    }

    public string Manufacturer
    {
        get
        {
            return manufacturer;
        }
        set
        {
            manufacturer = value;
        }
    }

    public string Model
    {
        get
        {
            return model;
        }
        set
        {
            model = value;
        }

    }
    public string PhoneNumber
    {
        get
        {
            return phoneNumber;
        }
        set
        {
            phoneNumber = value;
        }
    }
    public void display()
    {
        Console.WriteLine();
        Console.WriteLine("Manufacturer: {0}", Manufacturer);
        Console.WriteLine("Model: {0}", Model);
        Console.WriteLine("Phone Number: {0}", PhoneNumber);
    }
4

2 回答 2

3

问题是您在父类中的空白构造函数 ,您没有在其中设置任何属性,只是忽略了参数,这就是您没有得到任何输出的原因。public Telephone(string manufacturer, string model, string phoneNumber)Telephone

修改您的构造函数并将属性设置为:

public Telephone(string manufacturer, string model, string phoneNumber)
{
   Manufacturer = manufacturer;
   Model = model;
   PhoneNumber = phoneNumber;
}

在您的子类LandlinePhone中,您正在设置属性HasCord,我相信您应该获得YES针对行的输出:

Console.WriteLine("Cordless: {0}", (yourHomePhone.HasCord ? "Yes" : "No"));

您也可以Console.ReadLine()在最后添加,以便您可以在 Visual Studio 的调试过程中看到输出。

于 2013-02-19T06:13:01.977 回答
1
public Telephone(string manufacturer, string model, string phoneNumber)
{
//You did not assign the inputs to the properties in your class.
   Manufacturer = manufacturer;
   Model = model;
   phoneNumber;

}
于 2013-02-19T06:16:22.070 回答