程序设计Csharp程序设计 (79).pdf

上传人:刘静 文档编号:52870162 上传时间:2022-10-24 格式:PDF 页数:23 大小:1.07MB
返回 下载 相关 举报
程序设计Csharp程序设计 (79).pdf_第1页
第1页 / 共23页
程序设计Csharp程序设计 (79).pdf_第2页
第2页 / 共23页
点击查看更多>>
资源描述

《程序设计Csharp程序设计 (79).pdf》由会员分享,可在线阅读,更多相关《程序设计Csharp程序设计 (79).pdf(23页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、C#程序设计 Programming in C#对象的高级应用 1、对象与数组 C#程序设计程序设计 2、对象与方法 7.3.3 使用对象 3 3对象与数组 在定义类时,类的字段可以为数组类型。7.3.3 使用对象 4 usingusing SystemSystem;classclass PointPoint public doublepublic double temptemp=newnew doubledouble 3 3;classclass TestTest static voidstatic void MainMain()()Point p1Point p1=newnew Point

2、Point();();ConsoleConsole.WriteLineWriteLine(00,11,22,p1,p1.temptemp 0 0,p1p1.temptemp 1 1,p1,p1.temptemp 2 2););p1p1.temptemp 0 0=3 3;p1p1.temptemp 1 1=4 4;p1p1.temptemp 2 2=5 5;ConsoleConsole.WriteLineWriteLine(00,11,22,p1,p1.temptemp 0 0,p1p1.temptemp 1 1,p1,p1.temptemp 2 2););7.3.3 使用对象 5 usingu

3、sing SystemSystem;classclass PointPoint public doublepublic double temptemp=newnew doubledouble 3 3;classclass TestTest static voidstatic void MainMain()()Point p1Point p1=newnew PointPoint();();ConsoleConsole.WriteLineWriteLine(00,11,22,p1,p1.temptemp 0 0,p1p1.temptemp 1 1,p1,p1.temptemp 2 2););p1p

4、1.temptemp 0 0=3 3;p1p1.temptemp 1 1=4 4;p1p1.temptemp 2 2=5 5;ConsoleConsole.WriteLineWriteLine(00,11,22,p1,p1.temptemp 0 0,p1p1.temptemp 1 1,p1,p1.temptemp 2 2););7.3.3 使用对象 6 usingusing SystemSystem;classclass PointPoint public doublepublic double temptemp=newnew doubledouble 3 3;classclass TestT

5、est static voidstatic void MainMain()()Point p1Point p1=newnew PointPoint();();ConsoleConsole.WriteLineWriteLine(00,11,22,p1,p1.temptemp 0 0,p1p1.temptemp 1 1,p1,p1.temptemp 2 2););p1p1.temptemp 0 0=3 3;p1p1.temptemp 1 1=4 4;p1p1.temptemp 2 2=5 5;ConsoleConsole.WriteLineWriteLine(00,11,22,p1,p1.temp

6、temp 0 0,p1p1.temptemp 1 1,p1,p1.temptemp 2 2););7.3.3 使用对象 7 将具有相同类类型的对象有序地集合在一起可以构成对象数组。一维对象数组的定义形式为:类名类名 对象数组名对象数组名=newnew 类名类名 数组长度数组长度;7.3.3 使用对象 8 关于对象数组有如下说明:如果对象数组所属类具有带参数的构造函数时,可以对对象数组进行初始化。例如:PointPoint p1p1 =newnew PointPoint 3 3newnew PointPoint(1 1,1 1),),newnew PointPoint(2 2,2 2),),ne

7、wnew PointPoint(3 3,3 3););7.3.3 使用对象 9 对象数组中每个元素是一个对象,可以按照使用对象的方法对之进行操作。例如:PointPoint p1p1 =newnew PointPoint 3 3newnew PointPoint(1 1,1 1),),newnew PointPoint(2 2,2 2),),newnew PointPoint(3 3,3 3););p1p1 0 0.PrintPrint();();7.3.3 使用对象 10 4对象与方法 对象可以作为方法的参数与返回值。当方法的参数和返回值为类类型时,与其他基类型作为方法的参数和返回值的语法类

