文档库 最新最全的文档下载
当前位置:文档库 › 第3章面向对象编程基础

第3章面向对象编程基础

第三章面向对象编程基础

一选择题

1.语言的核心是面向对象编程(OOP),所有OOP语言都至少具有3个特性:(A)

A.封装,继承和多态 B. 类,对象和方法

C.封装,继承和派生 D. 封装,继承和接口

2. C#的构造函数分为实例构造函数和静态构造函数,实例构造函数可以对(C)进行初始化,静态构造函数只能对(A)进行初始化。

A.静态成员

B.非静态成员

B.静态成员或非静态成员

C.静态成员和非静态成员

3.C#实现了完全意义上的面向对象,所以它没有(D),任何数据域和方法都必须封装在类体中。

A.全局变量

B.全局常数

C.全局方法

D.全局变量,全局常数和全局方法

4.方法中的值参数是(A)的参数。

A.按值传递

B.按引用传递

C.按地址传递

D.不传递任何值

5.下面对方法中的ref和out参数说明错误的是(C)

A.ref和out参数传递方法相同,都是把实在参数的内存地址传递给方法,实参与形参指向同一个内存存储区域,但ref要求实参必须在调用之前明确赋过值。

B.ref是将实参传入形参,out它只有用于从方法传出值,而不能用从方法调用处接收实参数据。

C.ref和out参数因为传递的是实参的地址,所以要求实参和形参的数据类型必须一致。

D.ref和out参数要求实参和形参的数据类型或者一致,或者实参能被隐式的转化为为形参的类型。

6.假设class Mclass类的一个方法的签名为:public void Max,(out int max,params int [] a ),m1是Mclass类的一个对象,maaxval是一个int型的值类型变量,arrayA 是一个int型的数组对象,则下列调用该方法有错的是()。

A.m1.Max(out maxval);

B. m1.Max(out maxval,4,5,3,);

C.m1.Max(out maxval,ref arrayA);

D.m1.Max(out maxval,3,3.5);

7.以下有关属性的叙述正确的是()

A.要求与字段域一一对应

B.只包含get访问器的属性是只写属性

C.不能把它当变量使用

D.在静态属性访问器中可访问静态数据

二.填空题

1.构析函数不能由程序显示地调用,而是由系统在(释放对象)时自动调用。如果这个对象是一个派生类对象,那么在调用构析函数时,除了执行派生类的构析函数,也会执行基类的构析函数,其执行顺序与构析函数(正好相反)。

2.C#实现了完全意义上的面向对象,所以它没有(全局变量、全局函数和全局方法),任何数据域,方法都必须封装在类中。

3.在类中如果一个数据成员被声明为static的,则说明这个类的所有实例都共享这个static数据成员。在类体外,static成员不能通过(继承)来访问,它必须通过(静态方法(构造函数)方法)来访问。

4.程序运行结果()

using System;

public class Test

{

Public void changel( string s) {

s = s + “Changel”;

}

public void change2 ( ref string s ) {

s = s + “Change2”;

}

public void change3 (string s1, out string s2 ) {

s1 = s1 + “Change3”;

s2 = s1;

}

}

public class Exe8

{

public static void Main () {

string s1, s2;

s1 = “Hello, ”;

Test t1=new Test();

t1.changel(s1);

Console.WriteLine (“s1 after call to change1 is {0}”, s1 );

t1.change2( ref s1);

Console.WriteLine(“s1 after call to change2 is {0}”, s1);

t1.chnage3(s1, out s2 );

Console.WriteLine(“s1 after call to change3 is {0}”, s1);

Console.WriteLine(“s2 after call to change3 is {0}”, s2);

Console.Read();

}

}

5.程序运行结果是:

(s1 after call to change1 is Hello

S1 after call to change2 is Hello.change2

S1 after call to change3 is Hello.change2

S2 after call to change3 is Hello.change2.change3)

using System;

public class Test

{

public void change ( string s) {

s = s + “Change1”;

}

public void change ( ref string s ) {

s = s + “Change2”;

}

public void change (string s1, out string s2 ) {

s1 = s1 + “Change3”;

s2 = s1;

}

}

public class Exe9

{

public static void Main () {

string s1, s2;

s1 = “Hello, ”;

Test t1=new Test();

t1.change (s1);

Console.WriteLine (“s1 is {0}”, s1 );

t1.change ( ref s1);

Console.WriteLine (“s1 is “{0}”, s1 );

t1.change (s1, out s2 );

Console.WriteLine (“s1 is {0}, s2 is {1}”, s1, s2 );

Console.Read();

}

}

三.编程题

