面向对象基础知识点归纳

一、面向对象编程
面向对象编程(缩写:oop)是一种编程理念,这种编程理念更符合我们人的逻辑思维;使用面向对象编程可以提高我们开发速度和代码的重用率;面向对象的开发非常适合大型程序开发,开发速度和后期维护都比过程开发要好很多 。
二、面向过程和面向对象理解
面向过程和面向对象都是用来解决问题的,只是解决问题的思路不同。
面向过程可以理解亲力亲为,面向对象可以理解为坐享其成。
例如炒菜
面向过程
1)市场买菜、2)讨价还价、3)洗菜、4)切菜、5)炒菜、6)吃
面向对象
1)点开APP、2)下单、3)吃
面向过程:
在生活中就是亲力亲为,在程序面向过程编程就是函数之间的一个相互调用。以函数为基本结构使用。
面向对象:
在生活中坐享其成,别人做好的东西为我所用。在程序中面向对象编程是以对象为基本结构使用。
三、对象和类的相关概念
1、类
生活中的类和对象概念
类:是对一群有相同特征或者行为的事物的统称,是抽象的,不能直接使用的。
例如人类猫类狗类
描述一个类通过行为和特征
行为:会跑,会汪
特征:黄毛,大眼睛
类可以理解为事物的模板
2、对象
是由类创造出来的具体实体,可以直接使用的。例如大黄,小白。
3、类和对象的关系
1)先有类后有对象
2)类只有一个对象可以有多个
4、类的设计
在程序中设计一个类,通常要满足一下3个要素:
类名:大驼峰命名法
属性:描述这类事物的特征。
方法:描述这类事物的具体行为。
四、定义一个简单的类(只包含方法)

class 	Dog:#定义类用class	
       def eat(self)
             print('吃狗粮')
       def sleep(self):
		    print('睡觉去')

wangcai=Dog()#创建对象
wangcai.eat()#旺财会吃饭
wangcai.sleep()#旺财会睡觉


ha=Dog()#再创建一个对象
ha.eat()#二哈会吃饭
ha.sleep()#二哈会睡觉

五、self变量
self:哪个对象调用方法或者属性,self就是那个对象。

练习查看self和对象的内存id值是否相同。
class Dog:
    def eat(self):
        print('吃狗粮')

    def sleep(self):
        print('睡觉去')

dog=Dog()
dog.eat()
dog.sleep()
print('dog的id'id(dog))

dog1=Dog()
dog1.sleep()
dog1.eat()
print('dog1的id'id(dog1))
#你会发现dog和dog1的id地址不同,因为他们是两个不同的对象

六、在类的外部增加属性
方式:对象名.属性名=值
注意:这种方式虽然简单,但是不推荐使用

class Person:
    def eat(self):
        print('吃饭了')
        print(self.name)#打印name的值

per=Person()
per.name='张三'
per.eat()

per1=Person()
#不添加name属性,但是调用eat()方法就会报错
per1.eat()

七、init()方法
init()内置方法在创建对象的时候自动执行。
我们可以在__init__()中做一些初始化操作

class Cat:
    def __init__(self):
        print('__init__是初始化方法')
        self.name='tom'#给对象添加一个name属性
cat=Cat()
print(cat.name)

cat2=Cat()
print(cat2.name)

1、利用参数设置初始值
在调用__init__():方法的时候进行初始值的设置
格式:init(self,arg1,arg2…)
对象调用时:dog=Dog(‘aa’,‘bb’…)
实参和形参一一对应。

class Dog:
    def __init__(self,new_name,nem_age):
        self.new_name=new_name
        self.new_age=nem_age

    def lst(self):
        print('狗名{},年龄是{}岁'.format(self.new_name,self.new_age))

dog=Dog('旺财',2)
dog.lst()

dog1=Dog('二哈',2)
dog.lst()

八、str()方法
打印对象的名称时默认用的是__str__()方法,此方法默认返回的是对象的内存地址。
我们可以重写__str__()方法打印对象保存的信息。

class Dog:
    def __init__(self,new_name,new_age):
        self.new_name=new_name
        self.new_age=new_age

    def __str__(self):
        msg=('此狗叫{},今年{}岁了'.format(self.new_name,self.new_age))
        return msg
dog=Dog('旺财',3)
print(dog)

小练习
1、球圆的周长和面积

from math import pi
class Circle:
    def __init__ (self,r):
        self.r=r
    def zhouchang (self):
        return 2*pi*self.r
    def area(self):
        return pi*self.r*self.r
c1=Circle(3)
zc=c1.zhouchang()
print(zc)
mj=c1.area()
print(mj)

2、烤羊肉串

class KYRC:
    def __init__(self):
        self.cooke_time=0
        self.cooke_zt='生的'
        self.cooke_zl=[]

    def cooke(self,cooke_time):
        self.cooke_time+=cooke_time
        if self.cooke_time>=0 and cooke_time<=3:
            self.cooke_zt='生的'

        elif self.cooke_time>=4 and cooke_time<=6:
            self.cooke_zt='半生不熟'

        elif self.cooke_time>=7 and cooke_time<=10:
            self.cooke_zt='熟了'

        else:
            self.cooke_zt='烧焦了'

    def add_zl(self,zl):
        self.cooke_zl.append(zl)

    def __str__(self):
        msg=('烤了{}分钟,现在状态{},添加的作料有{}'.format(self.cooke_time,self.cooke_zt,self.cooke_zl))
        return msg

chuan1=KYRC()
chuan1.cooke(2)
chuan1.add_zl('刷油')
print(chuan1)
chuan1.cooke(4)
chuan1.add_zl('盐')
print(chuan1)
chuan1.cooke(7)
chuan1.add_zl('孜然,辣椒粉')
print(chuan1)

3、星球崛起

class Person:
    def __init__(self,name,hp,atk,):
        self.name=name
        self.hp=hp
        self.atk=atk
    def attack(self,StartStart):
       starstar.hp=starstar.hp-self.atk
    def __str__(self):
        return '{}发动攻击,攻击力{},剩余HP{},对手HP{}'.\
            format(starstar.name,starstar.atk,starstar.hp,self.hp)

class StarStar:
    def __init__(self,name,hp,atk):
        self.name=name
        self.hp=hp
        self.atk=atk
    def attack(self,person):
        person.hp=person.hp-self.atk
    def __str__(self):
        return '{}发动攻击,攻击力{},剩余HP{},对手HP{}'.\
            format(person.name,person.atk,person.hp,self.hp)

person=Person('诸葛亮',100,10)
starstar=StarStar('金刚',100,20)
person.attack(starstar)#人攻击星星
print(starstar)
print('--------------->')
starstar.attack(person)
print(person)
#注:可根据自己语言表达方式自由搭配format传递给前面{}的值,逻辑正确即可

猜你喜欢

转载自blog.csdn.net/qq_44240254/article/details/86317087