8、似。7.3.3 使用对象 11 usingusing SystemSystem;classclass PointPoint public doublepublic double temptemp =newnew doubledouble 2 2;publicpublic PointPoint(doubledouble x,x,doubledouble y y)temptemp 0 0=x x;temptemp 1 1=y y;classclass TestTest static doublestatic double distancedistance(Point p,Point qPoint

9、p,Point q)returnreturn MathMath.SqrtSqrt(p p.temptemp 0 0 -q q.temptemp 0 0)*)*(p p.temptemp 0 0 -q q.temptemp 0 0)+()+(p p.temptemp 1 1 -q q.temptemp 1 1)*)*(p p.temptemp 1 1 -q q.temptemp 1 1););7.3.3 使用对象 12 static voidstatic void MainMain()()Point p1Point p1=newnew PointPoint(0 0,0 0););Point p2

10、Point p2 =newnew PointPoint(1 1,0 0););ConsoleConsole.WriteLineWriteLine(The distance between p1 and p2(The distance between p1 and p2 is:is:00,distancedistance(p1,p2p1,p2););7.3.3 使用对象 13 对象除了可以作为值参数之外,还可以作为方法的引用参数和输出参数。usingusing SystemSystem;classclass PointPoint public doublepublic double x,yx,y

11、;publicpublic PointPoint(doubledouble xx,xx,doubledouble yyyy)x x =xxxx;y y =yyyy;classclass TestTest 7.3.3 使用对象 14 static voidstatic void distancedistance(Point p1,Point p1,refref Point p2,Point p2,outout Point p3Point p3)doubledouble d1,d2d1,d2;d1d1 =MathMath.SqrtSqrt(p1p1.x x *p1p1.x x +p1p1.y y

12、*p1p1.y y););d2d2 =MathMath.SqrtSqrt(p2p2.x x *p2p2.x x +p2p2.y y *p2p2.y y););p3p3 =d1d1 d2d2?p1p1 :p2p2;p1p1.x x =0 0;p1p1.y y =0 0;p2p2.x x =0 0;p2p2.y y =0 0;static voidstatic void MainMain()()Point p1Point p1=newnew PointPoint(3 3,4 4););Point p2Point p2 =newnew PointPoint(2 2,5 5););Point p3Po

13、int p3=newnew PointPoint(0 0,0 0););7.3.3 使用对象 15 static voidstatic void distancedistance(Point p1,Point p1,refref Point p2,Point p2,outout Point p3Point p3)doubledouble d1,d2d1,d2;d1d1 =MathMath.SqrtSqrt(p1p1.x x *p1p1.x x +p1p1.y y *p1p1.y y););d2d2 =MathMath.SqrtSqrt(p2p2.x x *p2p2.x x +p2p2.y y

14、*p2p2.y y););p3p3 =d1d1 d2d2?p1p1 :p2p2;p1p1.x x =0 0;p1p1.y y =0 0;p2p2.x x =1 1;p2p2.y y =1 1;static voidstatic void MainMain()()Point p1Point p1=newnew PointPoint(3 3,4 4););Point p2Point p2 =newnew PointPoint(2 2,5 5););Point p3Point p3=newnew PointPoint(0 0,0 0););7.3.3 使用对象 16 ConsoleConsole.W

15、riteLineWriteLine(p1:(p1:00,11,p1,p1.x,p1x,p1.y y););ConsoleConsole.WriteLineWriteLine(p2:(p2:00,11,p2,p2.x,p2x,p2.y y););ConsoleConsole.WriteLineWriteLine(p3:(p3:00,11,p3,p3.x,p3x,p3.y y););distancedistance(p1,p1,refref p2,p2,outout p3p3););ConsoleConsole.WriteLineWriteLine(-p1:p1:00,11,p1,p1.x,p1x

16、,p1.y y););ConsoleConsole.WriteLineWriteLine(-p2:p2:00,11,p2,p2.x,p2x,p2.y y););ConsoleConsole.WriteLineWriteLine(-p3:p3:00,11,p3,p3.x,p3x,p3.y y););7.3.3 使用对象 17 ConsoleConsole.WriteLineWriteLine(p1:(p1:00,11,p1,p1.x,p1x,p1.y y););ConsoleConsole.WriteLineWriteLine(p2:(p2:00,11,p2,p2.x,p2x,p2.y y);)

