C#:可能是多态函数?但不确定

时间:2011-01-28 16:47:30

标签: c#

我目前正在上课的实验室,我们被告知在主窗体中创建一个名为“DisplayInformation。”的方法,它接受Time类型的参数,然后显示Time对象信息“这听起来不难对我而言,我们被告知使用2个按钮来访问此方法。一个只是去基类并获取当前时间并返回它。另一个假设是转到派生类。这就是我的问题开始的地方。

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

和扩展时间类

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


    }

1 个答案:

答案 0 :(得分:2)

在DisplayInformation类中,如果从一个单击处理程序传入Time对象,而从另一个处理程序传入extTime对象,那么您应该只能调用displayTime方法。

private void DisplayInformation (Time zone)
{
    zone.displayTime();
}

在时间一上它显然会调用基本方法。在extTime上尽管您将其视为Time对象,但它将调用重写方法。