C#:可能是多态函数?但不确定
我目前正在为一个类的实验室工作,我们被告知以主形式创建一个名为“DisplayInformation.它接受类型为Time的参数,然后显示时间对象信息”的方法,这听起来并不难,但我们被告知使用2个按钮来访问此方法。我们只需转到基类,获取当前时间并返回它。另一个是假定转到派生类。这就是我的问题开始的地方C#:可能是多态函数?但不确定,c#,C#,我目前正在为一个类的实验室工作,我们被告知以主形式创建一个名为“DisplayInformation.它接受类型为Time的参数,然后显示时间对象信息”的方法,这听起来并不难,但我们被告知使用2个按钮来访问此方法。我们只需转到基类,获取当前时间并返回它。另一个是假定转到派生类。这就是我的问题开始的地方 private void btnDisplayTime_Click(object sender, EventArgs e) { DisplayInformation(time1); }
private void btnDisplayTime_Click(object sender, EventArgs e)
{
DisplayInformation(time1);
}
这就是我不确定的DisplayInformation功能:
private string DisplayInformation (Time zone)
{
time1 = zone;
time1.displayTime();
// extTime1.displayTime();
return "okay";
}//end of DisplayInformation
当我用第一个按钮调用DisplayInformation时,一切都会很好。
如果我从第二个按钮调用这个方法,也可以。但我需要能够选择我要上哪门课。真的很难解释。
我只需要能够从每个按钮调用该方法,并根据按钮获得不同的输出。我不确定这是否只是一个if语句或什么
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CIS247A_Week_4_Lab_BREWER
{
class Time
{
private const int MIN_HOUR = 0;
private const int MAX_HOUR = 24;
internal int hour;
internal int minute;
public Time()
{
hour = 12;
minute = 00;
}//end of Time
public Time(int Hour, int Minute)
{
hour = Hour;
minute = Minute;
}//end of Time
public int incrementHour(int step)
{
if (step > 0 && hour < 24)
{
//step = step % hour;
hour = (hour + step) % 24;
return hour;
}//end of if
else
{
MessageBox.Show("Please enter a positive number.");
return 0;
}//end of else
}//end of incrementHour
public int incrementMinute(int step)
{
if (step > 0 && minute < 60)
{
minute = (minute + step) / 60;
return 0;
}//end of if
else if (step < 0)
{
MessageBox.Show("Please enter a positive number.");
minute = 0;
return 0;
}//end of else if
else
{
MessageBox.Show("Unknown error.");
return 0;
}
}//end of incrementMinute
public virtual string displayTime()
{
DateTime time = DateTime.Now; // Use current time
string format = "MMM ddd d HH:mm yyyy"; // Use this format
MessageBox.Show(time.ToString(format)); // Write to console
return time.ToString(format);
}//end of displayTime
public int Hour
{
get { return hour; }
set
{
if (value < MIN_HOUR)
{
hour = 0;
MessageBox.Show("Hour value " + value.ToString() + " cannot be negative. Reset to " + MIN_HOUR.ToString(),
"Invalid Hour", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
//take the modulus to ensure always less than 24 hours
//works even if the value is already within range, or value equal to 24
hour = value % MAX_HOUR;
}
}
}
public int Minute
{
get { return minute; }
set
{
if (value < 0)
{
minute = 0;
//MessageBox.Show("cannot be negitive" , MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
minute = value % 60;
}
}
}
}//end of Time Class
}//end of Namespace
为了时间起见,我正在处理的表格部分:
public partial class frmTime : Form
{
Time time1;
ExtendedTime extTime1;
public frmTime()
{
//DateTime Ctime = DateTime.Now; // Use current time
// Ctime = new DateTime();
// label1.Text = Ctime.ToString();
InitializeComponent();
time1 = new Time();
extTime1 = new ExtendedTime();
}
private void DisplayInformation (Time zone)
{
time1 = zone;
// time1.displayTime();
extTime1.displayTime();
//return "okay";
}//end of DisplayInformation
//exit Button (btnExit)
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();//closes the program
}
private void btnDisplayTime_Click(object sender, EventArgs e)
{
DisplayInformation(time1);
}
在DisplayInformation类中,如果您从一个单击处理程序传入一个时间对象,从另一个处理程序传入一个extTime对象,那么您应该能够调用displayTime方法
private void DisplayInformation (Time zone)
{
zone.displayTime();
}
在第一时间,它显然会调用base方法。在extTime one上,尽管您将其视为时间对象,但它将调用重写的方法。您能同时提供这两个类吗?我们不能讨论没有类的多态性和继承。两个按钮使用相同的事件处理程序吗?是的。只是通过一个不同的变量“DisplayInformation(extTime1);”传递,您不能只从一个按钮调用time1.displayTime(),从另一个按钮调用extTime1.displayTime(),或者您不打算使用这样的两个不同实例吗?或者实际上调用zone.displayTime()/face palm>\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu/爆炸。谢谢克里斯,该死的。我尽量不发布有用的答案,这样我就可以得到无名英雄徽章了(尽管如此,我想我仍然是44名学生中的11名。”——@allthosemiles:这几乎可以肯定是你应该学习的课程,所以现在一定要记住,好吗?:)^的确如此,先生。非常感谢。明白了。我几乎觉得这不是完整的课程,但是因为这个节目的所有额外部分。但是谢谢!好啊无论如何,这当然是一个关键的教训。祝其余的人好运。:)
private void DisplayInformation (Time zone)
{
zone.displayTime();
}