• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

C#实验5类的编程含实验小结

武飞扬头像
广航康小猿
帮助1

一、实验目的:

1.理解面向对象的概念,掌握C#的定义类和创建对象的方法。

2.区分类的不同数据成员,包括常量、字段和属性的定义方法,并学会控制其可访问性。

3.掌握类的方法成员的声明与调用,理解各种参数在方法中的意义及使用。

4.理解构造函数和终结器的作用机制。 

二、实验设备:

  1. PC一台;
  2. 软件:Windows OS,Visual Studio 2010等。

三、实验内容:(编写程序调试输出结果)

1.设计一个Windows应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生,中学生,大学生等派生类,当输入相关数据,单击不同的按钮(小学生,中学生,大学生)将分别创建不同的学生对象,并输出当前的学生总人数,该学生的姓名,学生类型和平均成绩。

  1.  
    using System;
  2.  
     
  3.  
    using System.Collections.Generic;
  4.  
     
  5.  
    using System.ComponentModel;
  6.  
     
  7.  
    using System.Data;
  8.  
     
  9.  
    using System.Drawing;
  10.  
     
  11.  
    using System.Linq;
  12.  
     
  13.  
    using System.Text;
  14.  
     
  15.  
    using System.Windows.Forms;
  16.  
     
  17.  
     
  18.  
    namespace 实验5._1
  19.  
    {
  20.  
    public partial class Form1 : Form
  21.  
    {
  22.  
    public Form1()
  23.  
    {
  24.  
    InitializeComponent();
  25.  
    }
  26.  
     
  27.  
    //小学生
  28.  
    private void button1_Click(object sender, EventArgs e)
  29.  
    {
  30.  
    try
  31.  
    {
  32.  
    实验5._1.Form1.Student.Pupil pu = new 实验5._1.Form1.Student.Pupil(textBox1.Text, Convert.ToInt16(textBox2.Text), Convert.ToDouble(textBox3.Text), Convert.ToDouble(textBox4.Text));
  33.  
     
  34.  
    label6.Text = pu.getInfo();
  35.  
     
  36.  
    textBox1.Text = "";
  37.  
     
  38.  
    textBox2.Text = "";
  39.  
     
  40.  
    textBox3.Text = "";
  41.  
     
  42.  
    textBox4.Text = "";
  43.  
     
  44.  
    textBox5.Text = "";
  45.  
     
  46.  
    }
  47.  
    catch
  48.  
    {
  49.  
    MessageBox.Show("请按要求输入!");
  50.  
    }
  51.  
    }
  52.  
    //中学生
  53.  
    private void button2_Click(object sender, EventArgs e)
  54.  
    {
  55.  
    try
  56.  
    {
  57.  
    实验5._1.Form1.Student.Middle pu = new 实验5._1.Form1.Student.Middle(textBox1.Text, Convert.ToInt16(textBox2.Text), Convert.ToDouble(textBox3.Text), Convert.ToDouble(textBox4.Text), Convert.ToDouble(textBox5.Text));
  58.  
     
  59.  
    label6.Text = pu.getInfo();
  60.  
     
  61.  
    textBox1.Text = "";
  62.  
     
  63.  
    textBox2.Text = "";
  64.  
     
  65.  
    textBox3.Text = "";
  66.  
     
  67.  
    textBox4.Text = "";
  68.  
     
  69.  
    textBox5.Text = "";
  70.  
     
  71.  
    }
  72.  
    catch
  73.  
    {
  74.  
    MessageBox.Show("请按要求输入!");
  75.  
    }
  76.  
    }
  77.  
    //大学生
  78.  
    private void button3_Click(object sender, EventArgs e)
  79.  
    {
  80.  
    try
  81.  
    {
  82.  
    实验5._1.Form1.Student.College pu = new 实验5._1.Form1.Student.College(textBox1.Text, Convert.ToInt16(textBox2.Text), Convert.ToDouble(textBox3.Text), Convert.ToDouble(textBox4.Text));
  83.  
     
  84.  
    label6.Text = pu.getInfo();
  85.  
     
  86.  
    textBox1.Text = "";
  87.  
     
  88.  
    textBox2.Text = "";
  89.  
     
  90.  
    textBox3.Text = "";
  91.  
     
  92.  
    textBox4.Text = "";
  93.  
     
  94.  
    textBox5.Text = "";
  95.  
    }
  96.  
    catch
  97.  
    {
  98.  
    MessageBox.Show("请按要求输入!");
  99.  
    }
  100.  
    }
  101.  
    public abstract class Student
  102.  
    {
  103.  
    protected string name;
  104.  
    protected int age;
  105.  
    protected static int count;
  106.  
     
  107.  
    public Student(string name, int age)
  108.  
    {
  109.  
    this.name = name;
  110.  
    this.age = age;
  111.  
    count ;
  112.  
    }
  113.  
    public string Name
  114.  
    {
  115.  
    get {
  116.  
    return name;
  117.  
    }
  118.  
    }
  119.  
    public virtual string type
  120.  
    {
  121.  
    get
  122.  
    {
  123.  
    return "学生";
  124.  
    }
  125.  
    }
  126.  
    public abstract double total();
  127.  
     
  128.  
    public string getInfo()
  129.  
    {
  130.  
    string result = string.Format("总人数:{0},姓名:{1},{2},{3}岁", count, Name, type, age);
  131.  
    if (type == "小学生")
  132.  
    {
  133.  
    result =string.Format(",平均成绩为{0:N2}:\n",total()/2);
  134.  
    }
  135.  
    else if(type == "中学生")
  136.  
    {
  137.  
    result = string.Format(",平均成绩为{0:N2}:\n", total() / 3);
  138.  
    }
  139.  
    else if (type == "大学生")
  140.  
    {
  141.  
    result = string.Format(",平均成绩为{0:N2}:\n", total());
  142.  
     
  143.  
    }
  144.  
    return result;
  145.  
    }
  146.  
    public class Pupil:Student
  147.  
    {
  148.  
    protected double chinese;
  149.  
    protected double math;
  150.  
    public Pupil(string name,int age,double chinese,double math):base(name,age)
  151.  
    {
  152.  
    this.chinese = chinese;
  153.  
    this.math = math;
  154.  
    }
  155.  
    public override string type
  156.  
    {
  157.  
    get
  158.  
    {
  159.  
    return "小学生";
  160.  
    }
  161.  
    }
  162.  
    public override double total()
  163.  
    {
  164.  
    return chinese math;
  165.  
    }
  166.  
    }
  167.  
    public class Middle : Student //派生中学生类
  168.  
     
  169.  
    {
  170.  
    protected double chinese;
  171.  
     
  172.  
    protected double math;
  173.  
     
  174.  
    protected double english;
  175.  
     
  176.  
    public Middle(string name, int age, double chinese, double math,double english): base(name, age)
  177.  
     
  178.  
    {
  179.  
    this.chinese = chinese;
  180.  
     
  181.  
    this.math = math;
  182.  
     
  183.  
    this.english = english;
  184.  
     
  185.  
    }
  186.  
     
  187.  
    public override string type
  188.  
     
  189.  
    {
  190.  
    get
  191.  
     
  192.  
    {
  193.  
    return "中学生";
  194.  
     
  195.  
    }
  196.  
     
  197.  
    }
  198.  
     
  199.  
    public override double total()
  200.  
     
  201.  
    {
  202.  
    return chinese math english;
  203.  
     
  204.  
    }
  205.  
     
  206.  
    }
  207.  
     
  208.  
    public class College : Student //派生大学生类
  209.  
     
  210.  
    {
  211.  
    protected double obligatory;
  212.  
     
  213.  
    protected double elective;
  214.  
     
  215.  
    public College(string name, int age, double obligatory, double elective)
  216.  
     
  217.  
    : base(name, age)
  218.  
     
  219.  
    {
  220.  
    this.obligatory = obligatory;
  221.  
     
  222.  
    this.elective = elective;
  223.  
     
  224.  
    }
  225.  
     
  226.  
    public override string type
  227.  
     
  228.  
    {
  229.  
    get
  230.  
     
  231.  
    {
  232.  
    return "大学生";
  233.  
     
  234.  
    }
  235.  
     
  236.  
    }
  237.  
     
  238.  
    public override double total()
  239.  
     
  240.  
    {
  241.  
    return obligatory elective ;
  242.  
     
  243.  
    }
  244.  
     
  245.  
    }
  246.  
    }
  247.  
    }
  248.  
    }

