类与对象的定义与使用

'''
面向过程与面向对象
面向过程:核心是过程二字,过程即解决问题的步骤,就是先干什么再干什么
基于该思想写程序就好比在设计一条流水线,是一种机械化的思维方式
优点:复杂的过程流程化,进而简单化
缺点:扩展性差
面向对象:核心是对象二字,对象是特征与技能的结合体
基于该思想编写程序就好比在创造一个世界,世界是由一个个对象组成,是一种“上帝式”的思维
优点:可扩展性强
缺点:编程复杂高,容易出现过度设计
'''
'''

对象是特征与技能的结合体,类就是一系列对象相似的特征与技能的结合体
在现实世界中:一定是先有的一个个具体存在的对象,后总结出的类
在程序中:一定是先定义类,后产生对象
'''
# class Student: # 程序中定义类,用class,后面写类名,类名首字母要大写
# '''
# 注释信息
# '''
# school='oldboy'
# def learn(self):
# print('is learning')
# def choose_course(self):
# print('choose course')
# print(Student) # <class '__main__.Student'>
# class Student:
# school='oldboy'
# def learn(self):
# print('is learning')
# def choose_course(self):
# print('choose course')
# print('>>:')
# print(Student.__dict__)
# print(Student.school) # 数据属性
# print(Student.learn) # 函数属性
'''
>>: # 类体代码在类的定义阶段就会立刻执行
{'__module__': '__main__', 'school': 'oldboy', 'learn': <function Student.learn at 0x000001DC081A4D38>, 'choose_course': <function Student.choose_course at 0x000001DC081A4DC8>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None}
oldboy
<function Student.learn at 0x0000027085F04D38>
'''
# class Student:
# school='oldboy'
# def learn(self):
# print('is learning')
# def choose_course(self):
# print('choose course')
# Student.country='China'
# print(Student.__dict__)
# print(Student.country)
'''
{'__module__': '__main__', 'school': 'oldboy', 'learn': <function Student.learn at 0x000001BB79AB4D38>, 'choose_course': <function Student.choose_course at 0x000001BB79AB4DC8>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None, 'country': 'China'}
China
'''
# class Student:
# school='oldboy'
# def learn(self):
# print('is learning')
# def choose_course(self):
# print('choose course')
# Student.school='Oldboy'
# print(Student.school)
'''
Oldboy
'''
# class Student:
# school='oldboy'
# def learn(self):
# print('is learning')
# def choose_course(self):
# print('choose course')
# del Student.country
# print(Student.country) # 报错

# class Student:
# school='oldboy'
# def learn(self):
# print('is learning')
# def choose_course(self):
# print('choose course')
#
# def func():pass
# print(func)
# print(Student.learn)
'''
<function func at 0x000002DC14584D38>
<function Student.learn at 0x000002DC14584DC8>
'''
# Student.learn('xxxxx')
'''
is learning
'''
# class Student:
# school='oldboy'
# def learn(self):
# print('is learning')
# def choose_course(self):
# print('choose course')
'''
调用类的过程又称为实例化
得到一个返回值,即对象,该对象是一个空对象
来触发Student.__init__()
'''
# stu1=Student()
# stu2=Student()
# stu3=Student()
# print(stu1,stu2,stu3)
'''
<__main__.Student object at 0x00000227D0CA23C8> <__main__.Student object at 0x00000227D0CA2408> <__main__.Student object at 0x00000227D0CA2448>
'''
# class Student:
# school='oldboy'
# def __init__(self,name,age,gender):
# self.Name=name
# self.Age = age
# self.Gender=gender
# def learn(self):
# print('is learning')
# def choose_course(self):
# print('choose course')
# # stu1=Student('李三炮','18','男')
# # print(stu1.__dict__) # {'Name': '李三炮', 'Age': '18', 'Gender': '男'}
# # print(stu1.Name,stu1.Age,stu1.Gender) # 李三炮 18 男
# stu2=Student('张美丽','18','女')
# stu3=Student('二狗子','18','男')
# # print(stu2.__dict__)
# # print(stu3.__dict__)
# '''
# {'Name': '张美丽', 'Age': '18', 'Gender': '女'}
# {'Name': '二狗子', 'Age': '18', 'Gender': '男'}
# '''
# print(stu2.Name) # 张美丽
原文地址:https://www.cnblogs.com/0B0S/p/12063511.html