1.定义描述复数的类,并实现复数的输入和输出。设计三个方法分别完成复数的加法,减法和乘法运算。

解:Using System;

Using System Collections Generic;

Using System Text;

Namespace

{

static void main(string[]args)

{complex a=new complex(2,5);

complex b=new complex(4,6);

complex c=a+b;

c.print();complex d=a-b;

d.print();complex m=a+b;

m.print();comsole.Read();

}

}

Class complex

{

Double r,v;

Public complex(double r,double v)

{ this r=r; this v=v;

Public complex {}

Public static complex operator+(complex a, complex b)

{ return new complex (a·r+b·r, a·r+b·v);}

Public static complex operator-( complex a, complex b)

{double y,k; y=a·r*a·v-a·v*b·v;

K=a·r+b·v+a·v*b·r;

Return new complex(y,k);}

Public void print()

{console.write(r+“+”+v+“i/n”);

}

}

}

2.定义全班学生成绩类,包括:姓名,学号,C++成绩,英语成绩,数学成绩和平均成绩。设计4个方法:

(1)全班成绩的输入;

(2)求出每一个同学的平均成绩;

(3)按平均成绩的升序排序;

(4)输出全班成绩。

using System;

using System.Collections.Generic;

using System.Text;

using System.Text.RegularExpressions;

static void Main(string[] args)

{

#region Student Text

AllStudent all = new AllStudent(3);

all.AddAllreslut();

all.RtAvg();

all.printStu();

all.sorting();

Console.WriteLine("(冒泡排序)排序后成绩如下:");

all.printStu();

#endregion

}

#region Student类

public class Students

{

#region 构造函数

public Students()

{

}

public Students(string name, string number, float Cres, float Elys, float maths)

{

_name = name;

_number = number;

_Cres = Cres;

_ely = Elys;

_math = maths;

_avg = (Cres + Elys + maths) / 3;

}

#endregion

#region 字段

private string _name;

public string Name

{

get { return _name; }

set { _name = value; }

}

private string _number;

public string Number

{

get { return _number; }

set { _number = value; }

}

private float _Cres;

public float Cres

{

get { return _Cres; }

set { _Cres = value; }

}

private float _ely;

public float Ely

{

get { return _ely; }

set { _ely = value; }

}

private float _math;

public float Math

{

get { return _math; }

set { _math = value; }

}

private float _avg;

public float Avg

{

get { return _avg; }

set { _avg = value; }

}

#endregion

}

#endregion

#region 全体学生类

public class AllStudent

