c#
题目
自定义一个时间类。该类包含小时、分、秒字段和属性,具有将秒增1操作的方法
要求定义一个Timel类,包括:
(1)3个私有字段表示时、分、秒。
(2)两个构造函数,一个通过传入的参数对时间初始化,一个获得系统当前的时间。
(3)3个只读属性对时、分、秒的读取。
(4)一个方法用于对秒增 1操作(注意60进位的问题)
主函数
button按钮
时间类
Addsecond 函数
所有函数:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _4_2实验
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
t.AddSecond();
textBox5.Text = Convert.ToString(t.H1);//第一个窗口
textBox1.Text = Convert.ToString(t.m1);//第二个窗口
textBox4.Text = Convert.ToString(t.s1);//第三个窗口
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
time t = new time();
private void Form1_Load(object sender, EventArgs e)
{
textBox5.Text =Convert.ToString(t.H1);
textBox1.Text = Convert.ToString(t.m1);
textBox4.Text = Convert.ToString(t.s1);
}
}
class time
{
private int hour;
private int minute;
private int second;
public int H1
{
get
{
return hour;
}
}
public int m1
{
get
{
return minute;
}
}
public int s1
{
get
{
return second;
}
}
public time() {
this.hour = System.DateTime.Now.Hour;
this.minute = System.DateTime.Now.Minute;
this.second = System.DateTime.Now.Second;
}
public time(int hour,int minute,int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
public void AddSecond()
{
second++;
if (second >= 60)
{
second = second % 60;
minute++;
}
if (minute >= 60)
{
minute = minute % 60;
hour++;
}
}