在程序中定义Student基类和派生类.docVIP

  • 6
  • 0
  • 约2.93千字
  • 约 4页
  • 2017-05-02 发布于北京
  • 举报
在程序中定义Student基类和派生类

创建Windows应用程序,在程序中定义Student基类及派生类Student_l 基类定义 1、包括“学号”、“姓名”、“性别”、“年龄”等字段。 2、显式声明默认构造函数,声明含“学号”、“姓名”、“性别”、“年龄”等参数的构造函数重载 3、声明用于显示对象信息的方法 派生类定义 1、包括两门课程成绩的字段 2、显示声明默认构造函数,声明含“学号”、“姓名”、“性别”、“年龄”与两门课程成绩参数的构造函数重载 3、声明求两门课程总分与两门课程平均分的方法。 运行 1、在界面中创建相应的输入项目textbox 2、在文本框中输入派生类对象的数据,单击【创建对象】的按钮,用文本框中的数据通过派生类构造函数创建派生类对象,并将对象的数据显示在输出窗口。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication8 { ??? public partial class Form1 : Form ??? { ??????? public Form1() ??????? { ??????????? InitializeComponent(); ??????? } ??????? private void button1_Click(object sender, EventArgs e) ??????? { ??????????? int id=int.Parse(this.txtId.Text);//取值,个人信息 ??????????? string name=this.txtName.Text; ??????????? string sex=this.txtSex.Text; ??????????? int age=int.Parse(this.txtAge.Text); ??????????? int english=int.Parse(this.txtEng.Text);//两门学科成绩 ??????????? int languages=int.Parse(this.txtLang.Text); ??????????? Student1 stu = new Student1(id, name, sex, age);//创建一个对象 ??????????? int t=stu.Total(english, languages);//调用类,总分 ??????????? int a = stu.Average(english, languages);//调用类,平均分 ??????????? string str = ();//调用继承类中的info方法 ??????????? MessageBox.Show(str+\r\n+总分:+t+\r\n平均分:+a);//输出 ??????? } ??? } } using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WindowsFormsApplication8 { ??? class Student ??? { ??????? protected int studentid;//学号 ??????? protected string name; ??????? protected string sex; ??????? protected int age; ??????? public Student()//构造函数 ??????? { ??????? } ??????? public Student(int studentid,string name,string sex,int age)//构造函数 ??????? { ??????????? this.studentid = studentid; ??????????? = name; ??????????? this.sex = sex; ??????????? this.age = age; ??????? } ??????? public string info()//方法,用于输出个人信息 ??????? { ??????????? string str = 学号: + studentid + \r\

文档评论(0)

1亿VIP精品文档

相关文档