学新通

 2.设计一个Windows应用程序,在该程序定义平面图形抽象类和其派生类圆,矩形和三角形。该程序实现的功能包括:输入相应图形的参数,如矩形的长和宽,单机相应的按钮,根据输入参数创建图形类并输出该图形的面积。

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.ComponentModel;
  4.  
    using System.Data;
  5.  
    using System.Drawing;
  6.  
    using System.Linq;
  7.  
    using System.Text;
  8.  
    using System.Windows.Forms;
  9.  
     
  10.  
    namespace 实验5._2
  11.  
    {
  12.  
    public partial class Form1 : Form
  13.  
    {
  14.  
    public Form1()
  15.  
    {
  16.  
    InitializeComponent();
  17.  
    }
  18.  
     
  19.  
    private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
  20.  
    {
  21.  
     
  22.  
    }
  23.  
     
  24.  
    private void button1_Click(object sender, EventArgs e)
  25.  
    {
  26.  
    Circle c = new Circle(Convert.ToDouble(textBox1.Text));
  27.  
    richTextBox1.Text = "圆的面积为:" c.Area();
  28.  
    }
  29.  
     
  30.  
    private void button2_Click(object sender, EventArgs e)
  31.  
    {
  32.  
    Trangle t = new Trangle(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text));
  33.  
    richTextBox1.Text = "矩形的面积为:" t.Area();
  34.  
     
  35.  
    }
  36.  
     
  37.  
    private void button3_Click(object sender, EventArgs e)
  38.  
    {
  39.  
    S s = new S(Convert.ToDouble(textBox1.Text), Convert.ToDouble(textBox2.Text));
  40.  
    richTextBox1.Text = "三角形的面积为:" s.Area();
  41.  
     
  42.  
    }
  43.  
    public abstract class Figure
  44.  
    {
  45.  
    public abstract double Area();
  46.  
    }
  47.  
    public class Circle:Figure
  48.  
    {
  49.  
    double r;
  50.  
    public Circle(double r)
  51.  
    {
  52.  
    this.r = r;
  53.  
    }
  54.  
    public override double Area()
  55.  
    {
  56.  
    return r * r * 3.14;
  57.  
    }
  58.  
    }
  59.  
    public class Trangle:Figure
  60.  
    {
  61.  
    double h;
  62.  
    double w;
  63.  
    public Trangle(double h,double w)
  64.  
    {
  65.  
    this.h = h;
  66.  
    this.w = w;
  67.  
    }
  68.  
    public override double Area()
  69.  
    {
  70.  
    return h*w;
  71.  
    }
  72.  
    }
  73.  
    public class S:Figure
  74.  
    {
  75.  
    double h;
  76.  
    double w;
  77.  
    public S(double h,double w)
  78.  
    {
  79.  
    this.h = h;
  80.  
    this.w = w;
  81.  
    }
  82.  
    public override double Area()
  83.  
    {
  84.  
    return w * h/2;
  85.  
    }
  86.  
    }
  87.  
    private void Form1_Load(object sender, EventArgs e)
  88.  
    {
  89.  
     
  90.  
    }
  91.  
     
  92.  
    }
  93.  
    }

