8.26 (day23) and the process-oriented object-oriented, object classes and attributes and function, binding method, all objects are

Object-Oriented Programming

# 面向对象:
# 优点:可扩展性好
# 缺点:编写复杂
# 面向过程:
# 优点:复杂问题流程化,进而简单化
# 缺点:可扩展性差

Properties and function of the object class

# 类与对象之间的属性和参数
# 对象:属性与方法的结合体
# 类:一系列共同的属性和方法
# 类
# 获取类的属性和方法:
# 1.类.__dict__
# 类来调用属性和方法
# 2.1.通过dict来取(复杂,不用)
# 2.类名.属性/函数

# 先有类,再有对象

# 定义类(驼峰体)
# class Student:
#     school = 'oldboy'   #属性用变量表示
#     def choose(self):   #方法用函数表示
#         print('xuanke')
#     def study(self):
#          print('xuexi')
# 生成对象
# 类名加括号,生成对象
# Student()
# stu1 = Student()
# 获取属性和方法
# print(stu1.school)
# stu2 = Student()
# print(stu2.school)

# 调用方法
# 通过.获取
# print(stu1.choose())
# # print(stu2.study())
# print(Student.__dict__)   #类中的属性与函数
# print(Student.__dict__['school'])
# print(Student.__dict__['choose']())  #函数内存地址
# stu2 = Student.choose(123)
# print(stu2.school)

# 对象需要加参数,

# 获取对象的属性和方法
# 1.对象.__dict__
# 对象来调用属性和方法   对象调用方法:第一个不用传参
# 2.对象.属性/f方法
# 定义类产生对象
# class 类名;
# 产生对象
# 对象 = 类名()


# 产生对象
# 绑定对象,属性的查找顺序
# class Student:
#     school = 'oldboy'
#     def choose(self):
#         print('xuanke')
#     def study(self):
#         print('xuexi')
# stu1 = Student()
# stu1.name = 'nick sb'
# print(stu1.__dict__)
# stu1.school = 'oldgirl'
# print(stu1.__dict__)
# print(stu1.school)

# 属性查找顺序:先从自身找,找不到去类中找,没有报错

# 类的属性修改,对象属性也修改

# 对象中放属性:
# 第一种方式:实例化后放入
# stu1 = Student()
# stu1.name = 'nick_sb'
# 第二种:使用__init__方法
# class Student:
#     school = 'oldboy'
#     def __init__(self,name):
#         self.name = name
#     def choose(self):
#         print('xuanke')
#     def study(self):
#         print('xuexi')
# stu1 = Student('nick')
# print(stu1.name)
# 实例化产生对象会自动调用init方法完成对象的初始化
# stu1 = Student('1')
# Student.__init__(stu1,'nick')
# print(stu1.name)

Binding approach

# 绑定方法:
# 定义在类内部的方法:
# 如果类调用:就是一个普通函数,有几个参数就需要几个传参
# 对象来调用,它叫对象的绑定方法,第一个参数不需要传,自动传递

Everything is an object

一切皆对象
# 所有数据都是

Methods and Function

# 方法与函数形参,穿多少实参
# def 定义函数有多少
# 方法第一个参数可以不用传

Guess you like

Origin www.cnblogs.com/jiann/p/11529092.html