云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > C#习题集

C#习题集

  • 62 次阅读
  • 3 次下载
  • 2025/5/3 18:56:17

答:“void A(){ }”应改为“A(){}”,因为构造函数不能带任何返回类型修饰符。“private A(int x){ }”和“private A(int y){ }”实际上是同一个构造函数,应删除其中一个。 4. class A

{ int f(){ return 1; } void f(){ } void g(int x){ } void g(int y){ } }

答:类A中试图重载函数f()和函数g(),但函数的重载是通过定义不同的参数个数和类型来实现的,其返回类型不能说明函数的重载特性,因此应删除其中的一个f()函数;类似的原因,函数g()中的形参名不同也不能说明函数的重载特性,实际上这两个g()函数是一样的,因此也需去掉其中之一。 四.问答题

1.简述类和对象的关系。 2.简述面向对象的基本特征。

3.类的声明格式中包含哪些部分?各有什么意义? 4.什么是实例方法?什么是静态方法?

5.类可以使用哪些修饰符?各代表什么含义? 6.简述构造函数和析构函数的作用。 7.什么是属性?

答:属性是C#中的一种语法结构。如下: private int _年龄; public int年龄 { set { if (value > 0){ _年龄= value;} } get{ return _年龄; } } 8.为什么使用属性?

答:属性是实现封装的一种方式,起到保护类成员变量的作用。调用简洁方便。而且,通过属性可以访问私有成员变量。

9.属性的类型有什么?

答:属性类型有:读写属性,只读属性,只写属性。 11.什么是索引器?

答:索引器是C#中的一种语法结构。如下: [访问修饰符] 数据类型this[数据类型 标识符] { get{}; //get访问器 set{}; //set访问器 } 12. 索引器的作用是什么?

答:作用:索引器是实现封装的一种方式,起到保护类成员的作用;同时使用起来简单方便:为集合对象创建索引器后,可以对类对象指定索引来直接访问数组元素即用索引数组的方式索引对象。

13. 索引器可以重载吗?可以为索引器指定几个索引器参数?

答:索引器可以重载。可以为索引器至少指定一个索引器参数。 14.什么时候使用属性,什么时候使用索引器?

答:类中有成员变量就应该有属性。当成员变量是集合类型的时候,可以创建索引器。

- 24 -

五.程序阅读题

1.阅读下列程序,写出程序的输出结果。 考察类对象 public class TPoint { int x, y; public void setpoint(int x1,int y1) { x = x1; y = y1; } public void dispoint()

{ Console.WriteLine(\点:({0},{1})\ } }

class Program

{ static void Main(string[] args)

{ TPoint p1, p2; p1= new TPoint(); p1.setpoint(2, 6); p2 = p1; p2.setpoint(8, 3);

p1.dispoint(); p2.dispoint(); } }

点:(8,3) 点:(8,3) 2.阅读下列程序,写出程序的输出结果。考察方法参数传递 class Program

{ static void f1(ref int a, ref int b, out int sum, out int sub) { int t; t=a; a=b; b=t;

sum = a + b; sub = a - b; }

public static void Main(string[] args) { int x=10, y=20,xx=30,yy=40; f1(ref x, ref y, out xx, out yy); Console.WriteLine(\ } }

x=20,y=10,xx=30,yy=10 3.阅读下列程序,写出程序的输出结果。考察索引器 public class Weeks

{ string[] week= { \ public string this[int i]

{ get

{ if (i >= 0 && i< 7) return week[i]; else return week[0]; } set

{ if (i >= 0 && i< 7) week[i] = value;

- 25 -

} } }

class Program

{ static void Main()

{ Weeks w = new Weeks();

w[0] = \星期天\ w[1] = \星期一\

Console.WriteLine(w[2]); Console.WriteLine(w[7]); } } Tues 星期天 4.阅读下列程序,写出程序的输出结果。考察委托使用 delegate void mydelegate(double x, double y); class MyDeClass

{ public void add(double x, double y)

{ Console.WriteLine(\ public void sub(double x, double y)

{ Console.WriteLine(\ }

class Program

{ static void Main(string[] args)

{ MyDeClass obj = new MyDeClass(); mydelegate p;

p = obj.add; p += obj.sub; p(10, 20); } } 10+20=30 10-20=-10 5.阅读下列程序,写出程序的输出结果。考察委托使用 public delegate int mydelegate(int x, int y); class Math

{ public int fun1(int x, int y) { return x * x + y * y; } public int fun2(int x, int y) { return x * x - y * y; } }

class Program

{ static void Main(string[] args) { int result;

mydelegate objCall;

- 26 -

} } Math objMath = new Math(); objCall = objMath.fun1; result = objCall(5, 3); Console.WriteLine(\结果为{0}\ objCall = objMath.fun2; result = objCall(5, 3); Console.WriteLine(\结果为{0}\ Console.Read(); } }

结果为34 结果为16 5. 阅读下列程序,写出程序的输出结果。考察事件 public delegate void mydelegate1(int i);

class MyEvent { public event mydelegate1 Event1; public void FireEvent1(int i)

{ if (Event1 != null) Event1(i); } }

public class Program { public static void show(int i)

{ Console.Write(\ } public static void Main()

{ MyEvent a = new MyEvent();

a.Event1 += new mydelegate1(show); for (int i = 0; i <= 30; i++)

if (i % 7 == 0) a.FireEvent1(i); } }

0 7 14 21 28 6. 阅读下列程序,写出程序的输出结果。考察运算符重载 class Complex { double a, b;

public Complex(double m, double n) { a=m; b=n; }

public static Complex operator +(Complex x,Complex y) { Complex t=new Complex(0,0); t.a=x.a+y.a; t.b=x.b+y.b; return(t); }

public void DispCom() { if (b>0 ) Console.WriteLine(\ else Console.WriteLine(\ }

- 27 -

搜索更多关于: C#习题集 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

答:“void A(){ }”应改为“A(){}”,因为构造函数不能带任何返回类型修饰符。“private A(int x){ }”和“private A(int y){ }”实际上是同一个构造函数,应删除其中一个。 4. class A { int f(){ return 1; } void f(){ } void g(int x){ } void g(int y){ } } 答:类A中试图重载函数f()和函数g(),但函数的重载是通过定义不同的参数个数和类型来实现的,其返回类型不能说明函数的重载特性,因此应删除其中的一个f()函数;类似的原因,函数g()中的形参名不同也不能说明函数的重载特性,实际上这两个g()函数是一样的,因此也需去掉其中之一。 四.问答题 1.简述类和对象的关系。 2.简述面向对象的基本特征。

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com