学新通

 学新通

3.声明一个播放器接口IPlayer,包含5个接口方法:播放,停止,暂停,上一首和下一首。

设计一个Windows应用程序,在该程序中定义一个MP3播放器类和一个AVI播放器类,以实现该接口,最后创建相应类实例测试程序集。

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.ComponentModel;
  4.  
    using System.Data;
  5.  
    using System.Drawing;
  6.  
    using System.Linq;
  7.  
    using System.Text;
  8.  
    using System.Windows.Forms;
  9.  
     
  10.  
    namespace 实验5._3
  11.  
    {
  12.  
    public partial class Form1 : Form
  13.  
    {
  14.  
    public Form1()
  15.  
    {
  16.  
    InitializeComponent();
  17.  
    }
  18.  
    IPlayer iplayer;
  19.  
    MP3 mp3;
  20.  
    AVI avi;
  21.  
    interface IPlayer //接口定义
  22.  
    {
  23.  
    string Play(); //播放
  24.  
    string Stop(); //停止
  25.  
    string Pause(); //暂停
  26.  
    string Pre(); //上一首
  27.  
    string Next(); //下一首
  28.  
    }
  29.  
    public class MP3 : IPlayer
  30.  
    {
  31.  
    public string Play()
  32.  
    {
  33.  
    return "正在播放MP3歌曲!";
  34.  
    }
  35.  
    public string Stop()
  36.  
    {
  37.  
    return "停止播放MP3歌曲!";
  38.  
    }
  39.  
    public string Pause()
  40.  
    {
  41.  
    return "暂停播放MP3歌曲!";
  42.  
    }
  43.  
    public string Pre()
  44.  
    {
  45.  
    return "播放上一首MP3歌曲!";
  46.  
    }
  47.  
    public string Next()
  48.  
    {
  49.  
    return "播放下一首MP3歌曲!";
  50.  
    }
  51.  
    }
  52.  
    public class AVI : IPlayer
  53.  
    {
  54.  
    public string Play()
  55.  
    {
  56.  
    return "正在播放AVI视频!";
  57.  
    }
  58.  
    public string Stop()
  59.  
    {
  60.  
    return "停止播放AVI视频!";
  61.  
    }
  62.  
    public string Pause()
  63.  
    {
  64.  
    return "暂停播放AVI视频!";
  65.  
    }
  66.  
    public string Pre()
  67.  
    {
  68.  
    return "播放上一首AVI视频!";
  69.  
    }
  70.  
    public string Next()
  71.  
    {
  72.  
    return "播放下一首AVI视频!";
  73.  
    }
  74.  
    }
  75.  
     
  76.  
    private void button2_Click(object sender, EventArgs e)
  77.  
    {
  78.  
    avi = new AVI(); //新建AVI对象
  79.  
    iplayer = (IPlayer)avi; //将avi转化为接口
  80.  
    }
  81.  
     
  82.  
    private void button1_Click(object sender, EventArgs e)
  83.  
    {
  84.  
    mp3 = new MP3(); //新建Mp3对象
  85.  
    iplayer = (IPlayer)mp3; //将MP3转换为接口
  86.  
    }
  87.  
     
  88.  
    private void button3_Click(object sender, EventArgs e)
  89.  
    {
  90.  
    if (mp3 is IPlayer || avi is IPlayer) //判断
  91.  
    {
  92.  
    richTextBox1.Text = "\n" iplayer.Pre();
  93.  
    }
  94.  
    else
  95.  
    {
  96.  
    MessageBox.Show("请选择播放MP3歌曲或AVI视频!");
  97.  
    }
  98.  
     
  99.  
    }
  100.  
     
  101.  
    private void button4_Click(object sender, EventArgs e)
  102.  
    {
  103.  
    if (mp3 is IPlayer || avi is IPlayer) //判断类型
  104.  
    {
  105.  
    richTextBox1.Text = "\n" iplayer.Stop();
  106.  
    }
  107.  
    else
  108.  
    {
  109.  
    MessageBox.Show("请选择播放MP3歌曲或AVI视频!");//判断是否为空
  110.  
    }
  111.  
     
  112.  
    }
  113.  
     
  114.  
    private void button5_Click(object sender, EventArgs e)
  115.  
    {
  116.  
    if (mp3 is IPlayer || avi is IPlayer)
  117.  
    {
  118.  
    richTextBox1.Text = "\n" iplayer.Play();
  119.  
    }
  120.  
    else
  121.  
    {
  122.  
    MessageBox.Show("请选择播放MP3歌曲或AVI视频!");
  123.  
    }
  124.  
     
  125.  
    }
  126.  
     
  127.  
    private void button6_Click(object sender, EventArgs e)
  128.  
    {
  129.  
    if (mp3 is IPlayer || avi is IPlayer)
  130.  
    {
  131.  
    richTextBox1.Text = "\n" iplayer.Pause();
  132.  
    }
  133.  
    else
  134.  
    {
  135.  
    MessageBox.Show("请选择播放MP3歌曲或AVI视频!");
  136.  
    }
  137.  
    }
  138.  
     
  139.  
    private void button7_Click(object sender, EventArgs e)
  140.  
    {
  141.  
    if (mp3 is IPlayer || avi is IPlayer)
  142.  
    {
  143.  
    richTextBox1.Text = "\n" iplayer.Next();
  144.  
    }
  145.  
    else
  146.  
    {
  147.  
    MessageBox.Show("请选择播放MP3歌曲或AVI视频!");
  148.  
    }
  149.  
     
  150.  
    }
  151.  
    }
  152.  
    }

 学新通

四、实验小结:

通过此次实验,理解面向对象的概念,掌握C#的定义类和创建对象的方法。.区分类的不同数据成员,包括常量、字段和属性的定义方法,并学会控制其可访问性。掌握类的方法成员的声明与调用,理解各种参数在方法中的意义及使用。理解构造函数和终结器的作用机制。 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfikigf
系列文章
更多 icon
同类精品
更多 icon
继续加载