17、;ConsoleConsole.WriteLineWriteLine(p3:(p3:00,11,p3,p3.x,p3x,p3.y y););distancedistance(p1,p1,refref p2,p2,outout p3p3););ConsoleConsole.WriteLineWriteLine(-p1:p1:00,11,p1,p1.x,p1x,p1.y y););ConsoleConsole.WriteLineWriteLine(-p2:p2:00,11,p2,p2.x,p2x,p2.y y););ConsoleConsole.WriteLineWriteLine(-p3:p3:

18、00,11,p3,p3.x,p3x,p3.y y););7.3.3 使用对象 18 ConsoleConsole.WriteLineWriteLine(p1:(p1:00,11,p1,p1.x,p1x,p1.y y););ConsoleConsole.WriteLineWriteLine(p2:(p2:00,11,p2,p2.x,p2x,p2.y y););ConsoleConsole.WriteLineWriteLine(p3:(p3:00,11,p3,p3.x,p3x,p3.y y););distancedistance(p1,p1,refref p2,p2,outout p3p3););

19、ConsoleConsole.WriteLineWriteLine(-p1:p1:00,11,p1,p1.x,p1x,p1.y y););ConsoleConsole.WriteLineWriteLine(-p2:p2:00,11,p2,p2.x,p2x,p2.y y););ConsoleConsole.WriteLineWriteLine(-p3:p3:00,11,p3,p3.x,p3x,p3.y y););7.3.3 使用对象 19 由于类类型为引用类型,所以类类型作为方法的值参数,并没有将实参对象的实际数据复制一份,失去了作为值参数的意义。7.3.3 使用对象 20 对象作为方法返回值的

20、示例如下:usingusing SystemSystem;classclass PointPoint public doublepublic double x,yx,y;publicpublic PointPoint(doubledouble xx,xx,doubledouble yyyy)x x =xxxx;y y =yyyy;classclass TestTest 7.3.3 使用对象 21 staticstatic PointPoint distancedistance(Point p1,Point p2Point p1,Point p2)doubledouble d1,d2d1,d2;

21、d1d1 =MathMath.SqrtSqrt(p1p1.x x *p1p1.x x +p1p1.y y *p1p1.y y););d2d2 =MathMath.SqrtSqrt(p2p2.x x *p2p2.x x +p2p2.y y *p2p2.y y););returnreturn d1d1 d2d2?p1p1 :p2p2;static voidstatic void MainMain()()Point p1Point p1=newnew PointPoint(3 3,4 4););Point p2Point p2 =newnew PointPoint(2 2,5 5););Point

22、p3Point p3;p3p3=distancedistance(p1,p2p1,p2););ConsoleConsole.WriteLineWriteLine(p3:(p3:00,11,p3,p3.x,p3x,p3.y y););7.3.3 使用对象 22 staticstatic PointPoint distancedistance(Point p1,Point p2Point p1,Point p2)doubledouble d1,d2d1,d2;d1d1 =MathMath.SqrtSqrt(p1p1.x x *p1p1.x x +p1p1.y y *p1p1.y y););d2d2

23、 =MathMath.SqrtSqrt(p2p2.x x *p2p2.x x +p2p2.y y *p2p2.y y););returnreturn d1d1 d2d2?p1p1 :p2p2;static voidstatic void MainMain()()Point p1Point p1=newnew PointPoint(3 3,4 4););Point p2Point p2 =newnew PointPoint(2 2,5 5););Point p3Point p3;p3p3=distancedistance(p1,p2p1,p2););ConsoleConsole.WriteLineWriteLine(p3:(p3:00,11,p3,p3.x,p3x,p3.y y););结束

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 大学资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com