C#实验四类与对象

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实训五、在C#中实现面向对象编程
一、实训目的:
通过本章的学习,主要把握以下内容:
•理解C# 的类和对象
•使用C# 构造函数和析构函数
•使用C# 访问修饰符
•使用方法
•在C#中使用方法的重写
•理解属性及其不同的类型、实现
•理解和使用索引器
三、实验目的与要求
(1)加深理解面向对象编程的概念,如类、对象、实例化等;(2)熟悉掌握类的声明格式,特别是类的成员定义、构造函数、初始化对象等。

(3)数量掌握方法的声明,理解并学会使用方法的参数传递,方法的重载等。

四、实训内容:
(1)定义一个描述学生基本情况的类,数据成员包括姓名,学号,C#、英语和数学成绩。

成员函数包括输出数据、置姓名和学号、置三门课的成绩,求出总成绩和平均成绩。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassStudent
{
class Program
{
class Student
{
public string Sname;
public string Sno;
public int Csharp;
public int English;
public int Math;
public void display()
{
Console.WriteLine("大䨮家¨°好?!ê?我¨°是º? {0}分¤? ,ê?学¡ì号?是º? {1} ,ê?我¨°的Ì?C#成¨¦绩¡§ {2}分¤? 我¨°的Ì?英®¡é语®?成¨¦绩¡§ {3}分¤? 我¨°的Ì?数ºy学¡ì成¨¦绩¡§ {4}分¤? ", this.Sname, this.Sno, this.Csharp, this.English, this.Math);//this.我¨°自Á?己o的Ì?****
}
public void GiveSname(string name)
{
this.Sname = name;
}
public void GiveSno(string Sno)
{
this.Sno = Sno;
}
public void GiveCsharp(int Csharp)
{
this.Csharp = Csharp;
}
public void GiveEnglish(int English)
{
this.English = English;
}
public void GiveMath(int Math)
{
this.Math = Math;
}
public double Avg(int c, int e, int m)
{
double avg = (c + e + m) / 3.0;
return avg;
}
public double Count(int c, int e, int m)
{
double con = (c + e + m);
return con;
}
}
static void Main(string[] args)
{
double a, c;
Student s = new Student();
Console.WriteLine("姓?名?:êo");
String Sname = Console.ReadLine();
s.GiveSname(Sname);
Console.WriteLine("学¡ì号?:êo");
String Sno = Console.ReadLine();
s.GiveSno(Sno);
Console.WriteLine("C#成¨¦绩¡§:êo");
int Csharp = Convert.ToInt16(Console.ReadLine());
s.GiveCsharp(Csharp);
Console.WriteLine("英®¡é语®?成¨¦绩¡§:êo");
int English = Convert.ToInt16(Console.ReadLine());
s.GiveEnglish(English);
Console.WriteLine("数ºy学¡ì成¨¦绩¡§:êo");
int Math = Convert.ToInt16(Console.ReadLine());
s.GiveMath(Math);
s.display();
Console.WriteLine("我¨°的Ì?平?均¨´分¤?是º? {0}分¤? ,ê?我¨°的Ì?总Á¨¹分¤?是º? {1}分¤? ", a = s.Avg(Csharp, English, Math), c = s.Count(Csharp, English, Math));
}
}
}
(2)定义一个人员类CPerson,包括数据成员:姓名、编号、性别和用于输入输出的成员函数。

并实现对人员信息的输入输出。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cprison
{
class Program
{
class CPerson
{
private string Name;
private string Pno;
private string Sex;
public void Intput()
{
Console.WriteLine("姓?名?:êo");
= Console.ReadLine();
Console.WriteLine("编À¨¤号?:êo");
this.Pno = Console.ReadLine();
Console.WriteLine("性?别Àe:êo");
this.Sex = Console.ReadLine();
}
public void Output()
{
Console.WriteLine("大䨮家¨°好?,ê?我¨°是º? {0},ê?我¨°的Ì?编À¨¤号?是º? {1},ê?我¨°的Ì?性?别Àe是º? {2}", , this.Pno, this.Sex);
}
}
static void Main(string[] args)
{
CPerson cp = new CPerson();
cp.Intput();
cp.Output();
}
}
}。

相关文档
最新文档