第3章面向对象编程基础.docx

上传人:b****1 文档编号:2727223 上传时间:2023-05-04 格式:DOCX 页数:28 大小:20.96KB
下载 相关 举报
第3章面向对象编程基础.docx_第1页
第1页 / 共28页
第3章面向对象编程基础.docx_第2页
第2页 / 共28页
第3章面向对象编程基础.docx_第3页
第3页 / 共28页
第3章面向对象编程基础.docx_第4页
第4页 / 共28页
第3章面向对象编程基础.docx_第5页
第5页 / 共28页
第3章面向对象编程基础.docx_第6页
第6页 / 共28页
第3章面向对象编程基础.docx_第7页
第7页 / 共28页
第3章面向对象编程基础.docx_第8页
第8页 / 共28页
第3章面向对象编程基础.docx_第9页
第9页 / 共28页
第3章面向对象编程基础.docx_第10页
第10页 / 共28页
第3章面向对象编程基础.docx_第11页
第11页 / 共28页
第3章面向对象编程基础.docx_第12页
第12页 / 共28页
第3章面向对象编程基础.docx_第13页
第13页 / 共28页
第3章面向对象编程基础.docx_第14页
第14页 / 共28页
第3章面向对象编程基础.docx_第15页
第15页 / 共28页
第3章面向对象编程基础.docx_第16页
第16页 / 共28页
第3章面向对象编程基础.docx_第17页
第17页 / 共28页
第3章面向对象编程基础.docx_第18页
第18页 / 共28页
第3章面向对象编程基础.docx_第19页
第19页 / 共28页
第3章面向对象编程基础.docx_第20页
第20页 / 共28页
亲,该文档总共28页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

第3章面向对象编程基础.docx

《第3章面向对象编程基础.docx》由会员分享,可在线阅读,更多相关《第3章面向对象编程基础.docx(28页珍藏版)》请在冰点文库上搜索。

第3章面向对象编程基础.docx

第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.假设classMclass类的一个方法的签名为:

publicvoidMax,(outintmax,paramsint[]a),m1是Mclass类的一个对象,maaxval是一个int型的值类型变量,arrayA是一个int型的数组对象,则下列调用该方法有错的是()。

A.m1.Max(outmaxval);B.m1.Max(outmaxval,4,5,3,);

C.m1.Max(outmaxval,refarrayA);D.m1.Max(outmaxval,3,3.5);

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

A.要求与字段域一一对应B.只包含get访问器的属性是只写属性

C.不能把它当变量使用D.在静态属性访问器中可访问静态数据

二.填空题

1.构析函数不能由程序显示地调用,而是由系统在(释放对象)时自动调用。

如果这个对象是一个派生类对象,那么在调用构析函数时,除了执行派生类的构析函数,也会执行基类的构析函数,其执行顺序与构析函数(正好相反)。

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

3.在类中如果一个数据成员被声明为static的,则说明这个类的所有实例都共享这个static数据成员。

在类体外,static成员不能通过(继承)来访问,它必须通过(静态方法(构造函数)方法)来访问。

4.程序运行结果()

usingSystem;

publicclassTest

{

Publicvoidchangel(strings){

s=s+“Changel”;

}

publicvoidchange2(refstrings){

s=s+“Change2”;

}

publicvoidchange3(strings1,outstrings2){

s1=s1+“Change3”;

s2=s1;

}

}

publicclassExe8

