CSharp面向对象入门基础.docx
《CSharp面向对象入门基础.docx》由会员分享,可在线阅读,更多相关《CSharp面向对象入门基础.docx(25页珍藏版)》请在冰豆网上搜索。
CSharp面向对象入门基础
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.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.WriteLine("年龄是{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)。
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。
privatestringName。
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.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
{
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。
}
publicstringName
{
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.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)。
}
Console.ReadKey()。
}
class机器人
{
publicstringName{get。
set。
}
privateintFullLevel{get。
set。
}
publicvoidSayHello()
{
Console.WriteLine("我叫:
{0}",Name)。
}
publicvoidEat(intfoodCount)
{
if(FullLevel>100)
{
return。
}
FullLevel=FullLevel+foodCount。
}
publicvoidSpeak(stringstr)
{
if(FullLevel<=0)
{
Console.WriteLine("饿死了,不说了!
")。
return。
}
if(str.Contains("姓名")||str.Contains("名字"))
{
this.SayHello()。
}
elseif(str.Contains("女朋友"))
{
Console.WriteLine("年龄小,不考虑!
")。
}
else
{
Console.WriteLine("听不懂!
")。
}
FullLevel--。
}
}
}
}
3.构造函数
usingSystem。
usingSystem.Collections.Generic。
usingSystem.Linq。
usingSystem.Text。
namespace构造函数1
{
classProgram
{
staticvoidMain(string[]args)
{
Personp1=newPerson()。
Personp2=newPerson("tom")。
Personp3=newPerson("jerry",20)。
Personp4=newPerson()。
Console.WriteLine("年龄:
{0},姓名:
{1}",p1.Age,p1.Name)。
Console.WriteLine("年龄:
{0},姓名:
{1}",p2.Age,p2.Name)。
Console.WriteLine("年龄:
{0},姓名:
{1}",p3.Age,p3.Name)。
Console.ReadKey()。
}
}
classPerson
{
publicstringName{get。
set。
}
publicintAge{get。
set。
}
publicPerson()
{
Name="未命名"。
Age=0。
}
publicPerson(stringname)
{
this.Name=name。
}
publicPerson(stringname,intage)
{
this.Name=name。
this.Age=age。
}
}
}
4.继承
usingSystem。
usingSystem.Collections.Generic。
usingSystem.Linq。
usingSystem.Text。
namespace继承
{
classProgram
{
staticvoidMain(string[]args)
{
中国人c1=new中国人()。
c1.Height=180。
c1.Name="李小龙"。
c1.SayHello()。
c1.户口="北京"。
韩国人k1=new韩国人()。
k1.Name="金喜善"。
k1.Height=170。
k1.SayHello()。
k1.做泡菜()。
Personp1=c1。
p1.SayHello()。
Personp2=k1。
k1.SayHello()。
//中国人zgr=p1。
//报错,因为p1是Person类
中国人zgr=(中国人)p1。
zgr.SayHello()。
//中国人zgr1=(中国人)p2。
//一旦程序员的保证不靠谱,照样报错
//zgr1.SayHello()。
Objectobj=3。
//Object是所有类的一个基类
Console.ReadKey()。
}
classPerson//所有的类都继承于Object类,等价于Person:
Object
{
publicstringName{get。
set。
}
publicintAge{get。
set。
}
publicintHeight{get。
set。
}
publicvoidSayHello()
{
Console.WriteLine("SayHello",this.Name)。
}
}
class中国人:
Person
{
publicstring户口{get。
set。
}
publicvoid功夫()
{
Console.WriteLine("我打!
!
!
")。
}
}
class韩国人:
Person
{
publicstring饭量{get。
set。
}
publicvoid做泡菜()
{
Console.WriteLine("泡菜香!
")。
}
}
}
}
5.异常
usingSystem。
usingSystem.Collections.Generic。
usingSystem.Linq。
usingSystem.Text。
namespace异常
{
classProgram
{
staticvoidMain(string[]args)
{
/*
try
{
Console.WriteLine("Convert之前")。
inti=Convert.ToInt32("abc")。
Console.WriteLine("Convert之后")。
}
catch(Exceptionex)
{
Console.WriteLine("数据错误:
"+ex.Message+"。
异常堆栈"+ex.StackTrace)。
}
Console.WriteLine("ReadKey之前")。
Console.ReadKey()。
//DeleteFile("c:
\1.avi")。
//DeleteFile("c:
\2.avi")。
//inti=Convert.ToInt32("abc")。
*/
try
{
stringdesc=GetAgeDesc(300)。
}
catch(Exceptionex)
{
Console.WriteLine("数据错误:
"+ex.Message)。
}
Console.ReadKey()。
}
staticstringGetAgeDesc(intage)
{
if(age>=0&&age<=3)
{return"婴幼儿"。
}
elseif(age>3&&age<=18)
{return"青少年"。
}
elseif(age>19&&age<150)
{return"大人"。
}
elseif(age<0)
{thrownewException("您来自反物质世界吧!
")。
}
else
{thrownewException("您见过老佛爷吧!
")。
}
}
staticintDeleteFile(stringfilepath)
{
//尝试删除文件,发现无法删除
return-1。
//return0。
如果没有权限return-2,找不到删除的文件return-3。
}
}
}
6.常量
usingSystem。
usingSystem.Collections.Generic。
usingSystem.Linq。
usingSystem.Text。
namespace常量
{
classProgram
{
publicconstintpi=3。
//不用new一个类就可以直接调
staticvoidMain(string[]args)
{
intr=10。
//intpi=3。
//constintpi=3。
//pi声明为常量
intl=2*pi*r。
Console.WriteLine("周长:
{0}",l)。
intm=pi*r*r。
Console.WriteLine("面积:
{0}",m)。
//Math.PI。
Console.ReadKey()。
}
}
}
7.静态成员
usingSystem。
usingSystem.Collections.Generic。
usingSystem.Linq。
usingSystem.Text。
namespace静态成员
{
classProgram
{
staticvoidMain(string[]args)
{
Person.TotalCount=30。
Console.WriteLine(Person.TotalCount)。
DoIt()。
Dogd=newDog()。
d.叫唤()。
Person.人口汇报()。
Personp1=newPerson()。
p1.Age=300。
p1.SayHello()。
Console.ReadKey()。
}
publicstaticvoidDoIt()
{
Console.WriteLine("ffff")。
Console.WriteLine("使用全局变量:
{0}",Person.TotalCount)。
}
}
classPerson
{
publicstaticintTotalCount。
//静态成员
publicintAge。
publicstaticvoid人口汇报()//static函数
{
//Console.WriteLine("总人口:
{0},年龄:
{1}",TotalCount,Age)。
//static函数中不能调用非static成员
}
publicvoidSayHello()//非static函数
{
Console.WriteLine("我的年龄:
{0},全球总人口:
{1}",Age,TotalCount)。
//非static函数可以调用static成员
}
}
classDog
{
publicvoid叫唤()
{
Console.WriteLine("旺旺:
{0}",Person.TotalCount)。
}
}
//静态类
staticclassConsoleHelper//静态类不能被实例化,也就是不能被new的类
{
publicstaticintReadInt()
{
stringstr=Console.ReadLine()。
returnConvert.ToInt32(str)。
}
}
}
8.命名空间
Program.cs
usingSystem。
usingSystem.Collections.Generic。
usingSystem.Linq。
usingSystem.Text。
using命名空间.hr。
//如果要使用的类和当前的类不在同一个namespace,则要加using引用
using命名空间.oa。
usingSystem.Collections。
namespace命名空间
{
classProgram
{
staticvoidMain(string[]args)
{
Personp1=newPerson()。
命名空间.hr.Personp2=new命名空间.hr.Person()。
//就像文件的全路径一样
Dogd=newDog()。
Mousem=newMouse()。
ArrayListlist=newArrayList()。
//在ArrayList上点右键->解读->usingSystem.Collections
Console.ReadKey()。
}
}
classPerson
{
}
}
Person.cs
usingSystem。
usingSystem.Collections.Generic。
usingSystem.Linq。
usingSystem.Text。
namespace命名空间.hr
{
classPerson
{
}
}
Dog.cs
usingSystem。
usingSystem.Collections.Generic。
usingSystem.Linq。
usingSystem.Text。
namespace命名空间.hr
{
classDog
{
}
}
Mouse.cs
usingSystem。
usingSystem.Collections.Generic。
usingSystem.Linq。
usingSystem.Text。
namespace命名空间.oa
{
classMouse
{
}
}
9.索引器
usingSystem。
usingSystem.Collections.Generic。
usingSystem.Linq。
usingSystem.Text。
namespace索引
{
classProgram
{
staticvoidMain(string[]args)
{
int[]values={3,5,9,8}。
inti=values[1]。
Personp1=newPerson()。
p1[1]="小明"。
Console.WriteLine(p1[1]+p1[2])。
strings=""。
//s[0]='aa'。
//因为s是string类型,是一个只读的索引器
Console.WriteLine(p1["tom",3,9])。
//p1["tom",3,9]="aaa"。
//报错,因为p1只读
Console.ReadKey()。
}
}
classPerson
{
privatestringFirstName="大毛"。
privatestringSecondName="二毛"。
publicstringthis[stringname,intx,inty]//索引的参数可以多个
{
get
{
returnname+x+y。
}
}
publicstringthis[intindex]//索引器
{
set
{
if(index==1)
{
FirstName=value。
}
elseif(index==2)
{
SecondName=value。
}
else
{
thrownewException("错误的序号")。
}
}
get
{
if(index==1)
{
returnFirstName。
}
elseif(index==2)
{
returnSecondName。
}
else
{
thrownewException("错误的序号")。
}
}
}
}
}