文档库 最新最全的文档下载
当前位置:文档库 › 实验二(C#窗体应用程序)

实验二(C#窗体应用程序)

实验二(C#窗体应用程序)
实验二(C#窗体应用程序)

实验二

一.步骤

1.添加控件

在Windows窗体中添加三个标签控件和三个文本控件,控件的名称分别为label1、label2、label3和textBox1、textBox2、textBox3。在添加一个命令按钮控件,名称为button1。

2.编写事件处理代码

public partial class Form1 : Form

{

public class Person//定义Person类

{

public string name; //定义name字段

private int age; //定义age字段

public void sayAge() //定义sayAge方法

{

MessageBox.Show("I am " + age.ToString() + " years old.");

}

public Boolean setAge(int a) //定义setAge方法

{

if (a > 0 && a <= 120)

{ age = a;

return true }

e lse return false;}

}

public class Student:Person

{

public string Major;

public void sayMajor()

{ MessageBox.Show("My Major is " + Major.ToString());

}

}

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

Student s = new Student();

s.setAge(Convert.ToInt16(textBox2.Text));

s.Major = textBox3.Text;

s.sayAge();

s.sayMajor();

}

}

二.截图

图2-1 界面

图2-2 测试结果

图2-3 测试结果

相关文档