day15: Object-Oriented

First, object-oriented

Object oriented programming process more emphasis on
object-oriented three properties: encapsulation, inheritance, polymorphism
py in all classes with a parent class inherit

1, class with class

#class 类
class Person:
   pass

Was added to the class attributes:
# Attribute
name = "JS"
Age = 18 is
Hair = "Black"
to the class Incorporation:
DEF Tell (Self)

#class 类
class Person:
    #属性
    name = "js"
    age = 18
    hair = "black"
    
    #方法 self:谁调用就是谁
    def tell(self):
        print("my name is %s,age %s,hair is %s"%(self.name,self.age,self.hair))
    pass

2, the object is instantiated

Using the class name () shows an example of an object

p = Person()
print(p.name)
p.tell()

3, using the constructor

The method can be configured to initialize instance instantiation

#class 类
class Person:
    #属性
    name = "js"
    age = 18
    hair = "black"
    #构造方法:在实例化的时候自动调用
    #如果定义了构造器则没有默认的缺省的构造器了
    def __init__(self,name,age):
        self.name = name
        self.age = age
    #方法 self:谁调用就是谁
    def tell(self):
        print("my name is %s,age %s,hair is %s"%(self.name,self.age,self.hair))
    pass
#类名() 表示实例化一个对象
p = Person("js",18)
p.tell()
p1 = Person("LY",14)
p1.tell()

4, access to the original property

Can be accessed by Person.

#访问原来属性:
print(Person.name)
print(Person.age)
Person.tell(p1) #self可以传入实例

Note:
It can be seen, the class instance attributes are different.
When there is no parameter attributes incoming call, the call to class attributes

5, destructor

When an instance is recovered automatically invoked

class Person:
    def __init__(self,name,age):
        self.name = name
        self.age = age
    #析构函数,当实例被回收时自动调用
    def __del__(self):
        print("析构函数调用")
    def tell(self):
        print("mtname is %s,age %s"%(self.name,self.age))

p = Person("sj",18)
p.tell()
"""
当程序结束时,析构函数就会调用
"""

6, the use of private property, private methods

__ can be provided by private attributes and private methods: e.g.
__name__
DEF __setName (Self, name):

After setting up private and can only be called by the public

class Person:
    def __init__(self,name,age):
        #加两个下划线表示私有属性,不能直接被外部访问
        self.__name = name
        self.__age = age
    def getAge(self):
        return  self.__age
    def setAge(self,age):
        self.__age = age
    #可以设置私有方法,同理,只能通过公用方法调用
    def __setName(self,name):
        self.__name = name
    def setNamePass(self):
        pwd = 123
        res = int(input("请输入密码:"))
        if res == pwd:
            self.__setName("haha修改成功")
        else:
            print("pass word error")
    def tell(self):
        print("mtname is %s,age %s"%(self.__name,self.__age))

p = Person("sj",18)
p.tell()
print(p.getAge())
p.setAge(10)
p.tell() #可以看出修改成功

p.setNamePass()
p.tell()

7, inheritance, rewrite, expand

Inheritance, the parent class to fill in the brackets, so that you can have the properties of the parent class method, reduce duplication of effort

#继承,括号内填写父类,如
class MtList(list):
    pass

In succession, the often cited in other files class
by IF name '== main ' function can be imported only when inheritance is very common

A small exercise as follows:

from day15.面向对象 import Person
#通过 if __name__ == '__main__' 可以只导入功能,
p = Person("LS",18)
p.tell()

class Basterd(Person):
    #方法拓展,在保留父类方法时拓展
    def __init__(self,name,age,badhobby):
        super().__init__(name,age)
        self.badhobby = badhobby
    #方法重写,完全覆盖父类方法
    def tell(self):
        print("我叫%s,age %s,我爱%s"%(self.name,self.age,self.badhobby))
"""
建立一个坏弹类,
坏蛋是一个人,
坏蛋有他特殊的属性与发方法
"""
if __name__ == '__main__':
    p = Basterd("ly",18,"打架")
    p.tell()

Wherein, the init () uses expand,
Tell () is directly covered

8, small practice

class Clip:
    def __init__(self,bullet):
        self.bullet = bullet
class Gun:
    def __init__(self,clip):
        self.clip = clip
    def fier(self,n):
        if self.clip.bullet > n:
            self.clip.bullet -= n
            print("开枪;还剩%s发子弹"%(self.clip.bullet))
            for i in range(n):
                print("tu~")
        else:
            print("子弹不够啦,上子弹啊!")
class Person:
    def __init__(self,gun):
        self.gun = gun
    def fill(self,n):
        self.gun.clip.bullet += n
        print("上弹成功,还有%s发"%(self.gun.clip.bullet))
from day15.clip import *
from day15.gun import *
from day15.Person import *

c = Clip(10)
g = Gun(c)
p = Person(g)
for i in range(4):
    p.gun.fier(3)
p.fill(5)
for i in range(4):
    p.gun.fier(3)
"""
运行效果
开枪;还剩7发子弹
tu~
tu~
tu~
开枪;还剩4发子弹
tu~
tu~
tu~
开枪;还剩1发子弹
"""
Published 18 original articles · won praise 1 · views 942

Guess you like

Origin blog.csdn.net/JJ1M8/article/details/104812155