{

publicstaticvoidMain(){

strings1,s2;

s1=“Hello,”;

Testt1=newTest();

t1.changel(s1);

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

t1.change2(refs1);

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

t1.chnage3(s1,outs2);

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

Console.WriteLine(“s2aftercalltochange3is{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)

usingSystem;

publicclassTest

{

publicvoidchange(strings){

s=s+“Change1”;

}

publicvoidchange(refstrings){

s=s+“Change2”;

}

publicvoidchange(strings1,outstrings2){

s1=s1+“Change3”;

s2=s1;

}

}

publicclassExe9

{

publicstaticvoidMain(){

strings1,s2;

s1=“Hello,”;

Testt1=newTest();

t1.change(s1);

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

t1.change(refs1);

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

t1.change(s1,outs2);

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

Console.Read();

}

}

三.编程题

1.定义描述复数的类,并实现复数的输入和输出。

设计三个方法分别完成复数的加法,减法和乘法运算。

解:

UsingSystem;

UsingSystemCollectionsGeneric;

UsingSystemText;

Namespace

{

staticvoidmain(string[]args)

{complexa=newcomplex(2,5);

complexb=newcomplex(4,6);

complexc=a+b;

c.print();complexd=a-b;

d.print();complexm=a+b;

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

}

}

Classcomplex

{

Doubler,v;

Publiccomplex(doubler,doublev)

{thisr=r;thisv=v;

Publiccomplex{}

Publicstaticcomplexoperator+(complexa,complexb)

{returnnewcomplex(a·r+b·r,a·r+b·v);}

Publicstaticcomplexoperator-(complexa,complexb)

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

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

Returnnewcomplex(y,k);}

Publicvoidprint()

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

}

}

}

2.定义全班学生成绩类,包括:

姓名,学号,C++成绩,英语成绩,数学成绩和平均成绩。

设计4个方法:

(1)全班成绩的输入;

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

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

(4)输出全班成绩。

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Text.RegularExpressions;

staticvoidMain(string[]args)

{

#regionStudentText

AllStudentall=newAllStudent(3);

all.AddAllreslut();

all.RtAvg();

all.printStu();

all.sorting();

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

");

all.printStu();

#endregion

}

#regionStudent类

publicclassStudents

{

#region构造函数

publicStudents()

{

}

publicStudents(stringname,stringnumber,floatCres,floatElys,floatmaths)

{

_name=name;

_number=number;

_Cres=Cres;

_ely=Elys;

_math=maths;

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

}

#endregion

#region字段

privatestring_name;

publicstringName

{

get{return_name;}

set{_name=value;}

}

privatestring_number;

publicstringNumber

{

get{return_number;}

set{_number=value;}

}

privatefloat_Cres;

publicfloatCres

{

get{return_Cres;}

set{_Cres=value;}

}

privatefloat_ely;

publicfloatEly

{

get{return_ely;}

set{_ely=value;}

}

privatefloat_math;

publicfloatMath

{

get{return_math;}

set{_math=value;}

}

privatefloat_avg;

publicfloatAvg

{

get{return_avg;}

set{_avg=value;}

}

#endregion

}

#endregion

#region全体学生类

publicclassAllStudent

{

#region构造函数

publicAllStudent(intcout)

{

_cout=cout;

_stuList=newList();

}

#endregion

#region字段和属性

privateint_cout;

publicintCout

{

get{return_cout;}

set{_cout=value;}

}

privateList_stuList;

publicListStuList

{

get{return_stuList;}

set{_stuList=value;}

}

#endregion

#region学生成绩录入方法

publicvoidAddAllreslut()

{

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

{

string[]strs=newstring[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();

}

}

Studentsstudent=newStudents(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按学号查询平均成绩

publicvoidRtAvg()

{

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

");

stringnumber=Console.ReadLine();

floatavg=RtAvg(number);

if(avg!

=0)

{

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

"+avg);

Console.ReadKey();

}

else

{

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

");

Console.ReadKey();

}

}

publicfloatRtAvg(stringnumber)

{

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

{

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

{

return_stuList[i].Avg;

}

}

return0;

}

#endregion

#region按平均分排序方法

publicvoidsorting()

{

Listlist=newList();

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

{

Studentsstus=newStudents();

for(intj=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输出所有学生成绩

publicvoidprintStu()

{

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

");

for(inti=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验证浮点数方法

publicstaticboolIsfloat(stringInput)

{

if(Input==null)

{

returnfalse;

}

else

{

stringpattern="^(\\d*\\.)?

\\d+$";

if(Regex.Match(Input,pattern,RegexOptions.Compiled).Success)

{

returntrue;

}

else

{

returnfalse;

}

}

}

#endregion

}

#endregion

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

classstudent

{

publicstringname;

publicintnum;

publicfloatc,e,m,ave;

publicstudent()

{

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());

}

publicvoidprint()

{

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

}

publicvoidgetSumAve(boolprint)

{

floatsum=c+e+m;

ave=sum/3;

if(print)

{

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

}

}

}

classtheclass

{

student[]students;

intcount;

publictheclass()

{

Console.WriteLine("学生数:

");

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

students=newstudent[count];

for(inti=0;i

{

if(i==count)

break;

students[i]=newstudent();

students[i].getSumAve(false);

}

}

publicvoidpaixu()

{

studentt;

for(inti=0;i

{

for(intj=0;j

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

{

t=students[i];

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

students[i+1]=t;

}

}

}

publicvoidshuchu()

{

for(inti=

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2