2019.8.26 learning content and notes

summary

  1. Object-oriented and object-oriented programming
    concepts:

Process-oriented

Advantages: the process of complex issues, and further simplification of
disadvantages: poor scalability

Object-Oriented

Advantages: high scalability

Cons: write complex

  1. Classes and Objects

The definition of class

class Keyword class name:

​ pass

Produced objects

Object class name = ()

-class:

  1. And get property methods of the class

    Class ._ _ dict_ _

  2. Call the properties and methods of the class

    1. dict be taken by (complex, no)

    2. The name of the class attribute / function

- Object:

  1. Gets the object properties and methods

    Objects. _ _ Dict _ _

    2.  对象来调取属性和方法

    Object Property / Method

3 generates an object (binding method, to find the order of attributes)

- Properties Find Properties

- start looking object itself ---------> class find ---------> error

- Object Properties Fu

- One way:

​ stu1=student()
​ sru1.name='nick sb'

- Second way: through _ _init __

- The method is defined in the class, method of write parameters

- when an object instance of the object is generated, the values ​​in parentheses pass, may be passed in _ _ init_ _

- binding method:

- is defined within the class

- object to call, it will automatically pass over the object, you can modify the object inside the method

'''
对象:属性方法的结合体
类:一系列共同的属性和方法

现实生活中:先有对象再有类
程序中:先有类再有对象
'''

'''
学生1:
    学习:老男孩
    姓名:李铁蛋
    性别:女
    年龄:18
    方法:
        选课
        学习
学生2:
    学习:老男孩
    姓名:Nick
    性别: 男
    年龄:48
    方法:
        选课
        学习  
'''
# 定义类(类型建议用驼峰体命名)
# class关键字 类名:
# class Student:
#     #变量表示属性
#     school = 'oldboy'
#     def choose(self):
#         print("选课")
#     def study(self):
#         print("学习")
# 生成对象  类加括号,生成对象
#stu1=Student()
# 获取对象属性和方法,通过 . 获取
# print(stu1.school) #oldboy
# print(stu1.choose)#<bound method Student.choose of <__main__.Student object at 0x000000000295F0B8>>(内存地址)

#查看类中的属性和函数
#print(Student.__dict__)
# print(Student.__dict__['school'])#oldboy
# print(Student.__dict__['choose'])#<function Student.choose at 0x0000000002996620>
# Student.__dict__['choose'](123)#123代表方法传的参数
# Student.choose(123)##123代表方法传的参数
# print(Student.school)#oldboy

#获取对象的属性和方法
class Student:
    #变量表示属性
    school='oldboy'
    def choose(self):
        print("选课....")
    def study(self):
        print('学习')

#stu1=Student()
# print(stu1.__dict__)#{}
# print(stu1.school)#oldboy
#对象来调用方法,第一个参数不用传
#print(stu1.choose)#<bound method Student.choose of <__main__.Student object at 0x000000000297F080>>

#stu2=Student()
#print(stu2.school)#oldboy
#对象自己属性和方法

# stu1=Student()
# stu1.name='nick'
# stu1.school='新东方'
# print(stu1.name)#nick
# print(stu1.school)#新东方
# print(stu1.__dict__)#{'name': 'nick', 'school': '新东方'}

#属性查找顺序:先从对象自身找----->类中找----->没有就报错
# Student.school='xxx'
# stu1=Student()
# print(stu1.school)#xxx

#向对象中放属性
#第一种方式:
# stu1=Student()
# stu1.name='nick'

#第二种方式:通过__init__方法
class Student:
    #变量表示属性
    school='oldboy'
    def __init__(self,name):
        self.name=name
    def choose(self):
        print("选课....")
    def study(self):
        print("学习")

#产生对象
#stu1=Student()没有参数报错
#stu1=Student('nick')
# print(stu1.name)#nick
#内部帮我们做了一些事 :当我在实例化产生对象的时候,会自动调用__init__方法,完成对象的初始化
#print(stu1.name)#nick

# stu2=Student('jason')
# print(stu2.name)#jason

def change(obj,name):
    obj.name=name

stu1=Student('xxx')
print(stu1.name)#xxx
Student.__init__(stu1,'nick')
change(stu1,'nick')
print(stu1.name)#nick

stu2=Student('李铁蛋')
print(stu2.name)#李铁蛋
#绑定方法:
#定义在类内部的方法
#如果类来调用:就是一个普通函数,有几个参数就要传几个参数
#对象来调用:它叫对象的绑定方法,第一个参数不需要传,自动传递
class Student:
    #变量表示属性
    school='oldboy'
    def __init__(self,name):
        self.name=name
    def choose(self):
        print("选课")
    def study(self):
        print("%s学会了python"%self.name)
stu1=Student('nick')
stu1.study()#nick学会了python
stu2=Student('李铁蛋')
stu2.study()#李铁蛋学会了python

4 Everything Object

#在Python中,字典,列表字符串...都是对象
# 类即类型(type)
#list=[1,2,3]
#类实例化产生对象
list1=list([1,2,3])
list2=list([5,6,7])
#对象调用对象的绑定方法,修改对象自己
list1.append(5)
print(list1)#[1, 2, 3, 5]

list.append(list1,9)
print(list1)#[1, 2, 3, 5, 9]

class Student:
    #变量表示属性
    school='oldboy'
    def school(self):
        print('选课')
    def study(self):
        print('学习')
a=Student()
print(type(a))#<class '__main__.Student'>

5 small practice

People and dogs shootout

#人狗大战
#定义一个狗类
class Dog:
    type_dog='藏獒'
    def __init__(self,name,aggressivity,hp=100):
        self.name=name
        self.aggressivity=aggressivity
        self.hp=hp
    def bite(self,target):

        target.hp-=self.aggressivity
        print('''
        狗的品种:%s
        %s狗咬了一下%s人,
        人掉血:%s
        人的血剩余:%s'''%(self.type_dog,self.name,target.name,self.aggressivity,target.hp))

#人类
class Human:
    def __init__(self,name,aggressivity,hp=100):
        self.name=name
        self.aggressivity=aggressivity
        self.hp=hp
    def bite(self,target):
        target.hp-=self.aggressivity
        print('''
        %s人咬了一下%s狗,
        狗掉血:%s
        狗的剩余血量:%s'''%(self.name,self.aggressivity,target.hp,target.name))

#实例化产生狗对象
dog=Dog('旺旺财',10,200)
nick=Human('nick',50)

dog.bite(nick)
print(nick.hp)#90

nick.bite(dog)
print(dog.hp)#150

Guess you like

Origin www.cnblogs.com/chmily/p/11420842.html