欢迎来到天天文库
浏览记录
ID:62033106
大小:79.50 KB
页数:13页
时间:2021-04-15
《CSharp面向对象基础.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
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;
2、 p1.SayHello(); Person2p2= newPerson2(); p2.Age=20;p2.GiveName(”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.WriteLin
3、e(”年龄是{0}",p3。Age1); Person4p4= newPerson4(); p4.Age=30; p4.Age =p4.Age+1; Console。WriteLine(p4.Age); //Person5p5 = newPerson5(); //p5.Age =30; //Console。WriteLine(p5。Age); Person6 p6=newPerson6(); p6.IncAge(); p6.IncAge(); //p6.Age=30
4、; Console.WriteLine("年龄{0}”,p6。Age); Person7p7 =newPerson7(); p7。Age =30; Console。WriteLine(p7。Age); Console.ReadKey(); } classPerson { public int Height; public int Age; private string Name; publicvoid SayHello() { Console。WriteL
5、ine(”大家好,我叫{0},我的身高是{1},我的年龄是{2}”,this.Name,this.Height,this.Age); } } classPerson2 { publicintHeight; publicintAge; privatestringName; public voidGiveName(stringname) { if(name=="jerry") { return; } this.Name=name; } public
6、void SayHello() { 睁眼(); Console.WriteLine("大家好,我叫{0},我的身高是{1},我的年龄是{2}”,this。Name, this。Height, this。Age); } private void睁眼() { Console.WriteLine("睁开双眼”); } } classPerson3 { privateint age; publicintAge1; public i
7、ntAge { set //赋值 { if(value<0) { return; } this。age=value; } get//取值 { returnthis。age; } } } classPerson4 { public int Age { set { } get { return3; } } } classPerson5{ private
8、intage; publicintAge { set { this.Age = value;//给自己赋值,死循环 } get { returnthis.Age; } } } classPerson6 { privateintage; public intAge//只读属性,因为只有get,没有set { get{ return age; } } publicvoidIncAge()
此文档下载收益归作者所有