私は現在、クラスのラボに取り掛かっていて、 "DisplayInformation。"と呼ばれるメインフォームで、Time型のパラメータを受け取り、Timeオブジェクト情報を表示するメソッドを作成するように指示されました。私にとっては難しいですが、このメソッドにアクセスするには2つのボタンを使用するように指示されています。 1つは基本クラスに行き、現在の時刻を取得して返します。もう1つは、派生クラスに行くと仮定します。そして、これが私の問題が始まるところです。C#:おそらく多型関数ですか?しかし確かではありません
private void btnDisplayTime_Click(object sender, EventArgs e)
{
DisplayInformation(time1);
}
そして、これは私がわからないんだけど表示情報機能である:私は最初のボタンで表示情報を呼び出すと
private string DisplayInformation (Time zone)
{
time1 = zone;
time1.displayTime();
// extTime1.displayTime();
return "okay";
}//end of DisplayInformation
それがうまく行きます。 そして、私が2番目のボタンからこのメソッドに電話をかけると、それはうまくいくでしょう。しかし、私はどのクラスに行くか選ぶことができる必要があります。本当に説明するのは難しいです。 ボタンごとにそのメソッドにコールし、ボタンに応じて異なる出力を得ることができればいいだけです。これが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
そしてextendedtimeクラス
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CIS247A_Week_4_Lab_BREWER
{
class ExtendedTime : Time
{
private string timeZone{get; set;}
public ExtendedTime() : base()
{
timeZone = "CDT";
}//end of ExtendedTime
public ExtendedTime(int Hour, int Minute, String TimeZone) :base(Hour, Minute)
{
timeZone = TimeZone;
}//end of ExtendedTime
public override string displayTime()
{
//return base.displayTime();
MessageBox.Show(base.displayTime() + timeZone);
return base.displayTime() + timeZone;
}//end of DisplayTime
}//end of ExtendedTime 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);
}
両方のクラスを提供できますか?私たちはクラスなしで多形性と相続について議論することはできません。 – Amy
理解しやすい。編集されました。 –
両方のボタンに同じイベントハンドラを使用していますか? – Gabe