C#基础编程设计实验报告
C# 基础编程 设计实验报告
一、实验目的
1、熟悉 Visual Studio .NET 开发环境。
2、掌握 C#应用程序的基本操作过程。
3、掌握 C#的数据类型,运算符以及表达式的使用。
4、掌握分支和循环语句的使用方法。
5、掌握一维数组,二维数组及数组型数组的使用。
二、实验要求
(1)编写程序要规范、正确,上机调试过程和结果要有记录 (2)做完实验后给出本实验的实验报告。
三、实验设备、环境
安装有 Visual Studio .NET 软件。
四、实验步骤
1、分析题意。
2、根据题目要求,新建项目。
3、编写并输入相关的程序代码。
5、运行与调试项目。
6、保存项目。
五、实验内容
1、编写一个简单的控制台应用程序,打印一行文字(如你的姓名)。
using System; using System.Collections.Generic;
using System.Linq; using System.Text;
namespace one.first {
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("我叫王蕾!");
}
} } 2、编写一个简单的 Windows 应用程序,在窗体 Load 事件中书写代码,标签中显示你的姓名。
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 one.second {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Windows 程序";
Label lblShow = new Label();
lblShow.Location = new Point(20, 30);
lblShow.AutoSize = true;
lblShow.Text = "王蕾!";
this.Controls.Add(lblShow);
}
}
} 3、编写一个一个程序,用来判断输入的是大写字母,小写字母,数字还是其他的字符。
using System; using System.Collections.Generic; using System.Text;
namespace one.third {
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入一个字符:");
char c = Convert.ToChar(Console.ReadLine());
if ((c>="a"&&c<="z")||(c>="A"&&c<="Z"))
Console.WriteLine("这是一个字母");
if (char.IsDigit(c))
Console .WriteLine("这是一个数字");
}
}
} 4、分别用 while,do-while,for 循环求 1 到 100 的和。
using System; using System.Collections.Generic; using System.Text;
namespace one.forth.one {
class Program
{
static void Main(string[] args)
{
int i = 1, sum = 0;
while (i <= 100)
{
sum = sum + i;
i++;
}
Console.WriteLine("1 到 100 的自然数之和为:" + sum);
}
}
} using System; using System.Collections.Generic; using System.Text;
namespace one.forth.two {
class Program
{
static void Main(string[] args)
{
int i = 1, sum = 0;
do
{
sum = sum + i;
i++;
}
while (i <= 100);
Console .WriteLine("1 到 100 的自然数的和为:" + sum );
}
}
} using System; using System.Collections.Generic; using System.Text;
namespace one.forth.three {
class Program
{
static void Main(string[] args)
{
int i , sum = 0;
for (i = 1; i <= 100; i++)
{
sum = sum + i;
}
Console.WriteLine("1 到 100 的自然数的和为:" + sum);
}
} } 5、定义一个一维数组,用随机数为此赋值,用 foreach 循环输
出其中的内容。
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace first.five {
class Program
{
static void Main(string[] args)
{
int[] a = {0,1,2,3,4};
foreach (int i in a)
{
Console.WriteLine(a[i]);
}
}
} } 6、实现二维数组的输入和输出。
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
namespace first.six {
class Program
{
static void Main(string[] args)
{
int[,] a = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{ Console.WriteLine(a[i, j]); }
}
}
}
} }
7、实现数组型数组的输入和输出。
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace first.seven {
class Program
{
static void Main(string[] args)
{
int[][] a = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 } };
for (int i = 0; i < a.Length; i++)
{
for (int j = 0; j < a[i].Length; j++)
{
Console.WriteLine(a[i][j]);
}
}
}
} } 六、实验体会(遇到问题及解决办法,编程后的心得体会)
刚开始编程的时候觉得无从下手,尽管我们已经学了好几种高级编程语言,但每个都有其独特的地方,稍不留神就会混淆。
通过这次实验,我体会到课后复习巩固的重要性。在编程的时候,很多内容都不记得,需要去翻书。不得不说,实验是巩固课程的好方法!本次实验,我熟悉 Visual Studio .NET 开发环境;掌握了 C#应用程序的基本操作过程;掌握了 C#的数据类型,运算符以及表达式的使用;掌握了分支和循环语句的使用方法以及一维数组,二维数组及数组型数组的使用。
实验项目名称:
类与对象
实验学时:
6
同组学生姓名:
实验地点:
1318
实验日期:
10 月 26 日-11 月 9 日 实验成绩:
批改教师:
批改时间:
实验 2
类与对象
一、实验目的、要求
(1)掌握类的定义和使用; (2)掌握类的数据成员,属性的定义和使用; (3)掌握方法的定义,调用和重载以及方法参数的传递; (4)掌握构造函数的定义和使用。
二、实验要求
(1)编写程序要规范、正确,上机调试过程和结果要有记录; (2)做完实验后给出本实验的实验报告。
三、实验设备、环境
安装有 Visual Studio .NET 软件。
四、实验步骤
1、分析题意; 2、根据题目要求,新建项目; 3、编写并输入相关的程序代码; 5、运行与调试项目; 6、保存项目。
五、实验内容
1、定义一个方法,实现两个数的交换(分别把参数按值传递和按引用传递)。
using System;
using System.Collections.Generic; using System.Text;
namespace second.one {
class Program
{
static void Main(string[] args)
{
Swaper s = new Swaper();
Console.WriteLine("输入 x 的值:");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("输入 y 的值:");
int b=Convert.ToInt32(Console.ReadLine());
Console.WriteLine(s.Swap(a, b));
Console.WriteLine(s.Swap(ref a,ref b));
}
class Swaper
{
public string Swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
return string.Format("按值传参交换之后:x={0},y={1}",x,y);
}
public string Swap(ref int x, ref int y)
{
int temp;
temp = x;
x = y;
y = temp;
return string.Format("按引用传参交换之后:x={0},y={1}", x, y);
}
}
} }2、定义一个方法,实现数组的排序。
using System; using System.Collections.Generic; using System.Text;
namespace second.two {
class Program
{
public class sort
{
public void change(int[] a)
{
Console.WriteLine("排序前,数组顺序为:");
show(a);
int i, j, m;
for (i = 0; i < 10; i++)
{
m = a[i];
j = i - 1; //a[j]为数组前一个值
while (j >= 0 && m > a[j])//判断 i 下标的数是否大于 j 下标的数
{
a[j + 1] = a[j];//如果 i 下标大于j 把 j 往后移一个位
j--;
}
a[j+1] = m; //当不大于 j 的时候就把 M的值放到 i 下标下面 j+1 是为了下标减到最前时考虑 -1 + 1 还是下标的最前面
}
Console.WriteLine("排序后,数组顺序为:");
show(a);
}
void show(int[] a)
{
int i;
for (i = 0; i < 10; i++)
{
Console.Write("{0} ", a[i]);
}
Console.WriteLine();
}
}
static void Main(string[] args)
{
int[] a ={ 4, 7, 1, 2, 5, 8, 9, 10, 3, 6 };
sort s=new sort();
s.change(a);
}
} } 3、定义一个学生类,把学生类当作对象来传递。
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace second.three {
class Program
{
public class student
{
public void st()
{
int a = 999;
}
}
public class st
{
public void aa(student s)
{
Console.WriteLine(s);
}
}
static void Main(string[] args)
{
student s=new student();
st s1 = new st();
s1.aa(s);
}
} } 4、定义一个方法,求两个数的和和差,通过参数把这两个值带回。
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace
second.four
{
class Program
{
public class sum
{
public void ab(out int m, out
int n,int a, int b)
{
m = a + b;
n = a - b;
}
}
static void Main(string[] args)
{
sum s = new sum();
int a = 10;
int b = 3;
int m, n;
s.ab(out m, out n, a, b);
Console.WriteLine("{0}+{1}={2};{0}-
{1}={3}",a,b,m,n);
}
} } 5、用构造函数重载,实现矩形的面积,圆的面积,梯形的面积; using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace secong.five {
class Program
{
public class square
{
public double area;
public square() { }
public square(double a)
{
area = a * a * 3.14;
}
public square(double a, double b)
{
area = a * b;
}
public square(double a, double b, double h)
{
area = (a + b) / 2 * h;
}
}
static void Main(string[] args)
{
double a, b, h,area;
a = 2; b = 5; h = 3;
square s = new square(a,b);
Console.WriteLine("求矩形面积,长为 a={0},宽为 b={1},面积 area={2}",a,b,s.area);
square i = new square(a);
Console.WriteLine("求圆形面积,半径 a={0},面积 area={1}", a, i.area);
square j = new square(a, b, h);
Console.WriteLine("求梯形面积,上底为a={0},下底为 b={1},高为 h={2}面积 area={3}", a, b,h, j.area);
}
} } 6、设计一个 windows 应用程序,在该程序中定义一个学生类和班级类,以处理每个学生的学号,姓名,语文,数学和英语成绩,要求:
1)能查询每个学生的总成绩。
2)能显示全班前三名的名单。
3)能显示单科成绩最高分和不及格的学生名单。
4)能统计全班学生的平均成绩。
5)能显示各科成绩不同分数段的学生人数的百分比。
Student 类:
using System; using System.Collections.Generic; using System.Text; namespace Test2_6 {
public class Student
{
public string stuNo;
public string name;
public double chinese;
public double math;
public double english;
public double sumScore
{
get { return chinese + math + english; }
}
} } StudentList 类:
using System; using System.Collections.Generic; using System.Text; namespace Test2_6 {
public class StudentList:Student
{
int snums;
public Student[] stu=new Student[50];
public StudentList()
{
snums = 0;
}
public void addstu(Student s)
{
stu[snums] = s;
snums++;
}
public int searchstu(string name)
{
int i;
for (i = 0; i < snums; i++)
{
if (stu[i].name == name) break;
}
if (i == snums) return -1;
else return i;
}
//给所有成绩排序,用后面实现前三名的排名
public void ProThree()
{
for (int i = 0; i < snums; i++)
{
int k = i;
for (int j = i + 1; j < snums; j++)
if (stu[j].sumScore > stu[k].sumScore) k = j;
if (k != i)
{
Student temp;
temp = stu[k];
stu[k] = stu[i];
stu[i] = temp;
}
}
}
//显示单科成绩的最高分
public int HighScore(int k)
{
int p = 0;
if (k == 0)
{
for (int i = 1; i < snums; i++)
if (stu[i].math > stu[p].math) p = i;
}
else if (k == 1)
{
for (int i = 1; i < snums; i++)
if (stu[i].chinese > stu[p].chinese) p = i;
}
else
{
for (int i = 1; i < snums; i++)
if (stu[i].chinese > stu[p].chinese) p = i;
}
return p;
}
//显示不及格名单
public string
BuhgName(int k)
{
string name=" ";
if (k == 0)
{
for (int i = 0; i < snums; i++)
if (stu[i].math < 60) name +=stu[i].name+"\n";
}
else if (k == 1)
{
for (int i = 0; i < snums; i++)
if (stu[i].chinese < 60) name += stu[i].name + "\n";
}
else
{
for (int i = 0; i < snums; i++)
if (stu[i].english < 60) name += stu[i].name + "\n";
}
return name;
}
public string getHL()
{
string Maxer = " ", Loser = " ";
Maxer += " 单 科 数 学 最 高 :
" + stu[HighScore(0)].name + "\n";
Maxer += " 单 科 语 文 最 高 :
" +
stu[HighScore(1)].name + "\n";
Maxer += " 单 科 英 语 最 高 :
" + stu[HighScore(2)].name + "\n";
Loser += "单科数学挂科名单:" +BuhgName(0) + "\n";
Loser += "单科语文挂科名单:" + BuhgName(1) + "\n";
Loser += "单科英语挂科名单:" + BuhgName(2) + "\n";
return Maxer + "\n" + Loser;
}
//全班的平均成绩
public string SumScore()
{
double sum = 0;
double avg=0;
for (int i = 0; i < snums; i++)
{
sum = sum + stu[i].sumScore;
}
avg = sum / snums;
return "班级总分平均分:"+avg;
}
//各科成绩不同分数段的学生百分比
//英语成绩各分数段百分比
public string PerC()
{
double per1, per2, per3, per4, per5;
double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;
for (int i = 0; i < snums; i++)
{
if ((stu[i].chinese > 90) && (stu[i].chinese <= 100))
{
sumC1++;
}
else if ((80 <= stu[i].chinese) && (stu[i].chinese < 90))
{
sumC2++;
}
else if((70<=stu[i].chinese)&& (stu[i].chinese < 80))
{
sumC3++;
}
else if((60<=stu[i].chinese)&&(stu[i].chinese < 70))
{
sumC4++;
}
else
{sumC5++;}
}
per1 = sumC1 / snums;
per2 = sumC2 / snums;
per3 = sumC3 / snums;
per4 = sumC4 / snums;
per5 = sumC5 / snums;
return " 语 文 成 绩 百 分 比 :"+"\n"+"90~100:"+per1+"
80~90:"+per2+"
80~70:"+per3+"
70~60:"+per4+"
60 以下的:"+per5;
}
//数学成绩各分数段百分比
public string PerM()
{
double per1, per2, per3, per4, per5;
double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;
for (int i = 0; i < snums; i++)
{
if ((stu[i].math> 90) &&(stu[i].math <= 100))
{
sumC1++;
}
else if ((80 <= stu[i].math) && (stu[i].math < 90))
{
sumC2++;
}
else if ((70 <= stu[i].math) && (stu[i].math < 80))
{
sumC3++;
}
else if ((60 <= stu[i].math) && (stu[i].math < 70))
{
sumC4++;
}
else
{ sumC5++; }
}
per1 = sumC1 / snums;
per2 = sumC2 / snums;
per3 = sumC3 / snums;
per4 = sumC4 / snums;
per5 = sumC5 / snums;
return string.Format("数学成绩百分比:" + "\n" + "90~100:" + per1 + "
80~90:" + per2 + "
80~70:" + per3 + "
70~60:" + per4 + "
60 以下的:" + per5);
}
//英语成绩各分数段百分比
public string PerE()
{
double per1, per2, per3, per4, per5;
double sumC1 = 0, sumC2 = 0, sumC3 = 0, sumC4 = 0, sumC5 = 0;
for (int i = 0; i < snums; i++)
{
if ((stu[i].english > 90) && (stu[i].english <= 100))
{
sumC1++;
}
else if ((80 <= stu[i].english) && (stu[i].english < 90))
{
sumC2++;
}
else if ((70 <= stu[i].english) && (stu[i].english < 80))
{
sumC3++;
}
else if ((60 <= stu[i].english) && (stu[i].english < 70))
{
sumC4++;
}
else
{ sumC5++; }
}
per1 = sumC1 / snums;
per2 = sumC2 / snums;
per3 = sumC3 / snums;
per4 = sumC4 / snums;
per5 = sumC5 / snums;
return string.Format("数学成绩百分比:" + "\n" + "90~100:" + per1 + "
80~90:" + per2 + "
80~70:" + per3 + "
70~60:" + per4 + "
60 以下的:" + per5);
}
} } From 窗体代码:
using System; using System.Collections.Generic;
using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Test2_6 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public StudentList sl = new StudentList();
private void btnAdd_Click(object sender, EventArgs e)
{
Student s = new Student();
s.stuNo = txtStuNo.Text;
s.name = txtName.Text;
s.chinese = Convert.ToDouble(txtChina.Text);
s.math = Convert.ToDouble(txtMath.Text);
s.english = Convert.ToDouble(txtEng.Text);
sl.addstu(s);
MessageBox.Show("添加成功");
}
private void btnSearch_Click(object sender, EventArgs e)
{
int pos = sl.searchstu(this.textBox1.Text);
if (pos != -1)
{
label7.Text = this.textBox1.Text + "的总成绩:" + sl.stu[pos].sumScore;
}
else { MessageBox.Show("不存在这个人!"); }
}
private void btnFinish_Click(object sender, EventArgs e)
{
label7.Text = "前 3 名:"+"\n";
for (int i = 0; i < 3; i++)
{
sl.ProThree();
label7.Text+= sl.stu[i].name+"\n";
}
label7.Text += sl.getHL()+"\n";
label7.Text += Convert.ToString(sl.SumScore())+"\n";
label7.Text += sl.PerC()+"\n";
label7.Text += sl.PerM()+"\n";
label7.Text += sl.PerE()+"\n";
}
} }
六、实验体会(遇到问题及解决办法,编程后的心得体会)
通过本次实验,我掌握了类的定义与使用;掌握了类的数据成员,属性的定义和使用;掌握了方法的定义,调用和重载以及方法参数的传递以及构造函数的定义和使用。值得注意的是:本次实验中 return的使用以及所在的位置,类型转换时也经常用到
实验项目名称:
继承与多态
实验学时:
6
同组学生姓名:
实验地点:
1318
实验日期:
11 月 16 日-11 月 30 日 实验成绩:
批改教师:
批改时间:
实验 3
继承与多态
一、实验目的、要求
(1)掌握类的继承性与多态性; (2)掌握虚方法的定义以及如何使用虚方法实现多态; (3)掌握抽象类的定义以及如何使用抽象方法实现多态; 二、实验要求
(1)编写程序要规范、正确,上机调试过程和结果要有记录; (2)做完实验后给出本实验的实验报告。
三、实验设备、环境
安装有 Visual Studio .NET 软件。
四、实验步骤
1、分析题意; 2、根据题目要求,新建项目; 3、编写并输入相关的程序代码; 5、运行与调试项目; 6、保存项目。
五 、实验内容
1、设计一个 Windows 应用程序,在该程序中首先构造一个学生基本类,再分别构造小学生、中学生、大学生派生类,当输入相关数据,单击不用的按钮时,将分别创建不同的学生类对象,并输出当前学生的总人数,该学生的姓名,学生类型,平均成绩。
Student 类:
using System; using System.Collections.Generic; using System.Text; namespace Test3_1 {
public abstract class Student
{
protected string name;
protected int age;
public static int number;
public Student(string name, int age)
{
this.name = name;
this.age = age;
number++;
}
public string Name
{
get { return name; }
}
public abstract double Average();
}
public class Pupil : Student
{
protected double chinese;
protected double math;
public Pupil(string name, int age, double chinese, double math)
: base(name, age)
{
this.chinese = chinese;
this.math = math;
}
public override double Average()
{
return (chinese + math) / 2;
}
}
public class Middle : Student
{
protected double chinese;
protected double math;
protected double english;
public Middle(string name, int age, double
chinese, double math, double english)
: base(name, age)
{
this.chinese = chinese;
this.math = math;
this.english = english;
}
public override double Average()
{
return (chinese + math + english) / 3;
}
}
public class College : Student
{
protected double required;
protected double elective;
public College(string name, int age, double required, double elective)
: base(name, age)
{
this.required = required;
this.elective = elective;
}
public override double Average()
{
return (required + elective) / 2;
}
} } Form 窗体内的代码:
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Test3_1 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSmall_Click(object sender, EventArgs e)
{
Pupil p = new Pupil(txtName.Text,Convert.ToInt32(txtAge.Text),Convert.ToDouble(txtChinese.Text),Convert.ToDouble(txtMath.Text));
lblShow.Text += " 总 人 数 :" +Convert.ToString( Student.number) + "," + "姓名:" + p.Name + "," + "小学生" + "," + "平均成绩为:" + p.Average() +"\n";
}
private void btnMiddle_Click(object sender, EventArgs e)
{
Middle m = new Middle(txtName.Text, Convert.ToInt32(txtAge.Text), Convert.ToDouble(txtChinese.Text), Convert.ToDouble(txtMath.Text),Convert.ToDouble(TxtEnglish.Text));
lblShow.Text += " 总 人 数 :" + Convert.ToString(Student.number) + "," + "姓名:" + m.Name +
"," + "中学生" + "," + "平均成绩为:" + m.Average() + "\n";
}
private void btnBig_Click(object sender, EventArgs e)
{
College c = new College(txtName.Text, Convert.ToInt32(txtAge.Text), Convert.ToDouble(txtChinese.Text), Convert.ToDouble(txtMath.Text));
lblShow.Text += " 总 人 数 :" + Convert.ToString(Student.number) + "," + "姓名:" + c.Name + "," + "大学生" + "," + "平均成绩为:" + c.Average() + "\n";
}
} } 2、设计一个 Windows 应用程序,在该程序中定义平面图形抽象类和派生类圆,矩形和三角形。
Figure 类代码:
using System; using System.Collections.Generic; using System.Text; namespace Test3_2
{
public abstract class Figure
{
public abstract double Area();
}
public class Circle:Figure
{
double radius;
public Circle(double r)
{
radius = r;
}
public override double Area()
{
return radius * radius * 3.14;
}
}
public class JUxing:Figure
{
double chang;
double kuan;
public JUxing(double c, double k)
{
this.chang = c;
this.kuan = k;
}
public override double Area()
{
return chang * kuan;
}
}
public class San:Figure
{
double bian;
double heigth;
public San(double b, double h)
{
this.bian = b;
this.heigth = h;
}
public override double Area()
{
return bian * heigth / 2;
}
} } Form 窗体代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Test3_2 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCircle_Click(object sender, EventArgs e)
{
Circle c=new
Circle(Convert.ToInt32(TxtChang.Text));
lblShow.Text = "圆的面积为:" + c.Area();
}
private void btnJu_Click(object sender, EventArgs e)
{
JUxing j = new JUxing(Convert.ToInt32(TxtChang.Text),Convert.ToInt32(TxtHigh.Text));
lblShow.Text = "矩形的面积为:" + j.Area();
}
private void btnSan_Click(object sender, EventArgs e)
{
San s = new San(Convert.ToInt32(TxtChang.Text), Convert.ToInt32(TxtHigh.Text));
lblShow.Text = "三角形的面积为:" + s.Area();
}
} }
3、定义一个 Person 类,包含姓名字段和一个方法,早上 8:30学生开始上课,教师开始讲课。分别用 new 关键字,虚方法,抽象类实现多态性。
New 关键字:
using System; using System.Collections.Generic; using System.Text;
namespace third.three {
class Program
{
static void Main(string[] args)
{
Student s=new Student("学生");
Teacher t=new Teacher("教师");
Console.WriteLine(s.name+s.work());
Console.WriteLine(t.name+t.work());
Console.ReadLine();
}
}
public class Person
{
public string name;
public interface method
{ string work();}
}
public class Student:Person
{
public Student(string name)
{ this.name = name; }
public string work()
{ return "早上 8:30 开始上课"; }
}
public class Teacher:Person
{
public Teacher (string name)
{ this.name = name; }
public string work()
{ return "开始讲课"; }
} } 虚方法:
using System;
using System.Collections.Generic; using System.Text;
namespace third.three.two {
class Program
{
static void Main(string[] args)
{
Student s = new Student("张三","学生");
PersonWork(s);
Teacher t=new Teacher("李斯","教师");
PersonWork(t);
}
private static void PersonWork(Person Person)
{ Console.WriteLine(Person.Work()); }
}
public class Person
{
public string name;
public Person(string name)
{ this.name = name; }
public virtual string Work()
{ return string.Format("Person{0}:早上 8:30 开始",name);}
}
public class Student : Person
{
private string type;
public Student(string name, string type)
: base(name)
{ this.type = type; }
public override string Work()
{
return string.Format("Person{0}:早上 8:30 开始上课", name);
}
}
public class Teacher : Person
{
private string type;
public Teacher(string name, string type)
: base(name)
{ this.type = type; }
public override string Work()
{
return string.Format("Person{0}:开始讲课", name);
}
} }
抽象类:
using System; using System.Collections.Generic; using System.Text;
namespace third.three.three {
class Program
{
static void Main(string[] args)
{
Student s = new Student("张三", "学生");
PersonWork(s);
Teacher t = new Teacher("李斯", "教师");
PersonWork(t);
}
private static void PersonWork(Person person)
{
Console.WriteLine(person.Work());
}
}
public abstract class Person
{
public string name;
public Person(string name)
{ this.name = name; }
public abstract string Work();
}
public class Student : Person
{
private string type;
public Student(string name, string type)
: base(name)
{
this.type = type;
}
public override string Work()
{
return string.Format("Person{0}:早上 8:30 开始上课", name);
}
}
public class Teacher : Person
{
private string type;
public Teacher(string name, string type)
: base(name)
{
this.type = type;
}
public override string Work()
{
return string.Format("Person{0}:开始讲课", name);
}
}
}
六、实验体会(遇到问题及解决办法,编程后的心得体会)
通过本次实验,我理解了类的继承性与多态性;掌握了虚方法的定义以及如何用虚方法来实现多态;掌握了抽象类的定义以及如何用抽象方法来实现多态。
这次实验与前两次不同,采用 Windows 应用程序,既涉及到代码段也涉及到界面的设计。所以,勉强通过实验。
实验项目名称:
接口、文件和流
实验学时:
6
同组学生姓名:
实验地点:
A205
实验日期:
12 月 7 日-12 月 21 日 实验成绩:
批改教师:
批改时间:
实验 4
接口、文件和流
一、实验目的
(1)掌握接口的定义及使用方法; (2)掌握流,序列化和反序列化的概念和使用方法; (3)掌握流文件的读写操作类及其使用方法; (4)掌握 OpenFileDialog,SaveFileDialog 等控件的使用。
二、实验要求
(1)编写程序要规范、正确,上机调试过程和结果要有记录; (2)做完实验后给出本实验的实验报告。
三、实验设备、环境
安装有 Visual Studio .NET 软件。
四、实验步骤
1、分析题意; 2、根据题目要求,新建项目; 3、编写并输入相关的程序代码; 5、运行与调试项目; 6、保存项目。
五 、实验内容
1、定义一个 Person 类,包含姓名字段和一个方法,早上 8:30学生开始上课,教师开始讲课。用接口来实现。
using System; using System.Collections.Generic;
using System.Text; namespace Test4_1 {
class Program
{
static void Main(string[] args)
{
Student s = new Student("张三","学生");
Console.WriteLine(s.Work());
Teacher t = new Teacher("李四","老师");
Console.WriteLine(t.Work());
}
public abstract class Person
{
public string name;
public Person(string name)
{
this.name = name;
}
}
interface IPerson
{
string type { get;}
string Work();
}
public class Student :Person, IPerson
{
public string type
{
get { return string.Format("老师"); }
}
public Student(string name, string type)
: base(name)
{
this.name=name;
}
public string Work()
{
return string.Format("Person{0}:早上 8:30 开始上课", name);
}
}
...
- 范文大全
- 职场知识
- 精美散文
- 名著
- 讲坛
- 诗歌
- 礼仪知识
-
超星尔雅学习通《对话大国工匠致敬劳动模范》题库附答案
超星尔雅学习通《对话大国工匠致敬劳动模范》题库附答案 1、历史只会眷顾坚定者、奋进者、搏击者,而不会
【入党申请书】 日期:2021-05-12
-
对于政治生态考核整改工作方案
本文系作者原创投稿,仅供学习参考,请勿照搬照抄! 关于政治生态考核整改工作的方案 为做好推进风清气正
【经济工作】 日期:2020-06-05
-
大学生学习2024年两会精神心得感悟
大学生学习2024年两会精神心得感悟过去一年,是全面贯彻二十大精神的开局之年,中国共产党带领全国各族人民,付出艰辛努力,换来重大成
【心得体会】 日期:2024-03-07
-
中国传统故事英文版 中国古代故事英文版
历史学科蕴含着许多丰富的、生动的、有趣的素材,每一个历史事件、历史人物都有相关的、动人的历史小故事,都能给人以启迪。你对中国古代的故事了解多少呢?下面是小编为您...
【调查报告】 日期:2019-05-22
-
基尔霍夫定律验证实验报告
基尔霍夫定律的验证的实验报告本文关键词:基尔,定律,霍夫,验证,实验基尔霍夫定律的验证的实验报告本文
【思想宣传】 日期:2021-03-08
-
中小学党建工作实施意见
中小学党建设工作实施意见中小学校担负着培养德智体美全面发展的社会主义建设者和接班人的重要使命。加强中
【爱国演讲】 日期:2020-09-22
-
地藏经诵读仪规(完整版)
地藏经诵读仪规(完整版) 恭请文: 恭请大慈大悲大愿地藏王菩萨、护法诸天菩萨慈悲加持护念弟子***能
【个人简历】 日期:2021-03-31
-
青年学生学习全国人大十四届二次会议心得感想16篇
青年学生学习全国人大十四届二次会议心得感想16篇报告中提到政府在经济调控、消费政策、基础设施和制造业投资、房地产调控以及地方债务
【心得体会】 日期:2024-03-07
-
小学党建工作制度
小学党建工作制度33篇 党建工作责任制度 1 党支部年初制定全年党建工作计划,将目标任务分解到有关部
【思想学习】 日期:2021-02-10
-
材料力学考题
材料力学考题本文关键词:材料力学,考题材料力学考题本文简介:材料力学1、简易起重设备中,AC杆由两根
【入党申请书】 日期:2021-03-06
-
执行信息公开网
执行信息公开网 执行信息公开网 执行信息公开网: zhi*ing (点击下图可直接进行访问) 全国
【职场知识】 日期:2020-07-03
-
年国家开放大学电大电子商务单选题题库
单选: 1、EDI是指A、电子商务B、电子数据交换C、电子交易 D、移动数据交换 答案: B 2、电
【职场知识】 日期:2020-06-05
-
有机磷酸酯类中毒及其解救(实验报告范文)
有机磷酸酯类中毒及其解救XXX、XXX一、实验目的1 观察有机磷酸酯类农药敌百虫中毒时的症状。 2
【职场知识】 日期:2020-08-30
-
大学教师毕业设计指导记录4篇
大学教师毕业设计指导记录4篇 毕业设计是指工、农、林科高等学校和中等专业学校学生毕业前夕总结性的独立作业。是实践性教学最后一
【职场知识】 日期:2022-05-11
-
“以学生为中心”的教学原则
以学生为中心的教学原则教师在开展以学生为中心的教学实践中,必须谨记学习目标不再是知识的获得,能力要比知识更重要。以下是蒲公英阅读网
【职场知识】 日期:2023-01-05
-
2021教育基础知识试题(附答案)
2021教育基础知识精选试题(附答案) 1、主张恢复西方传统教育核心价值,反对“进步教育
【职场知识】 日期:2021-03-17
-
组工干部学习谈治国理政第三卷《共建创新包容开放型世界经济》心得体会
组工干部学习谈治国理政第三卷《共建创新包容的开放型世界经济》心得体会 《习近平谈治国理政》第三卷第七
【职场知识】 日期:2020-09-22
-
男一分钟仰卧起坐标准表
表表11--13 男生一分钟仰卧起坐、引体向上单项评分表(单位:次) 等级 单项 得分 三年级 四年
【职场知识】 日期:2021-05-08
-
心理健康黑板报_心理健康黑板报图片
虽然工作上难免压力,但是只要正视压力,一切就不会太辛苦。下面就随小编看看心理健康黑板报内容,希望喜欢哦。 心理健康黑板报图片欣赏 心理健康黑板报图片1 心理健...
【职场知识】 日期:2020-02-26
-
“从青风公司审计案例看销售与收款循环审计”案例说明书
“从青风公司审计案例看销售与收款循环审计”案例说明书一、本案例要解决的关键问
【职场知识】 日期:2020-09-28
-
唐代诗人李昂个人信息
唐代诗人李昂个人信息 导读:我根据大家的需要整理了一份关于《唐代诗人李昂个人信息》的内容,具体内容:
【古典文学】 日期:2020-11-07
-
[关于中秋的朗诵诗词] 关于爱国的朗诵诗词
中秋,热闹的街头树起了灯彩,舞起了火龙。你知道多少关于中秋的朗诵诗词?下面小编为你整理了几篇关于中秋的朗诵诗词,希望对你有帮助。 关于中秋的朗诵诗词一 中秋佳节...
【古典文学】 日期:2019-06-06
-
叠加原理实验报告
一、实验目的1、通过实验来验证线性电路中的叠加原理以及其适用范围。 2、学习直流仪器仪表的测试方法。
【古典文学】 日期:2020-11-12
-
输血查对制度
输血查对制度依据卫生部《临床输血技术规范》的要求,制订抽血交叉配备查对制度、取血查对制度、输血查对制
【古典文学】 日期:2020-09-24
-
大气唯美黑板报【国庆节大气黑板报】
日本在投降的那一天,再也没有昔日的嚣张,我们中国的屈辱得到洗刷。下面就随小编看看国庆节大气黑板报内容,希望喜欢哦。 国庆节大气黑板报图片欣赏 国庆节大气黑板报...
【古典文学】 日期:2019-05-05
-
怎样认识世界处于百年未有之大变局
怎样认识世界处于百年未有之大变局 首先,“大变局”是对国际格局发生巨大变迁的
【古典文学】 日期:2020-10-28
-
【二人旅游英语情景对话】 二人英语对话2分钟旅游
随着国内外旅游业市场的不断扩大,旅游英语人才成为社会的紧缺人才。小编精心收集了二人旅游英语情景对话,供大家欣赏学习! 二人旅游英语情景对话1 A:Itsmyfirsttimeto...
【古典文学】 日期:2020-02-29
-
法律知识手抄报图片大全|法律知识手抄报
我国开展了全面的普法宣传工作,法制宣传教育、普及法律常识作为经常的重要任务。做法制教育手抄报,普及法律知识。下面是小编为大家带来的法律知识手抄报图片大全,希望大家...
【古典文学】 日期:2020-03-10
-
2021公安专业知识考试练习题(附答案)
2021公安专业知识考试练习题(附答案) 1 甲地公安机关接到群众举报,在当天举行的大型娱乐活动中,
【古典文学】 日期:2021-01-29
-
乳糖检测方法
附录A(规范性附录) 乳糖的测定A 1原理牛乳或乳粉样液经沉淀剂澄清后,样液中的乳糖在苯酚、氢氧化钠
【古典文学】 日期:2020-12-08
-
时尚女装店面装修效果图|韩式女装店面装修
在服装店的设计之中,我们要将多变、创新、品牌自身的定位与发展趋势相结合,用一种可持续的设计方式呈现出来,以便更加适应不断更新的展示主体。下面小编就为大家解开时尚女装店...
【中国文学】 日期:2019-05-16
-
2021年超星尔雅学习通《辩论与修养》章节测试试题(共183题附答案)
2021年超星尔雅学习通《辩论与修养》章节测试试题(共183题附答案)1、辩论的目的不是单纯获得某种
【中国文学】 日期:2021-05-12
-
天地人格最佳搭配起名技巧|天地人格的五行怎么算
天地有阴有阳,物体刚柔表里,而数字则有一个诱导力,那么你知道怎么计算天地人格来取名吗?今天小编为你整理了天地人格最佳搭配起名技巧,一起来看看用天地人格取名的方法有哪些...
【中国文学】 日期:2019-06-06
-
信息技术重要性
信息技术的重要性 信息技术与课程整合将带来课程内容的革新,信息技术的高速发展,要求传统的课程必须适应
【中国文学】 日期:2021-02-11
-
【世界上最大的半岛】阿拉伯半岛
你知道世界上最大的半岛是什么吗?下面由小编来介绍一下。 阿拉伯半岛的简介 阿拉伯半岛(阿拉伯文:)位于亚洲,是世界上最大的半岛。沙特阿拉伯、也门、阿曼、阿拉伯联合...
【中国文学】 日期:2019-05-24
-
2022年当前世界下中国面临国际形势论文范本
和平与发展仍然是当今时代的主题。谋和平、求合作、促发展是各国人民的共同愿望。为了大家学习方便,下面是小编为大家整理的当前世界下中国面临的国际形势论文范文内容,以供参...
【中国文学】 日期:2022-03-31
-
雪天安全行车注意事项_雪天安全行车提示语
维护城市交通秩序,争做河源文明市民。你们想看看雪天安全行车提示语有哪些吗?以下是小编推荐雪天安全行车提示语给大家,欢迎大家阅读! 安全行车温馨提示语【经典篇】 1...
【中国文学】 日期:2020-03-15
-
古代人物漫画女生唯美图片欣赏 漫画人物图片女孩唯美
中国漫画始于清末民初,而平面设计虽然其名称是在改革开放以后确立的,但设计活动却自古就有,二者的相互影响是本文的主要讨论范围。小编整理了唯美古代女生人物漫画,欢迎阅读!...
【中国文学】 日期:2020-03-19
-
2021年5月时事政治热点(国内+国际)
2021年年5月时事政治热点(国内+国际)国内部分 1 55月月66日,由商务部和海南省人民政府共同
【中国文学】 日期:2021-06-10
-
关于通过努力获得成功的故事:靠自己努力成功的例子
努力,是成功的一半。人生道路上难免会遇到挫折,但我们不应后退,应向理想之路奋勇前进。关于名人努力成功的故事你了解吗?以下是小编分享的关于通过努力获得成功的故事,一起...
【中国文学】 日期:2020-03-03
-
山东省生产经营单位安全生产主体责任规定(303号令)
山东省生产经营单位安全生产主体责任规定(2013年2月2日山东省人民政府令第260号公布根据2016
【外国名著】 日期:2020-10-22
-
改革开放大事记简表(改革开放新时期1978-2012年)
改革开放大事记简表 (1978-2012年) 时间1978年12月18日至22日地点北京事件党的十一
【外国名著】 日期:2021-06-17
-
大学生音乐欣赏论文 大学音乐鉴赏论文3000
今天小编就为你介绍关于大学生音乐欣赏论文,下面是!小编给你搜集了相关资料!希望可以能帮助到大家。 大学生音乐欣赏论文—第一篇 音乐是生活不可缺少的一部分,学会欣...
【外国名著】 日期:2019-05-27
-
材料力学金属扭转实验报告
材料力学金属扭转实验报告 【实验目的】 1、验证扭转变形公式,测定低碳钢的切变模量G。;测定低碳钢和
【外国名著】 日期:2020-11-27
-
长豆角家常做法怎么做好吃营养 炒豆角的家常做法
豆角在我们日常生活中是很常见的食材,可能我们只知道它含有优质蛋白和维生素,其实它还有其他的营养价值。它也是可以和很多食材做搭配的。下面小编为大家整理了长豆角的做法...
【外国名著】 日期:2020-02-26
-
白烛葵的花语:白烛葵的不死幻想症
白烛葵,花名,花语为“不感兴趣”。现又指《知音漫客》上连载漫画《极度分裂》里主要角色之一。下面小编为你整理了白烛葵的花语。欢迎阅读。 白烛葵的花语:不感兴趣 ...
【外国名著】 日期:2019-05-11
-
植物装饰画黑白图片欣赏|荷花装饰画黑白图片
装饰画是一种装饰性艺术,是装饰性和创造性相结合的艺术设计形式。小编整理了植物装饰画黑白,欢迎阅读! 植物装饰画黑白图片展示 植物装饰画黑白图片1 植物装饰画黑白...
【外国名著】 日期:2019-05-31
-
坚定不移全面从严管党治警研讨发言稿
坚定不移全面从严管党治警研讨发言稿政治建警、从严治警是党在新时代的建警治警方针。一年前的全国公安工作
【外国名著】 日期:2020-09-18
-
(新版)就业知识竞赛题库及答案解析
(新版)就业知识竞赛题库(全真题库) 一、单选题1 (单选):在职业生涯规划工具中,组织在展开员工职
【外国名著】 日期:2021-07-21
-
[10.1旅游去哪里好玩] 旅游去哪里好玩
十月一到,秋意已在一个我们不经意的黎明走来,习习凉风,却是最适合出门游行。小编为您整理了10 1旅游去哪里好玩,秋天,我们一起出发吧。 1、云南建水古城 建水古城...
【外国名著】 日期:2020-03-01
-
梧桐花的花语|梧桐花的功效与作用
梧桐花为梧桐科植物梧桐的花,植物形态详梧桐子条。今天小编为你整理了梧桐花的花语,欢迎阅读。 梧桐花的花语是:情窦初开 在春季里晚开的花朵,有着恬淡的气息。 ...
【寓言童话】 日期:2020-03-03
-
西部计划笔试题库(99题含答案)
西部计划笔试题库(99题含答案) 1 第十三届全国人大三次会议表决通过了《中华人民共和国民法典》,自
【寓言童话】 日期:2021-06-16
-
大学生音乐欣赏论文 大学音乐鉴赏论文3000
今天小编就为你介绍关于大学生音乐欣赏论文,下面是!小编给你搜集了相关资料!希望可以能帮助到大家。 大学生音乐欣赏论文—第一篇 音乐是生活不可缺少的一部分,学会欣...
【寓言童话】 日期:2020-03-12
-
年学生资助诚信教育主题活动方案
各二级学院(部): 为深入贯彻落实习近平总书记关于教育的重要论述,落实立德树人根本任务,增强当代大学
【寓言童话】 日期:2020-06-21
-
主题教育调查研究工作方案2篇
主题教育调查研究工作方案1根据省、市、县开展“不忘初心、牢记使命”主题教育工
【寓言童话】 日期:2021-03-19
-
油管、套管规格尺寸对照表
API油管规格及尺寸 公称尺寸(in) 不加厚外径(mm) 不加厚内径(mm) 加厚外径(mm) 加
【寓言童话】 日期:2020-08-31
-
惊悚鬼故事50字 令人惊悚的故事
这些惊悚故事在短短的篇幅和时间之内让您感受到故事里传达出来的恐怖感,令你感到害怕。下面就是小编给大家整理的令人惊悚的故事,希望对你有用! 令人惊悚的故事篇1:学校...
【寓言童话】 日期:2019-05-13
-
【古代男生漫画图片大全】男生漫画头像
漫画和动画组成了动漫产业的两大支柱。然而,与动画相比,漫画在业界和学界皆相对冷清。小编整理了古代男生漫画,欢迎阅读! 古代男生漫画图片展示 古代男生漫画图片1 ...
【寓言童话】 日期:2019-05-27
-
北京最好吃的自助餐厅 北京高档自助餐排名
自助餐简直就是拯救大胃王的最佳饮食!没有之一!世界上没有什么事情是吃一顿自助餐解决不了的,如果有,那就吃两顿!下面小编给大家推荐北京几家好吃的自助餐。 北京最好吃的...
【寓言童话】 日期:2020-02-25
-
廉洁自律自我剖析材料(精选)
廉洁自律自我剖析材料((精选多篇)) 信念。科学文化,提高自身素质的终身学习的意识,紧密联系群众,调
【寓言童话】 日期:2020-07-20
-
学生高考动员演讲稿
学生高考动员演讲稿3篇高考动员演讲稿11 老师们、同学们: 大家下午好!漫漫高考长征路已经进入尾声了
【百家讲坛】 日期:2021-09-22
-
企业安全演讲稿2021
最新企业安全的演讲稿5篇 演讲稿是作为在特定的情境中供口语表达使用的文稿。在充满活力,日益开放的今天
【百家讲坛】 日期:2021-09-22
-
XX镇扶贫项目实施专项整治工作总结_1
XX镇扶贫项目实施专项整治工作总结 为深入贯彻精准扶贫精准脱贫基本方略,认真落实党中央、国务院,省委
【百家讲坛】 日期:2021-09-22
-
对乡镇领导班子干部成员批评意见例文
对乡镇领导班子干部成员的批评看法范文 一、对党委书记XXX同志的批评看法〔3条〕 1、与干部交流偏少
【百家讲坛】 日期:2021-09-22
-
群英乡扶贫资金项目芬坡村祖埇村生产道路硬化工程绩效自评报告
群英乡扶贫资金项目((芬坡村祖埇村生产道路硬化工程))绩效自评报告 一、基本情况(一)群英乡扶贫资金
【百家讲坛】 日期:2021-09-22
-
党委书记警示教育大会上讲话2021汇编
党委书记在警示教育大会上的讲话55篇汇编 党委书记在警示教育大会上的讲话(一) 同志们: 根据省州委
【百家讲坛】 日期:2021-09-22
-
对于2021年召开巡视整改专题民主生活会对照检查材料
关于12021年召开巡视整改专题民主生活会对照检查材料 按照中央巡视组要求和省、市、区委统一部署,区
【百家讲坛】 日期:2021-08-14
-
消防安全知识培训试题.doc
消防安全知识培训试题姓名: 部门班组: 成绩: 一:填空题,每空4分,共44分。 1、灭火剂是通过隔
【百家讲坛】 日期:2021-08-14
-
涉疫重点人员“五包一”居家隔离医学观察工作流程
涉疫重点人员“五包一”居家隔离医学观察工作流程 目前,全球疫情仍处于大流行状
【百家讲坛】 日期:2021-08-14
-
疫情防控致全体师生员工及家长一封信
疫情防控致全体师生员工及家长的一封信 各位师生员工及全体家长朋友: 暑假已至,近期我省部分地方发现确
【百家讲坛】 日期:2021-08-14