day19 summary

class

面向过程编程:
    核心是“过程”二字,过程指的是做事情的步骤,即先做什么再做什么
    基于该编程思想编写程序,就好比一条工厂流水线,一种机械式的思维方式。
    优点:
        逻辑清晰,复杂的问题流程化,进而简单化。

    缺点:
        可扩展性差。

面向对象编程:
    核心是“对象”二字,对象指的是特征与技能的结合体。
    基于该编程思想编写程序,就好比在创造世界,一种上帝式的思维方式。

    优点:
        可扩展性高。

    缺点:
        编写程序的复杂程度要远高于面向过程编程思想。

优先使用面向过程还是面向对象?
    面向过程结合面向对象一起使用。
    # 选课系统项目。

如何造对象?首先需要学习类。

什么是类?
    类型、类别、分类。

    先定义类,后调用类产生对象。

    - 在现实世界中:
        对象是一个个具体存在的事物,类是由人类文明的发展抽象总结出来的。

    - 在程序中:
        必须遵循,先有类,再有对象。

    如何定义类:
        1.先从现实世界中,基于一个个的对象,抽取出类。
        2.再定义类。

    类: 对象是特征与技能的结合体,类是一系列对象相同的特征与技能的结合体。
类:
    对象是特征与技能的结合体,类是一系列对象相同的特征与技能的结合体。

如何定义类:
    首定义类,后调用类创造对象。

定义类的语法:
    def 函数名():

    class 类名:
        对象相同的特征
        对象相同的技能

    class: 用来定义类的,class后跟的是类名。
    类名: 类的名字,用来调用创造对象的。

    注意: 类名必须要遵循驼峰命名法,python在定义变量与函数时不推荐使用,但在定义类时推荐使用。

    在定义类发生的事情:
        1.类在定义时,会产生一个空的名称空间。
        2.会把类内部所有名字,扔进类的名称空间中。
        注意: 类在定义阶段就已经产生好了名称空间,执行python文件时会执行类内部的代码。


学生类:
    相同特征 --> 类内部的变量
        school

    相同技能 --> 类内部的函数
        learn
        choose_course
# # 学生类
# class OldboyStudent:
#     # 特征
#     school = 'oldboy'
#     print('tank is handsome...')
#
#     # 技能
#     def learn(self):
#         print('learn python...')
#
#     def choose_course(self):  # self = None
#         print('choose_course ....')


# OldboyStudent ---> 会指向类的名称空间
# print(OldboyStudent.__dict__)  # 查看类的名称空间内所有名字
# print(OldboyStudent.__dict__.get('school'))  # oldboy

# 查
# print(OldboyStudent.__dict__.get('school'))

# 改
# OldboyStudent.__dict__['school'] = 'oldgirl'
# print(OldboyStudent.__dict__.get('school'))

# 增
# OldboyStudent.__dict__['address'] = 'SH'
# print(OldboyStudent.__dict__['address'])

# 删
# del OldboyStudent.__dict__['school']
# print(OldboyStudent.__dict__['school'])


'''
类.特征或技能的方式对类内部的名字进行修改。
"."会指向类内部的名称空间,从而获取名字。
'''
# 查
# print(OldboyStudent.school)  # oldboy

# 改
# OldboyStudent.school = 'oldgirl'
# print(OldboyStudent.school)

# 增
# print(OldboyStudent.address)  # 报错,特征也称之为属性
# OldboyStudent.address = '上海'
# print(OldboyStudent.address)

# 删
# del OldboyStudent.address
# print(OldboyStudent.address)

# OldboyStudent.choose_course(123)

# 以上是类的操作。

Objects

How to generate objects:
Syntax:
class name + () call generates an object class.

Produced namespace:
1. The name of the class in the class definition phase space has been created.
2. The name of the object space, resulting in the calling class.

 # 学生类
# class OldboyStudent:
#     # 特征
#     school = 'oldboy'
#     print('tank is handsome...')
#
#     # 技能
#     def learn(self):
#         print('learn python...')
#
#     def choose_course(self):  # self = None
#         print('choose_course ....')
# stu1 = OldboyStudent()  # OldboyStudent()---> 学生对象
# stu2 = OldboyStudent()  # OldboyStudent()---> 学生对象
# stu3 = OldboyStudent()  # OldboyStudent()---> 学生对象
# # print(stu1, stu2, stu3)
# print('stu1', stu1.school)
# stu1.learn()
# stu1.choose_course()
# print('stu2', stu2.school)
# stu2.learn()
# stu2.choose_course()
# print('stu3', stu3.school)
# stu3.learn()
# stu3.choose_course()




