当前位置:首页 > 第6章 面向对象程序设计 习题
成员定义列表; }
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.什么时候使用属性,什么时候使用索引器?
答:类中有成员变量就应该有属性。当成员变量是集合类型的时候,可以创建索引器。 五.程序阅读题
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; } } }
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;
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(\ } }
class Program
共分享92篇相关文档