{

#region 构造函数

public AllStudent(int cout)

{

_cout = cout;

_stuList = new List();

}

#endregion

#region 字段和属性

private int _cout;

public int Cout

{

get { return _cout; }

set { _cout = value; }

}

private List _stuList;

public List StuList

{

get { return _stuList; }

set { _stuList = value; }

}

#endregion

#region 学生成绩录入方法

public void AddAllreslut()

{

for (int i = 0; i < _cout; i++)

{

string[] strs = new string[5];

Console.WriteLine("请输入学生姓名:");

strs[0] = Console.ReadLine();

Console.WriteLine("请输入学生学号:");

strs[1] = Console.ReadLine();

Console.WriteLine("请输入学生C++成绩:");

strs[2] = Console.ReadLine();

if (!Isfloat(strs[2]))

{

Console.WriteLine("请输入正确的成绩:");

strs[2] = Console.ReadLine();

}

else

{

if (float.Parse(strs[2]) > 100)

{

Console.WriteLine("请输入正确的成绩:");

strs[2] = Console.ReadLine();

}

}

Console.WriteLine("请输入学生英语成绩:");

strs[3] = Console.ReadLine();

if (!Isfloat(strs[3]))

{

Console.WriteLine("请输入正确的成绩:");

strs[3] = Console.ReadLine();

}

else

{

if (float.Parse(strs[3]) > 100)

{

Console.WriteLine("请输入正确的成绩:");

strs[3] = Console.ReadLine();

}

}

Console.WriteLine("请输入学生数学成绩:");

strs[4] = Console.ReadLine();

if (!Isfloat(strs[4]))

{

Console.WriteLine("请输入正确的成绩:");

strs[4] = Console.ReadLine();

}

else

{

if (float.Parse(strs[4]) > 100)

{

Console.WriteLine("请输入正确的成绩:");

strs[4] = Console.ReadLine();

}

}

Students student = new Students(strs[0], strs[1], float.Parse(strs[2]), float.Parse(strs[3]), float.Parse(strs[4]));

Console.WriteLine(strs[0] + "同学的平均成绩为:" + student.Avg);

Console.WriteLine();

_stuList.Add(student);

}

}

#endregion

#region 按学号查询平均成绩

public void RtAvg()

{

Console.WriteLine("请输入要查询平均成绩学生的学号:");

string number = Console.ReadLine();

float avg = RtAvg(number);

if (avg != 0)

{

Console.WriteLine(number + "的平均成绩为:" + avg);

Console.ReadKey();

}

else

{

Console.WriteLine("没有该学号学生成绩!");

Console.ReadKey();

}

}

public float RtAvg(string number)

{

for (int i = 0; i < _stuList.Count; i++)

{

if (_stuList[i].Number.Trim() == number.Trim())

{

return _stuList[i].Avg;

}

}

return 0;

}

#endregion

#region 按平均分排序方法

public void sorting()

{

List list = new List();

for (int i = 0; i < _stuList.Count; i++)

{

Students stus = new Students();

for (int j = 0; j < _stuList.Count - i - 1; j++)

{

if (_stuList[j].Avg > _stuList[j + 1].Avg)

{

stus = _stuList[j];

_stuList[j] = _stuList[j + 1];

_stuList[j + 1] = stus;

}

else

{

stus = _stuList[j + 1];

}

}

if (i == _stuList.Count - 1)

{

stus = _stuList[0];

}

list.Add(stus);

}

_stuList = list;

}

#endregion

#region 输出所有学生成绩

public void printStu()

{

Console.WriteLine("一下是所有学生信息:");

for (int i = 0; i < _stuList.Count; i++)

{

Console.WriteLine(_stuList[i].Name + "同学的基本信息:");

Console.WriteLine("学号:" + _stuList[i].Number + " C++成绩为:" + _stuList[i].Cres + " 英语成绩为:" + _stuList[i].Ely + " 数学成绩为:" + _stuList[i].Math+" 平均成绩

为:"+_stuList[i].Avg);

Console.WriteLine();

}

Console.ReadKey();

}

#endregion

#region 验证浮点数方法

public static bool Isfloat(string Input)

{

if (Input == null)

{

return false;

}

else

{

string pattern = "^(\\d*\\.)?\\d+$";

if (Regex.Match(Input, pattern, https://www.wendangku.net/doc/3610648917.html,piled).Success)

{

return true;

}

else

{

return false;

}

}

}

#endregion

}

#endregion

3.定义一个描述学生基本情况的类,数据成员包括姓名,学号,C++,英语和数学成绩;成员函数包括输出数据,姓名和学号,3门课的成绩,求出总成绩和平均成绩。

class student

{

public string name;

public int num;

public float c, e, m, ave;

public student()

{

Console.Write("请输入name:");

name = Console.ReadLine();

Console.Write("请输入num:");

num = int.Parse(Console.ReadLine());

Console.Write("请输入C++成绩:");

c = float.Parse(Console.ReadLine());

Console.Write("请输入English成绩:");

e = float.Parse(Console.ReadLine());

Console.Write("请输入Math成绩:");

m = float.Parse(Console.ReadLine());

}

public void print()

{

Console.WriteLine("name={0},num={1},C++成绩={2},English成绩={3},Math成绩={4}", name, num, c, e, m);

}

public void getSumAve(bool print)

{

float sum = c + e + m;

ave = sum / 3;

if (print)

{

Console.WriteLine("总成绩={0},平均成绩={1}", sum, ave);

}

}

}

class theclass

{

student[] students;

int count;

public theclass()

{

Console.WriteLine("学生数:");

count = int.Parse(Console.ReadLine());

students = new student[count];

for (int i = 0; i < students.Length; i++)

{

if (i == count)

break;

students[i] = new student();

students[i].getSumAve(false);

}

}

public void paixu()

{

student t;

for (int i = 0; i < students.Length; i++)

{

for (int j = 0; j < students.Length - i - 1; j++)

if (students[i].ave > students[i + 1].ave)

{

t = students[i];

students[i] = students[i + 1];

students[i + 1] = t;

}

}

}

public void shuchu()

{

for (int i = 0; i < students.Length; i++)

{

Console.WriteLine("姓名:{0},学号:{1},c语言:{2},数学:{3},英语:{4}", students[i].name, students[i].num, students[i].c, students[i].m, students[i].e);

}

}

}

class program

{

static void Main(string[] args)

{

theclass m = new theclass();

m.paixu();

m.shuchu();

Console.Read();

}

}

4.设有一个描述坐标点的CPoint类,其私有变量x和y代表一个点的x,y坐标值。编写程序实现以下功能:利用构造函数传递参数,并设其默认参数值为60和75,利用成员函数display()输出这一默认的值;利用公有成员函数setpoint ()将坐标值的修改为(80,150),并利用成员函数输出修改后的坐标值。using System;

using System.Collections.Generic;

public class MyClass

{

public static void Main()

{

CPoint cp=new CPoint();

cp.Display();

cp.SetPoint(80,150);

cp.Display();

Console.ReadLine();

}

}

public class CPoint

{

private int x;

private int y;

public CPoint():this(60,75)

{

}

public CPoint(int x,int y)

{

this.x=x;

this.y=y;

}

public void Display()

{

Console.WriteLine("x={0},y={1}",x,y);

}

public void SetPoint(int x,int y)

{

this.x=x;

this.y=y;

}

}

5.定义一个人员类CPerson,包括数据成员:姓名,编号,性别和用于输入输出的成员函数。在此基础上派生出学生类CStudent(增加成绩)和教师类C Teacher (增加教龄),并实现对学生和教师信息的输入输出。

class CPerson

{

string name;

int num;

string sex;

public void input()

{

Console.WriteLine("姓名:");

https://www.wendangku.net/doc/3610648917.html, = Console.ReadLine();

Console.WriteLine("编号:");

this.num = int.Parse(Console.ReadLine());

Console.WriteLine("性别:");

this.sex = Console.ReadLine();

}

public void print()

{

Console.WriteLine("姓名:" + name);

Console.WriteLine("编号:" + num);

Console.WriteLine("性别:" + sex);

}

class CStudent : CPerson

{

float score;

public void input1()

{

Console.WriteLine("这是学生类");

base.input();

Console.WriteLine("成绩:");

this.score = float.Parse(Console.ReadLine());

}

public void print1()

{

base.print();

Console.WriteLine("成绩:" + score);

}

}

class CTeacher : CPerson

{

public int age;

public void input2()

{

Console.WriteLine("这是教师类");

base.input();

Console.WriteLine("教龄:");

this.age = int.Parse(Console.ReadLine());

}

public void print2()

{

base.print();

Console.WriteLine("教龄:" + age);

}

}

class Program

{

static void Main(string[] args)

{

CStudent stu = new CStudent();

CTeacher teac = new CTeacher();

stu.input1();

stu.print1();

teac.input2();

teac.print2();

Console.ReadLine();

}

}

}

6.把定义平面直角坐标系上的一个点的类CPoint作为基类,派生出描述一条直线的类Cline,再派生出一个矩形类CRect。要求成员函数能求出两点间的距离,举行的周长和面积等。设计一个测试程序,并构造完整的程序。

class cpoint

{

public float x, y;

public cpoint(float x, float y)

{

this.x = x;

this.y = y;

}

}

class cline

{

cpoint p1, p2;

public cline(cpoint p1, cpoint p2)

{

this.p1 = p1;

this.p2 = p2;

}

public float getLength()

{

float len = (float)Math.Sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));

return len;

}

}

class crect

{

cline l1, l2;

public crect(cline l1, cline l2)

{

this.l1 = l1;

this.l2 = l2;

}

public float getArea()

{

return l1.getLength() * l2.getLength();

}

}

class Program

{

static void Main(string[] args)

{

crect rect1 = new crect(new cline(new cpoint(0, 0), new cpoint(2, 0)), new cline(new cpoint(0, 0), new cpoint(0, 1)));

Console.WriteLine(rect1.getArea());

Console.Read();

}

}

7.定义一个字符串类CStrOne,包含一个存放字符串的数据成员,能够通过构造函数初始化字符串,通过成员函数显示字符串的内容,在此基础上派生出CStrTwo类,增加一个存放字符串的数据成员,并能通过派生类的构造函数传递参数,初始化两个字符串,通过成员函数进行两个字符串的合并以及输出。using System;

using System.Text;

using System.IO;

using System.Text.RegularExpressions;

namespace BaiduTest

{

public class CStrOne

{

protected string m_str1 = string.Empty;

public CStrOne(string str1)

{

this.m_str1 = str1;

}

public void ShowStringOne()

{

Console.WriteLine(m_str1);

}

}

// 继承CStrOne

public class CStrTwo : CStrOne

{

private string m_str2 = string.Empty;

public CStrTwo(string str, string str2) : base(str)

{

this.m_str2 = str2;

}

public void ShowStringTwo()

{

Console.WriteLine("{0}{1}", m_str1, m_str2);

}

}

class Program

{

static void Main(string[] args)

{

CStrOne cs1 = new CStrOne("i love ");

cs1.ShowStringOne();

CStrTwo cs2 = new CStrTwo("i love ","this game.");

cs2.ShowStringTwo();

Console.ReadKey();

}

}

}

相关文档