c# - 无法输出存储的信息

标签 c#

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

    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);
    }

最佳答案

问题是你的 blank constructor 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 的调试过程中看到输出。

关于c# - 无法输出存储的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14950988/

相关文章:

c# - 析构函数 (c#) 中的异常?

c# - 如何在 Entity Framework 中使用 LINQ-to-SQL 回滚事务?

c# - 如何使用 System.Net.Http.HttpClient 在 POST 后使用 AllowAutoRedirect = true 进行 GET

c# - 排序数组的二分查找

javascript - 从服务器到客户端的 UTC 偏移量

c# - 如何在 JsonConverter 中注入(inject)/访问 HttpContext?

c# - 是否有可能在 1bpp 位图上使用 Graphics.DrawString?

c# - 您可以使用 Entity Framework 6 从主键中删除 Identity 吗?

c# - Protocol Buffer - protobuf-csharp-port : Does the equivalent of JAVA API call CodedInputStream. getBytesUntilLimit() 存在吗?

c# - VSTO c# 检查单元格是否为空