CSharp面向对象入门基础文档格式.docx
《CSharp面向对象入门基础文档格式.docx》由会员分享,可在线阅读,更多相关《CSharp面向对象入门基础文档格式.docx(25页珍藏版)》请在冰点文库上搜索。
![CSharp面向对象入门基础文档格式.docx](https://file1.bingdoc.com/fileroot1/2023-4/30/3adf7cec-16a1-4781-b5ad-02b577f22397/3adf7cec-16a1-4781-b5ad-02b577f223971.gif)
p4.Age=p4.Age+1。
Console.WriteLine(p4.Age)。
//Person5p5=newPerson5()。
//p5.Age=30。
//Console.WriteLine(p5.Age)。
Person6p6=newPerson6()。
p6.IncAge()。
//p6.Age=30。
年龄{0}"
p6.Age)。
Person7p7=newPerson7()。
p7.Age=30。
Console.WriteLine(p7.Age)。
Console.ReadKey()。
}
classPerson
publicintHeight。
publicintAge。
privatestringName。
publicvoidSayHello()
大家好,我叫{0},我的身高是{1},我的年龄是{2}"
this.Name,this.Height,this.Age)。
classPerson2
publicvoidGiveName(stringname)
if(name=="
jerry"
)
return。
this.Name=name。
睁眼()。
this.Name,this.Height,this.Age)。
privatevoid睁眼()
睁开双眼"
classPerson3
privateintage。
publicintAge1。
publicintAge
set//赋值
if(value<
0)
this.age=value。
get//取值
returnthis.age。
classPerson4
set
get
return3。
classPerson5
this.Age=value。
//给自己赋值,死循环
returnthis.Age。
classPerson6
publicintAge//只读属性,因为只有get,没有set
get{returnage。
publicvoidIncAge()
age++。
classPerson7
get。
//编译器自动帮我们生成私有字段和set、get代码块。
set。
publicstringName
}
2..聊天机器人
namespace面向对象版聊天机器人
机器人r1=new机器人()。
r1.Name="
小I"
r1.Eat(5)。
机器人r2=new机器人()。
r2.Name="
小J"
r1.Eat(8)。
Console.WriteLine()。
请选择机器人:
1→小I,2→小J"
机器人r。
stringstr=Console.ReadLine()。
if(str=="
1"
r=r1。
//r指向“r1指向的对象”
else
r=r2。
r.SayHello()。
while(true)
stringstr1=Console.ReadLine()。
r1.Speak(str1)。
class机器人
publicstringName{get。
set。
privateintFullLevel{get。
我叫:
{0}"
Name)。
publicvoidEat(intfoodCount)
if(FullLevel>
100)
FullLevel=FullLevel+foodCount。
publicvoidSpeak(stringstr)
if(FullLevel<
=0)
饿死了,不说了!
"
if(str.Contains("
姓名"
)||str.Contains("
名字"
))
this.SayHello()。
elseif(str.Contains("
女朋友"
年龄小,不考虑!
听不懂!
FullLevel--。
3.构造函数
namespace构造函数1
Personp2=newPerson("
Personp3=newPerson("
20)。
Personp4=newPerson()。
年龄:
{0},姓名:
{1}"
p1.Age,p1.Name)。
p2.Age,p2.Name)。
p3.Age,p3.Name)。
publicintAge{get。
publicPerson()
Name="
未命名"
Age=0。
publicPerson(stringname)
this.Name=name。
publicPerson(stringname,intage)
this.Age=age。
4.继承
namespace继承
中国人c1=new中国人()。
c1.Height=180。
c1.Name="
李小龙"
c1.SayHello()。
c1.户口="
北京"
韩国人k1=new韩国人()。
k1.Name="
金喜善"
k1.Height=170。
k1.SayHello()。
k1.做泡菜()。
Personp1=c1。
Personp2=k1。
//中国人zgr=p1。
//报错,因为p1是Person类
中国人zgr=(中国人)p1。
zgr.SayHello()。
//中国人zgr1=(中国人)p2。
//一旦程序员的保证不靠谱,照样报错
//zgr1.SayHello()。
Objectobj=3。
//Object是所有类的一个基类
classPerson//所有的类都继承于Object类,等价于Person:
Object
publicstringName{get。
publicintAge{get。
publicintHeight{get。
SayHello"
this.Name)。
class中国人:
Person
publicstring户口{get。
publicvoid功夫()
我打!
!
class韩国人:
Person
publicstring饭量{get。
publicvoid做泡菜()
泡菜香!
5.异常
namespace异常
/*
try
Console.WriteLine("
Convert之前"
inti=Convert.ToInt32("
abc"
Convert之后"
catch(Exceptionex)
数据错误:
+ex.Message+"
异常堆栈"
+ex.StackTrace)。
ReadKey之前"
Console.ReadKey()。
//DeleteFile("
c:
\1.avi"
\2.avi"
//inti=Convert.ToInt32("
*/
try
stringdesc=GetAgeDesc(300)。
catch(Exceptionex)
+ex.Message)。
staticstringGetAgeDesc(intage)
if(age>
=0&
&
age<
=3)
{return"
婴幼儿"
elseif(age>
3&
=18)
青少年"
19&
150)
大人"
elseif(age<
0)
{thrownewException("
您来自反物质世界吧!
您见过老佛爷吧!
staticintDeleteFile(stringfilepath)
//尝试删除文件,发现无法删除
return-1。
//return0。
如果没有权限return-2,找不到删除的文件return-3。
6.常量
namespace常量
publicconstintpi=3。
//不用new一个类就可以直接调
intr=10。
//intpi=3。
//constintpi=3。
//pi声明为常量
intl=2*pi*r。
周长:
l)。
intm=pi*r*r。
面积:
m)。
//Math.PI。
7.静态成员
namespace静态成员
Person.TotalCount=30。
Console.WriteLine(Person.TotalCount)。
DoIt()。
Dogd=newDog()。
d.叫唤()。
Person.人口汇报()。
p1.Age=300。
publicstaticvoidDoIt()
ffff"
使用全局变量:
Person.TotalCount)。
publicstaticintTotalCount。
//静态成员
publicstaticvoid人口汇报()//static函数
总人口:
{0},年龄:
TotalCount,Age)。
//static函数中不能调用非static成员
publicvoidSayHello()//非static函数
我的年龄:
{0},全球总人口:
Age,TotalCount)。
//非static函数可以调用static成员
classDog
publicvoid叫唤()
旺旺:
//静态类
staticclassConsoleHelper//静态类不能被实例化,也就是不能被new的类
publicstaticintReadInt()
stringstr=Console.ReadLine()。
returnConvert.ToInt32(str)。
8.命名空间
Program.cs
using命名空间.hr。
//如果要使用的类和当前的类不在同一个namespace,则要加using引用
using命名空间.oa。
usingSystem.Collections。
namespace命名空间
命名空间.hr.Personp2=new命名空间.hr.Person()。
//就像文件的全路径一样
Mousem=newMouse()。
ArrayListlist=newArrayList()。
//在ArrayList上点右键->
解读->
usingSystem.Collections
Person.cs
namespace命名空间.hr
Dog.cs
Mouse.cs
namespace命名空间.oa
classMouse
9.索引器
namespace索引
int[]values={3,5,9,8}。
inti=values[1]。
p1[1]="
小明"
Console.WriteLine(p1[1]+p1[2])。
strings="
//s[0]='
aa'
//因为s是string类型,是一个只读的索引器
Console.WriteLine(p1["
3,9])。
//p1["
3,9]="
aaa"
//报错,因为p1只读
privatestringFirstName="
大毛"
privatestringSecondName="
二毛"
publicstringthis[stringname,intx,inty]//索引的参数可以多个
returnname+x+y。
publicstringthis[intindex]//索引器
if(index==1)
FirstName=value。
elseif(index==2)
SecondName=value。
thrownewException("
错误的序号"
returnFirstName。
returnSecondName。