我会尽量说清楚,但请耐心听我说. 我需要制作一个节目:

  1. 创建一个名为LetterDemo的类,它实例化两个类(名为Letter和认证Letter)的对象
  2. Letter类包括信件收件人姓名和邮寄日期的自动实现属性.
  3. 在Letter类中,还要创建一个ToString()方法,该方法覆盖Object类的ToString()方法,并返回一个包含类名称的字符串(使用GetType()和Letter的数据字段值).
  4. 创建一个名为CertifiedLetter的子类,该类包含一个自动实现的属性,该属性保存信件的跟踪编号.(我假设,如果用户表明信件是经过认证的,就会给出这一点.)

我已经弄明白了大部分,但我被卡在证书和跟踪号码部分了. 对如何使用子类并在其中集成方法的任何帮助都将是救命稻草.谢谢!

//using System.ComponentModel.Design;
using System.Data;
using System.Globalization;
using System.Runtime.CompilerServices;
using static System.Console;

class LetterDemo
{
    static void Main()
    {
        Letter newLetter = new Letter();

        WriteLine("Please enter name of recipient >> ");
        string inputName = ReadLine();
        newLetter.Name = inputName;

        WriteLine("Please enter the date letter was mailed >> ");
        string inputDate = ReadLine();
        newLetter.DateMailed = inputDate;

        WriteLine("Is letter certified? Y or N >> ");
        string isCertified = ReadLine();

        if (isCertified.Length > 1)
        {
            WriteLine("Please enter only 'Y' or 'N' >> ");
        }
        else
        {
           // Send to CertifiedLetter
                }
        WriteLine(newLetter.ToString());

    }
    class Letter
    {
        private string name;
        private string dateMailed;
        public string Name { get; set; }
        public string DateMailed { get; set; }

        public override string ToString()
        {
              return GetType() + " is being sent to " + Name + " one the day of " + DateMailed + ".";
            
        }
    }
    class CertfiedLetter : Letter
    {
        public static int TrackingNumber()
        {
            Random rnd = new Random();
            int newNum = rnd.Next(1000, 9999);
            int trackingNumber = newNum;
            return trackingNumber;

        }
    }
}

推荐答案

  • 自动实现的属性:您已经正确地为Letter类中的Name和DateMailed实现了自动实现的属性.您将希望对CerfiedLetter类中的TrackingNumber执行类似的操作.

  • 认证信函类:认证信函应该继承自信函,并为跟踪编号添加一个额外的属性.TrackingNumber不应该是方法,而应该是属性.您可以在CerfiedLetter的构造函数中生成跟踪编号.

  • 处理认证信件:在Main方法中,当用户指示信件经过认证时,需要创建CertifiedLetter的实例.此实例还应该有一个名称和日期,以及跟踪编号.

  • CertifiedLetter中的ToString方法:重写CertifiedLetter中的ToString方法以包括有关跟踪编号的信息.

例如如下所示:

class LetterDemo
{
    static void Main()
    {
        WriteLine("Please enter name of recipient >> ");
        string inputName = ReadLine();

        WriteLine("Please enter the date letter was mailed >> ");
        string inputDate = ReadLine();

        WriteLine("Is letter certified? Y or N >> ");
        string isCertified = ReadLine();

        Letter letter;

        if (isCertified.Equals("Y", StringComparison.OrdinalIgnoreCase))
        {
            letter = new CertifiedLetter
            {
                Name = inputName,
                DateMailed = inputDate
            };
        }
        else
        {
            letter = new Letter
            {
                Name = inputName,
                DateMailed = inputDate
            };
        }

        WriteLine(letter.ToString());
    }
}

class Letter
{
    public string Name { get; set; }
    public string DateMailed { get; set; }

    public override string ToString()
    {
        return GetType().Name + " is being sent to " + Name + " on the day of " + DateMailed + ".";
    }
}

class CertifiedLetter : Letter
{
    public int TrackingNumber { get; }

    public CertifiedLetter()
    {
        Random rnd = new Random();
        TrackingNumber = rnd.Next(1000, 9999);
    }

    public override string ToString()
    {
        return base.ToString() + " The tracking number is " + TrackingNumber + ".";
    }
}

根据您的 comments 进行编辑:

您可以更直接地打印字母属性:

static void Main()
{
    WriteLine("Please enter name of recipient >> ");
    string inputName = ReadLine();

    WriteLine("Please enter the date letter was mailed >> ");
    string inputDate = ReadLine();

    WriteLine("Is letter certified? Y or N >> ");
    string isCertified = ReadLine();

    if (isCertified.Equals("Y", StringComparison.OrdinalIgnoreCase))
    {
        // Create and display a CertifiedLetter
        CertifiedLetter certifiedLetter = new CertifiedLetter
        {
            Name = inputName,
            DateMailed = inputDate
        };
        WriteLine(certifiedLetter.ToString());
    }
    else
    {
        // Create and display a regular Letter
        Letter letter = new Letter
        {
            Name = inputName,
            DateMailed = inputDate
        };
        WriteLine(letter.ToString());
    }
}

Csharp相关问答推荐

从.Net 6 DLL注册和检索COM对象(Typelib导出:类型库未注册.(异常来自HRESULT:0x80131165))

当用户右键单击文本框并单击粘贴时触发什么事件?

如何使用NumberFormatInfo

异步任务导致内存泄漏

从VS调试器而不是测试资源管理器运行的调试NUnitDotNet测试

将类移动到新命名空间后更新RavenDB Raven-Clr-Type

Selify只更改第一个下拉菜单,然后忽略REST-C#

在ASP.NET Core 8 MVC中本地化共享视图

为什么此名称不再被识别?名称不存在于当前上下文中?

在平行内使用跨度.用于循环

使用可空引用类型时C#接口实现错误

如何设置WinForms按钮焦点,使其看起来像是被Tab键插入其中?

从MudAutoComplete打开对话框,列表仍然可见

如何在更新数据库实体时忽略特定字段?

在使用xUnit和Mock执行单元测试时,控制器ViewResult返回空的Model集合

我什么时候不应该在Dispose中调用EgSuppressFinalize(This)?

";如何在C#中基于多个属性有效地对自定义对象列表进行排序?

调用.ToUpper()时,新字符串是否需要更多内存?

MRTK 标准着色器 - 边框光着色关闭

EF迁移不创建表