# 问题: 对象与对象之间的特征与技能一样。
'''
# 解决: 在类内部定义__init__函数。

# 中文翻译: 为对象初始化某些属性。
__init__: 会在调用类时,自动触发该函数。
'''


# 学生类
class OldboyStudent:
    # 若__init__中有self以外的参数,调用类时必须在括号内传入参数。
    def __init__(self, x, y, z):  # self --> 就是对象本身  x = 高峰峰  y=93   z=female
        # print(self, x, y, z)
        # print('此处时__init__。。。。')
        # 给对象添加特征的方式二:
        self.name = x
        self.age = y
        self.sex = z

    # 特征
    school = 'oldboy'
    # print('tank is handsome...')

    # 技能
    def learn(self):
        print('learn python...')

    def choose_course(self):  # self = None
        print('choose_course ....')


stu1 = OldboyStudent('高峰峰', 93, 'female')  # OldboyStudent()---> 学生对象
stu2 = OldboyStudent('小丁丁', 84, 'female')  # OldboyStudent()---> 学生对象
stu3 = OldboyStudent('大丁丁', 18, 'male')  # OldboyStudent()---> 学生对象

# print(stu1)
# print(stu2)
# print(stu3)

# 给对象添加属性的方式一:
# print(stu1.name)
# stu1.name = '高峰峰'
# stu1.age = 93
# stu1.sex = 'female'
# print(stu1.name, stu1.age, stu1.sex)

Call the class of things happen:
1. The first will produce an empty object is to have a "name space object."
2. automatically triggers __init__.
3. will object itself and the parameters within the brackets together to pass __init__ function.
Summary: call the class will produce an object instance of a class called procedure call class is called the class is instantiated, the resulting object.

# 查看类的名称空间
# print(OldboyStudent.__dict__)

# 查看对象的名称空间
# print(stu1.__dict__)
print(stu1.name, stu1.school)

Find the order of objects and classes

class OldboyStudent:
    SCHOOL = 'oldboy'
    NAME = 'DDDDDDDD'

    def __init__(self, name, age, sex, school):
        self.name = name
        self.age = age
        self.sex = sex
        self.SCHOOL = school


stu1 = OldboyStudent('小正正', 17, 'male', 'oldgirl')
print(stu1.SCHOOL)


'''
对象与类的查找顺序:
    1.对象.属性,若对象本身有,则优先查找对象自己的。
    2.若对象本身没有,则去类里面找,若类没有,则报错。
'''

# print(stu1.NAME)
# print(stu1.AGE)
# print(OldboyStudent.AGE)

Special about object binding method

: Internal function to the object class is mainly used
to call functions in a class by the class 1, the function is a normal function,
normal function will receive several parameters have passed several parameters.

2. by an object to invoke binding method called objects,
different object calls the bound method, it will be a different object passed in the binding process.

Binding method of an object, the object is invoked,
so special is the object as the first argument passed to the method.

class OldboyStudent:
    def __init__(self, name, age, sex):
        self.name = name
        self.age = age
        self.sex = sex
    # 特征
    school = 'oldboy'
    # 技能
    def learn(self):  # 普通函数
        # print('stu:', self)
        print('learn python...')

    def choose_course(self, course):  # self =
        print(f'[{self.name}]choose_course {course} ....')

# print(OldboyStudent.learn)  # function
# OldboyStudent.learn(123)


stu1 = OldboyStudent('高凤凤1', 73, 'female')
stu2 = OldboyStudent('高凤凤2', 84, 'male')

# 对象之间与类的方法功能是一样的
# OldboyStudent.learn(123)
# stu1.learn()
# print(OldboyStudent.learn)
# print(stu1.learn)
# print(stu2.learn)

stu1.choose_course('python')  # choose_course(self, 'python')
stu2.choose_course('linux')  # choose_course(self, 'python')

Everything in python objects

int, float, str, list, tuple, dict, set, bool in python everything is an object.

