面向对象.docx
- 文档编号:29782473
- 上传时间:2023-07-26
- 格式:DOCX
- 页数:33
- 大小:387.73KB
面向对象.docx
《面向对象.docx》由会员分享,可在线阅读,更多相关《面向对象.docx(33页珍藏版)》请在冰豆网上搜索。
面向对象
面向对象
面向对象概念OOP
.面向对象不是取代面向过程的。
.类、对象。
“人”是类,“张三”是“人”这个类的对象。
类是抽象的,对象是具体的。
按钮就是类,某个按钮就是对象。
对象可以叫做类的实例(Instance)。
类就像int,对象就像10。
字段Field(和某个对象相关的变量),字段就是类的状态。
人这个类有姓名、年龄、身高等字段。
类不占内存,对象才占内存。
.方法Method,方法就是类能够执行的动作,比如问好、吃饭等。
.类的继承,类之间可以有继承关系,比如“电脑”类可以从“电器”类继承,这样的好处是“电脑”类只需要定义自己特有的字段、方法就可以,也就是只要定义内存大小、CPU型号这些字段或者弹出光驱等方法就可以。
父类(Parent)、基类(Base,基业,祖宗十八代传下来的)。
电脑类是电器类的子类(ChildClass)。
重用。
.面向对象的三个特性:
封装、继承、多态。
.没有面向对象的世界中的难题。
staticvoidMain(string[]args)
{
/*
Personp=newPerson();//创建一个Person对象,用p指向它。
p.Name="tom";
p.Hight=160;
p.Age=36;
p.SayHello();
}
classPerson
{
publicintHight;
publicintAge;
publicstringName;
publicvoidSayHello()
{
Console.WriteLine("大家好,我是{0},我的身高是{1},我的年龄{2}",this.Name,this.Hight,this.Age);//this.我自己的****
}
}
staticvoidMain(string[]args)
{
Person1p1=newPerson1();
p1.Hight=165;
p1.Age=20;
//p1.Name="tom";//错误,Name被定义成private,不能在外部访问。
p1.GiveName("汤姆");
//p1.OpenEyes();//错误,OpenEyes方法被定义成private,不能在外部访问。
p1.SayHello();
}
classPerson1
{
publicintHight;
publicintAge;
privatestringName;
publicvoidGiveName(stringname)
{
if(name=="tom"||name=="汤姆")
{
this.Name=name;
}
else
{
return;
}
}
publicvoidSayHello()
{
OpenEyes();
Console.WriteLine("大家好,我是{0},我的身高是{1},我的年龄是{2}",this.Name,this.Hight,this.Age);//this.我自己的****
}
privatevoidOpenEyes()
{
Console.WriteLine("睁开眼睛!
");
}
}
staticvoidMain(string[]args)
{
person2p2=newperson2();
p2.Age=30;
Console.WriteLine("年龄:
{0}",p2.Age);
p2.Age=-30;
Console.WriteLine("年龄:
{0}",p2.Age);
person3p3=newperson3();
p3.Age=30;
p3.Age=p3.Age+1;
Console.WriteLine(p3.Age);
person4p4=newperson4();
p4.Age=30;
Console.WriteLine(p4.Age);
person5p5=newperson5();
//p5.Age=30;错误,属性只读。
p5.IncAge();
p5.IncAge();
Console.WriteLine("年龄:
{0}",p5.Age);
person6p6=newperson6();
p6.Age=30;
p6.Name="飞鸿";
Console.WriteLine("我的号是:
{0},我的年龄:
{1}",p6.Name,p6.Age);
}
classperson2
{
privateintage;
publicintAge//Age中没有保存数据,数据都保存到age中了
{
set//赋值
{
if(value<0||value>200)//与public字段和属性的区别,可以设置非法值的判断
{
return;
}
else
{
this.age=value;//value代表用户赋值过来的值
}
}
get//取值
{
returnthis.age;
}
}
}
classperson3
{
publicintAge
{
set
{
}
get
{
return3;
}
}
}
classperson4//最常见的错误的类,死循环
{
privateintage;
publicintAge
{
set
{
this.Age=value;
}
get
{
returnthis.Age;
}
}
}
classperson5
{
privateintage;
publicintAge//只读属性
{
get{returnage;}
}
publicvoidIncAge()
{
age++;
}
}
classperson6
{
//从.net3.0以后,如果对输入没有限制,可以用以下方式简写属性。
publicintAge
{
set;//编译器自动帮我生成私有字段和set,get代码块。
get;
}
publicstringName
{
set;
get;
}
}
课上练习
.面向对象版聊天机器人
.机器人有不同的名字、维护自己的FullLevel,可以SayHello(我叫***),可以喂食(Eat(intfoodCount)),可以对它说话(Speak),对异常情况(错误的喂饭数字,喂的太多撑死了)进行处理,有两个机器人供选择,一开始通过1、2数字选择聊天机器人。
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace面向对象的聊天机器人1
{
classProgram
{
staticvoidMain(string[]args)
{
机器人r1=new机器人();
r1.Name="小二";
r1.Eat(5);
机器人r2=new机器人();
r2.Name="小三";
r2.Eat(8);
Console.WriteLine("请选择机器人:
1->小二,2->小三");
strings=Console.ReadLine();
机器人r;
if(s=="1")
{
r=r1;//r指向“r1指向的对象”
}
else
{
r=r2;
}
r.SayHello();
while(true)
{
stringstr=Console.ReadLine();
r.Speek(str);
}
}
}
class机器人
{
publicstringName{get;set;}
privateintFullLevel{get;set;}
publicvoidSayHello()
{
Console.WriteLine("我叫:
{0}",this.Name);
}
publicvoidEat(intfoodCount)
{
if(FullLevel>100)
{
return;
}
FullLevel=FullLevel+foodCount;
}
publicvoidSpeek(stringstr)
{
if(FullLevel<=0)
{
Console.WriteLine("饿死了,不说了!
");
return;
}
if(str.Contains("姓名")||str.Contains("名字"))
{
this.SayHello();//类的方法调用同类的另一个方法
}
elseif(str.Contains("女朋友"))
{
Console.WriteLine("年龄小,不考虑!
");
}
else
{
Console.WriteLine("听不懂!
");
}
FullLevel--;
}
}
}
对象的引用(非常重要)
.i1=3;i2=i1;i1++;//i2是3
.p1=newPerson();p1.i=3;p2=p1;p1.i++;//p2.i是4
.int、string、decimal、bool、byte等基础类型(值类型)是传递拷贝;对象(引用类型)则是传递引用。
因为基础类型不怎么占内存,而对象则比较占内存。
.函数间传递对象。
.为对象变量重新赋值。
p2=p1是让p2指向p1指向的对象。
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace对象的引用
{
classProgram
{
staticvoidMain(string[]args)
{
inti=10;
intj=i;
i++;
Console.WriteLine(j);
//intdataTimecharbool等类型是值类型(ValueType),值类型赋值的时候是传递拷贝。
personp1=newperson(20);
personp2=p1;
p1.Age++;
Console.WriteLine(p2.Age);
//普通对象是引用类型,赋值的时候是传递引用。
IncAge(p2);
Console.WriteLine(p2.Age);//传递给函数也是传递引用
Console.ReadKey();
}
staticvoidIncAge(personp)
{
p.Age++;
}
}
classperson
{
publicintAge{get;set;}
publicperson(intage)
{
this.Age=age;
}
}
}
构造函数
.构造函数用来创建对象,并且可以在构造函数中对对象进行初始化。
.构造函数是用来创建对象的特殊函数,函数名和类名一样,没有返回值,连void都不用。
.构造函数可以有参数,new对象的时候传递函数参数即可
.构造函数可以重载,也就是有多个参数不同的构造函数。
.如果不指定构造函数,则类有一个默认的无参构造函数。
如果指定了构造函数,则不再有默认的无参构造函数,如果需要无参构造函数,则需要自己来写。
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace构造函数
{
classProgram
{
staticvoidMain(string[]args)
{
personp1=newperson();
personp2=newperson("小二");//构造函数的好处是创建一个对象时,给对像赋初值,构造函数怎么去处理,我不管。
personp3=newperson("小三",20);
Console.WriteLine("姓名:
{0},年龄:
{1}",p1.Name,p1.Age);
Console.WriteLine("姓名:
{0},年龄:
{1}",p2.Name,p2.Age);
Console.WriteLine("姓名:
{0},年龄:
{1}",p3.Name,p3.Age);
Console.ReadKey();
}
}
classperson
{
publicstringName{get;set;}
publicintAge{get;set;}
publicperson()
{
this.Name="未命名";
this.Age=0;
}
publicperson(stringname)
{
this.Name=name;
this.Age=0;
}
publicperson(stringname,intage)
{
this.Name=name;
this.Age=age;
}
}
}
继承
publicclassChinese:
Person
{
publicstringHuKou{get;set;}
publicvoidKongFu(){....}
}
Chinesep1=newChinese();
p1.Name="李雷";
p1.HuKou="北京市朝阳区";
p1.SayHello();
p1.KongFu();
虚方法
publicclassPerson
{
publicvirtualvoidSayHello()
{
Console.WriteLine("我的年龄是{0}",Age);
}
}
publicclassChinese:
Person
{
publicstringHuKou{get;set;}
publicoverridevoidSayHello()
{
Console.WriteLine("小生虚度光阴{0}载",Age);
}
}Korean类。
Chinesep1=newChinese();
p1.SayHello();
定义类的时候不指定父类,则父类是Object类。
Object类是任何类的直接或者间接父类。
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace继承
{
classProgram
{
staticvoidMain(string[]args)
{
中国人c1=new中国人();
c1.Name="李小龙";
c1.SayHello();
c1.户口="中国贵州";
c1.功夫();
Console.WriteLine(c1.户口);
韩国人k1=new韩国人();
k1.Name="金三顺";
k1.SayHello();
k1.做泡菜();
personp1=c1;
p1.SayHello();
personp2=k1;
p2.SayHello();
//中国人c2=newperson();
//中国人zgr=p1;//我要一个中国人,给了我一个person类型的变量,我一看p1是person类型,我就报错,给的要不是中国人咋办?
谁负责?
中国人zgr=(中国人)p1;//我看到了p1是person类型,但程序员说了,“大胆去指向吧”,p1指向的一定是一个中国人。
//中国人zgr1=(中国人)p2;//一旦程序员的保证不靠近普,照样报错。
objecto1=p1;
objecto2=3;//object类是所有类的基类,没有指定父类那么父类就是object类。
Console.ReadKey();
}
}
classperson
{
publicstringName{get;set;}
publicintAge{get;set;}
publicintHight{get;set;}
publicvoidSayHello()
{
Console.WriteLine("你好,我叫{0}",this.Name);
}
}
class中国人:
person
{
publicstring户口{get;set;}
publicvoid功夫()
{
Console.WriteLine("我打!
!
!
!
");
}
}
class韩国人:
person
{
publicstring饭量{get;set;}
publicvoid做泡菜()
{
Console.WriteLine("我是韩国人,我会做泡菜!
");
}
}
}
对象的隐式转换和显式转换
Chinesech=newChinese();//隐式转换,把子类变量赋值给父类变量
Personp=ch;//显式转换,把父类变量赋值给子类变量
Personp=newChinese();
Chinesech=(Chinese)p;//如果对象不在同一个继承树路径上//则不能强制类型转换
Dogg=newDog();
Chinesech=(Chinese)g;//错误
//is运算
if(pisChinese)
{
Console.WriteLine("中国人");
}
elseif(pisKorean)
{
Console.WriteLine("韩国人");
}
//as运算符
Chinesech=pasChinese;
if(ch!
=null)
{
Console.WriteLine("中国人");
}
Koreanch=pasKorean;
if(ch!
=null)
{
Console.WriteLine("韩国人");
}
()转换和as转换的区别:
如果转换失败()会报异常,而as则会返回null。
异常与异常处理
.传统的错误表示方式:
错误码。
举例。
需要知道不同错误码的含义,如果不处理错误码,则程序可能陷入不可以预置的错误。
陈摄影师以为文件已经被删除造成的麻烦。
.错误码的缺点:
不处理则很难发现,每次处理则很麻烦;难以看出错误的原因;容易使得程序进入不确定状态。
.trycatch。
Exceptionex异常也是对象。
.Exception类主要属性:
Message、StackTrace
.发生异常后程序默认就退出了,try代码块中的后续代码不会被执行。
catch以后的代码则会继续执行。
.不要吃掉异常,一般情况下不需要处理异常。
.扔出自己的异常,扔:
throw,抓住:
catch
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之前");
*/
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>18&&age<=150)
{
return"成年人";
}
elseif(age<0)
{
thrownewException("你来自反物质世界吧!
");//throw扔,catch抓,出现异常的时候我们已经new好一个exception对象扔(throw)出来,咱们抓(catch)住即可。
}
else
{
thrownewException("你见过老佛爷吧!
");
}
}
}
}
常量与静态成员
.const常量。
常量名要大写。
一定不会变化的值才能声明为常量。
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace常量
{
classProgram
{
publicconstintpi=3;//publicconst不用new一个类就可以直接调用
staticvoidMain(string[]args)
{
/*
intr=10;
intl=2*3*r;
Console.WriteLine
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向 对象