为什么我的 FormLoad 事件处理程序调用自定义 class' 默认构造函数而不是带参数的构造函数?
Why does my FormLoad event handler call the custom class' default constructor instead of the one with arguments?
我正在为一项任务制作一个基本的 multiclass GUI 程序,我在其中创建了三个自定义 classes,它们继承了抽象 class(ReadingMaterial)和一个界面。我 运行 遇到的问题是,当我 运行 它应该传递给每个 class 的构造函数的信息时(我已经包含了 class Online for example) 没有被传递,那些 classes 正在调用它们的默认构造函数,所以我的 GUI 组件显示零和空格而不是填充传递给构造函数的其他信息。为什么它不调用正确的构造函数而不是默认的构造函数?
(GUI 表单部分 class)
public partial class PresentationGUI : Form
{
private Book book;
private Magazine magazine;
private Online online;
public PresentationGUI()
{
InitializeComponent();
}
private void rdBtnOnline_CheckedChanged(object sender, EventArgs e)
{
txtBxPageCount.Text = online.PageCount.ToString();
txtBxTitle.Text = online.Title;
txtBxAuthor.Text = online.Author;
txtBxURL.Text = online.WebsiteURL;
txtBxPrintable.Text = online.HardCopyAvailability();
// ...
}
private void PresentationGUI_Load(object sender, EventArgs e)
{
book = new Book(1000, "C# Programming", "Barbara Doyle",
"Cengage", "5th Edition");
online = new Online(5, "C Sharp (Programming Language)", "Crowd Sourced Author",
"https://en.wikipedia.org/wiki/C_Sharp_(programming_language)");
magazine = new Magazine(200, "PC Magazine", "Varied Authors",
"Ziff Davis", 6, 16);
}
}
(在线class继承自ReadingMaterial摘要class(下))
public class Online : ReadingMaterial, IPrintable
{
private string websiteURL;
public string WebsiteURL { get; set; }
public Online()
:base()
{
websiteURL = "";
}
public Online(int pageCount, string title, string author, string url)
:base(pageCount, title, author)
{
websiteURL = url;
}
public string HardCopyAvailability()
{
return "Printable";
}
}
(基础class阅读材料)
public abstract class ReadingMaterial
{
private int pageCount;
private string title;
private string author;
public int PageCount { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public ReadingMaterial()
{
pageCount = 0;
title = "";
author = "";
}
public ReadingMaterial(int pageCount, string title, string author)
{
this.pageCount = pageCount;
this.title = title;
this.author = author;
}
}
不是构造函数的问题。您没有将支持变量连接到属性。要么这样做:
public abstract class ReadingMaterial
{
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
...
}
或者完全放弃支持变量并使用自动实现的属性。如果您这样做,C# 会自动为您创建一个隐藏的支持变量。
public string Title { get; set; }
并在构造函数中,将参数分配给 属性 而不是支持变量。
this.Title = title;
我正在为一项任务制作一个基本的 multiclass GUI 程序,我在其中创建了三个自定义 classes,它们继承了抽象 class(ReadingMaterial)和一个界面。我 运行 遇到的问题是,当我 运行 它应该传递给每个 class 的构造函数的信息时(我已经包含了 class Online for example) 没有被传递,那些 classes 正在调用它们的默认构造函数,所以我的 GUI 组件显示零和空格而不是填充传递给构造函数的其他信息。为什么它不调用正确的构造函数而不是默认的构造函数?
(GUI 表单部分 class)
public partial class PresentationGUI : Form
{
private Book book;
private Magazine magazine;
private Online online;
public PresentationGUI()
{
InitializeComponent();
}
private void rdBtnOnline_CheckedChanged(object sender, EventArgs e)
{
txtBxPageCount.Text = online.PageCount.ToString();
txtBxTitle.Text = online.Title;
txtBxAuthor.Text = online.Author;
txtBxURL.Text = online.WebsiteURL;
txtBxPrintable.Text = online.HardCopyAvailability();
// ...
}
private void PresentationGUI_Load(object sender, EventArgs e)
{
book = new Book(1000, "C# Programming", "Barbara Doyle",
"Cengage", "5th Edition");
online = new Online(5, "C Sharp (Programming Language)", "Crowd Sourced Author",
"https://en.wikipedia.org/wiki/C_Sharp_(programming_language)");
magazine = new Magazine(200, "PC Magazine", "Varied Authors",
"Ziff Davis", 6, 16);
}
}
(在线class继承自ReadingMaterial摘要class(下))
public class Online : ReadingMaterial, IPrintable
{
private string websiteURL;
public string WebsiteURL { get; set; }
public Online()
:base()
{
websiteURL = "";
}
public Online(int pageCount, string title, string author, string url)
:base(pageCount, title, author)
{
websiteURL = url;
}
public string HardCopyAvailability()
{
return "Printable";
}
}
(基础class阅读材料)
public abstract class ReadingMaterial
{
private int pageCount;
private string title;
private string author;
public int PageCount { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public ReadingMaterial()
{
pageCount = 0;
title = "";
author = "";
}
public ReadingMaterial(int pageCount, string title, string author)
{
this.pageCount = pageCount;
this.title = title;
this.author = author;
}
}
不是构造函数的问题。您没有将支持变量连接到属性。要么这样做:
public abstract class ReadingMaterial
{
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
...
}
或者完全放弃支持变量并使用自动实现的属性。如果您这样做,C# 会自动为您创建一个隐藏的支持变量。
public string Title { get; set; }
并在构造函数中,将参数分配给 属性 而不是支持变量。
this.Title = title;