欢迎来到天天文库
浏览记录
ID:36024469
大小:94.00 KB
页数:13页
时间:2019-04-29
《c sharp面向对象基础new》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、C#面向对象基础1.面向对象简介usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceC_Sharp面向对象基础{classProgram{staticvoidMain(string[]args){Personp1=newPerson();//p1.Name="tom";p1.Height=180;p1.Age=30;p1.SayHello();Person2p2=newPerson2();p2.Age=20;p2.GiveNa
2、me("tom");p2.SayHello();Person3p3=newPerson3();p3.Age=30;//Console.WriteLine("年龄是{0}",p3.age);p3.Age=-1;Console.WriteLine("年龄是{0}",p3.Age);p3.Age1=-100;Console.WriteLine("年龄是{0}",p3.Age1);Person4p4=newPerson4();p4.Age=30;p4.Age=p4.Age+1;Console.WriteLine(p4.Age);//Person5p5=n
3、ewPerson5();//p5.Age=30;//Console.WriteLine(p5.Age);Person6p6=newPerson6();p6.IncAge();p6.IncAge();//p6.Age=30;Console.WriteLine("年龄{0}",p6.Age);Person7p7=newPerson7();p7.Age=30;Console.WriteLine(p7.Age);Console.ReadKey();}classPerson{publicintHeight;publicintAge;privatestrin
4、gName;publicvoidSayHello(){Console.WriteLine("大家好,我叫{0},我的身高是{1},我的年龄是{2}",this.Name,this.Height,this.Age);}}classPerson2{publicintHeight;publicintAge;privatestringName;publicvoidGiveName(stringname){if(name=="jerry"){return;}this.Name=name;}publicvoidSayHello(){睁眼();Console.
5、WriteLine("大家好,我叫{0},我的身高是{1},我的年龄是{2}",this.Name,this.Height,this.Age);}privatevoid睁眼(){Console.WriteLine("睁开双眼");}}classPerson3{privateintage;publicintAge1;publicintAge{set//赋值{if(value<0){return;}this.age=value;}get//取值{returnthis.age;}}}classPerson4{publicintAge{set{}get{
6、return3;}}}classPerson5{privateintage;publicintAge{set{this.Age=value;//给自己赋值,死循环}get{returnthis.Age;}}}classPerson6{privateintage;publicintAge//只读属性,因为只有get,没有set{get{returnage;}}publicvoidIncAge(){age++;}}classPerson7{publicintAge{get;//编译器自动帮我们生成私有字段和set、get代码块。set;}public
7、stringName{get;set;}}}}2..聊天机器人usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespace面向对象版聊天机器人{classProgram{staticvoidMain(string[]args){机器人r1=new机器人();r1.Name="小I";r1.Eat(5);机器人r2=new机器人();r2.Name="小J";r1.Eat(8);Console.WriteLine();Console.W
8、riteLine("请选择机器人:1→小I,2→小J");机器人r;stringstr=Console.ReadLine();if(st
此文档下载收益归作者所有