# 定义一个列表
list1 = [1, 2, 3]  # list([1, 2, 3])
print(list1)  # [1, 2, 3]
print(id(list1))  # 2738050243464
print(type(list1))  # <class 'list'>


# print(list)
# list1.append(1)


class Foo:
    def append(self, x):
        print(x)
        pass


# print(Foo)
# obj = Foo()
# obj.append(1)

f = float()
s1 = str()
l = list()
t = tuple()
d = dict()
s2 = set()
b = bool()

print(type(f))
print(type(s1))
print(type(l))
print(type(t))
print(type(d))
print(type(s2))
print(type(b))
# print(filter)
# print(map)

People dog fight

'''
需求:
    人对象
    狗对象
    人狗互咬,直到一方生命值为0,则程序结束.

定义两个类:
    人类
    狗类
'''


# 在现实世界中
'''
对象人1:
    特征:
        name = '高冯冯'
        aggr = 100
        life = 500
    
    技能:
         咬狗
         def bite()
对象人2:
    特征:
        name = '小丁丁'
        aggr = 200
        life = 1000
    
    技能:
         咬狗
         def bite()
         
    相同特征:
        
    相同技能:
        bite
         
         
对象狗1:
    特征:
        name = 'nick'
        dog_type = '哈士奇'
        aggr = 200
        life = 300
    
    技能:
        咬人
        bite
        
对象狗2:
    特征:
        name = 'jason'
        dog_type = '泰日天'
        aggr = 500
        life = 100
    
    技能:
        咬人
        bite    
        
        
    相同特征:
    相同技能:
        bite
'''

class People:
    def __init__(self, name, aggr, life):
        # 对象特有的特征
        self.name = name
        self.aggr = aggr
        self.life = life

    # 人咬狗方法
    def bite(self, dog):
        if dog.life <= 0:
            return True

        if self.life:
            # 狗掉血
            dog.life -= self.aggr
            print(
                f'''
                人: [{self.name}]开始咬狗:[{dog.name}]
                狗掉血: [{self.aggr}] 
                狗还剩血量: [{dog.life}]
                ''')


class Dog:
    def __init__(self, name, dog_type, aggr, life):
        self.name = name
        self.dog_type = dog_type
        self.aggr = aggr
        self.life = life

    # 狗咬人方法
    def bite(self, people):
        if people.life <= 0:
            return True

        if self.life:
            # 人掉血
            people.life -= self.aggr

            print(
                f'''
                狗: [{self.name}]开始咬人:[{people.name}]
                人掉血: [{self.aggr}] 
                人还剩血量: [{people.life}]
                ''')


p1 = People('egon', 200, 1000)
d1 = Dog('nick', '哈士奇', 500, 400)

while True:

    flag1 = p1.bite(d1)

    # print(flag1)
    if flag1:
        break

    flag2 = d1.bite(p1)
    if flag2:
        break

1:
process-oriented programming:
the core is the "process" the word refers to the process step is to do things that do first what to do what
to write programs based on the programming ideas, like a factory assembly line, a mechanical way of thinking .
Advantages: logical, process of complex issues, and further simplification.
Disadvantages: poor scalability.
Object-oriented programming:
the core of the "object" word, with the features of the object refers to a combination of skills.
Write a program based on the programming ideas, like the creation of the world a God-like way of thinking.
Advantages: high scalability.
Disadvantages: the complexity of the programming is much higher than the process-oriented programming ideas.

2:
what happened in the definition of the class:
1. The class definition, generates an empty namespace.
2. All classes will internal name, namespace thrown into the class.
Note: In the class definition phase has produced good name space, will execute the code inside the class when executing python file.

3:
things (******) call the class occurs:
1. First will produce a null object is to have a "name space object."
2. automatically triggers __init__.
3. will object itself and the parameters within the brackets together to pass __init__ function.
Summary: call the class will produce an object instance of a class called procedure call class is called the class is instantiated, the resulting object.

4:
the init : When you call class will automatically trigger the function.

5:
1. The class function call inside the class, but ordinary function call.
Binding special about 2. The method object (*******):

Invoked by the object, would the object as the first argument passed to the method
with the object to call, it will put a different object to the different binding methods.

Guess you like

Origin www.cnblogs.com/zhm-cyt/p/11642127.html