第一范文网 - 专业文章范例文档资料分享平台

C#习题参考答案 《c#面向对象程序设计》 郑宇军

来源:用户分享 时间:2025/7/20 17:39:37 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

3-12

(1)

class Program {

static void Main() {

int sum = 0;

for (int i = 1; i <= 50; i++) {

sum += 2*i; }

Console.WriteLine(sum); Console.ReadLine(); } } (2)

class Program {

static void Main() {

int sum = 0, m = 1;

for (int i = 1; i <= 10; i++) {

m = m * 2;

sum = sum + m; }

Console.WriteLine(sum); Console.ReadLine(); } }

3-13 略

Chapter 4

4_1 静态字段是指某个类型的所有对象所共享的字段,程序会在首次该类型时为字段分配存储空间,所有对象都可以通过静态成员来读取或修改静态字段。常量字段是特殊的静态字段,它使用关键字const来修饰,且只能在声明时赋值,之后不允许被修改。

只读字段则是特殊的实例字段,它属于单个对象所有,在声明或构造函数中赋值,而在创建对象之后不允许被修改。

只读属性是指在属性中只定义get访问函数而不定义set访问函数,那么使用者只能访问该属性,而不能对属性赋值。

4_2 提示:在析构函数中将静态字段object_count的值减1 4_3 使用属性更为安全(可以在访问函数中增加控制代码),访问更加灵活(可以在访问函数中执行各种计算),可扩展性也更好(修改属性访问代码时不必修改字段名和属性名,这就不会影响到其它对象和方法的使用)。 4_4

public class ComplexNumber {

private double x, y ;

public double X //实数部分 {

get { return x; } set { x = value; }

}

public double Y; //虚数部分 {

get { return y; } set { y = value; } }

public ComplexNumber(double x, double y) {

this.x = x; this.y = y; }

public static ComplexNumber operator+(ComplexNumber c1, ComplexNumber c2) {

return new ComplexNumber(c1.x + c2.x, c1.y + c2.y); }

public static ComplexNumber operator-(ComplexNumber c1, ComplexNumber c2) {

return new ComplexNumber(c1.x - c2.x, c1.y - c2.y); }

public static bool operator ==(ComplexNumber c1, ComplexNumber c2) {

return (c1.x == c2.x) && (c1.y == c2.y); }

public static bool operator !=(ComplexNumber c1, ComplexNumber c2) {

return (c1.x != c2.x) || (c1.y != c2.y); }

public static bool operator >(ComplexNumber c1, ComplexNumber c2) {

double a = c1.x*c1.x + c1.y*c1.y; double b = c2.x*c2.x + c2.y*c2.y; return a > b; }

public static bool operator >=(ComplexNumber c1, ComplexNumber c2) { return a == b || a > b; }

public static bool operator <(ComplexNumber c1, ComplexNumber c2) {

return !(c1 >= c2); }

public static bool operator <=(ComplexNumber c1, ComplexNumber c2) {

return !(c1 > c2); } }

}

4_5 不能,因为this表示当前对象,而静态成员是通过类型本身而非对象来访问的。

4_6 如果字符串格式错误的几率很小,使用Parse方法具有更高的效率;否则应选用TryParse方法来提高程序的可靠性。 4_7 略 4_8

public int GetAge(DateTime birthday) {

int y = DateTime.Year, m = DateTime.Month, d = DateTime.Day; if (d < birthday.Day) m--;

if (m < birthday.Month) y--;

return y – birthday.Year; }

4_9 略

4_10 一个省份中的城市、一个城市中的景点一般变化较少,适合用数组存储。而旅游线路和方案要便于修改。

Chapter 5

5_1 派生类自动继承基类的公有成员和保护成员,但不能访问基类的私有成员;外部对象只能访问其中的公有成员,而不能访问保护成员和私有成员。

5_2 构造函数和析构函数不能重载和隐藏。除非是默认构造函数,否则派生类的构造函数须指明要调用的基类构造函数形式。 重载或隐藏属性时要分别对get和set访问函数进行重载或隐藏 重载或隐藏索引函数时与属性类似 5_3 略

5_4 (1)Animal中的抽象方法不能有执行代码 (2)派生类Bird不能只重载基类Animal中Life属性的get访问函数 (3)派生类Mammal的life字段不要new关键字来修饰,因为它不能访问基类中的私有字段。 5_5

public abstract class Disk {

protected double total, free; public float Total //总容量 { get { return total; } set { total = value; } } public float Free //剩余容量 {

get { return free; } }

public virtual void Write(float size) {

if (free >= size) free -= size; else

Console.WriteLine(\剩余空间不足!\ }

public virtual void Delete(float size) {

if (free + size <= total) free += size; } }

public class HardDisk {

public HardDisk(float size) {

this.total = this.free = size; } }

public class FlashDisk {

public FlashDisk() {

this.total = this.free = 64000; } }

public class CDROM {

public override void Delete(float size) {

Console.WriteLine(\光盘不可删除!\ } }

5_6

// 抽象类:图形Shape public abstract class Shape {

public abstract double Perimeter { get; } public abstract double Area { get; } }

// 派生类: 圆形Circle

public class Circle : Shape { private double r; public double R; {

get { return r; } set { r = value; } }

public override double Perimeter {

get { return 6.28 * r; } }

public override double Area

C#习题参考答案 《c#面向对象程序设计》 郑宇军.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c7588c1sm7a9kfa251dxp_2.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top