一、类与对象
类即类别、种类,是面向对象设计最终要的概念,对象是特征和技能的结合体,而类则是一系列对象向此的特征与技能的结合体
在现实世界中:先有对象,再有类
在程序中,先定义类,后产生对象
# 注意:
1、类中可以有任意python代码,这些代码在类定义阶段就会执行
2、因而会产生新的名称空间,用来存放类的变量名与函数名,可以通过oldyboystudent._dict_查看
3、对于经典类来说我们可以通过该字典才操作类名称空间的名字,但python为我们提供专门的的语法
4、点是访问属性的语法,类中定义的名字都是类的属性
# 程序中类的用法
. : 专门用来访问属性,本质操作的就是_dict_
OldboyStudent.school # 等于经典类的操作OldboyStudent._dict_['school']
OldboyStudent.school='Oldboy' #等于经典类的操作OldboyStudent.__dict__['school']='Oldboy'
OldboyStudent.x=1 #等于经典类的操作OldboyStudent.__dict__['x']=1
del OldboyStudent.x #等于经典类的操作OldboyStudent.__dict__.pop('x')
1、如何基于面向对象的思想写程序
stu_name = "egon"
stu_age = 18
stu_gender = "male"
def choose(name, age, gender):
print('%s:%s:%s 正在选课' % (name, age, gender))
choose(stu_name,stu_age,stu_gender)
2、基于对象式的思想进行改写
def choose(stu_self):
print('%s:%s:%s 正在选课' % (stu_self["stu_name"], stu_self["stu_age"],stu_self["stu_gender"],))
stu_obj = {
"stu_name": "egon",
"stu_age": 18,
"stu_gender": "male",
"choose":choose
}
print(stu_obj["stu_name"])
stu_obj["choose"](stu_obj)
'''
学生对象1
数据:
名字 = "冯疯子"
年龄 = 18
性别 = "female"
学生对象2
数据:
名字 = "郭靖"
年龄 = 19
性别 = "male"
学生对象3
数据:
名字 = "大雕"
年龄 = 200
性别 = "male"
学生的类
相同的数据
学校 = "oldboy"
相同的功能
选课
'''
class Student:
school = "oldboy"
def choose(self):
print("正在选课")
stu_obj1=Student()
stu_obj2=Student()
stu_obj3=Student()
stu_obj1.name = "冯疯子"
stu_obj1.age = 18
stu_obj1.gender = "female"
stu_obj2.name = "郭靖"
stu_obj2.age = 19
stu_obj2.gender = "male"
stu_obj3.name = "大雕"
stu_obj3.age = 200
stu_obj3.gender = "male"
print(stu_obj1.__dict__)
print(stu_obj2.__dict__)
print(stu_obj3.__dict__)
二、初始化方法
class Student:
school = "oldboy"
def choose(self):
print("正在选课")
stu_obj1=Student()
stu_obj2=Student()
stu_obj3=Student()
stu_obj1.name = "冯疯子"
stu_obj1.age = 18
stu_obj1.gender = "female"
stu_obj2.name = "郭靖"
stu_obj2.age = 19
stu_obj2.gender = "male"
stu_obj3.name = "大雕"
stu_obj3.age = 200
stu_obj3.gender = "male"
class Student:
school = "oldboy"
def choose(self):
print("正在选课")
stu_obj1 = Student()
stu_obj2 = Student()
stu_obj3 = Student()
def init(obj, x, y, z):
obj.name = x
obj.age = y
obj.gender = z
init(stu_obj1, "冯疯子", 18, "female")
init(stu_obj2, "郭靖", 19, "male")
init(stu_obj3, "大雕", 200, "male")
class Student:
school = "oldboy"
def __init__(obj, x, y, z):
obj.name = x
obj.age = y
obj.gender = z
def choose(self):
print("正在选课")
stu_obj1 = Student("冯疯子", 18, "female")
stu_obj2 = Student("郭靖", 19, "male")
stu_obj3 = Student("大雕", 200, "male")
print(Student.__dict__)
print(stu_obj1.__dict__)
三、属性查找
class Student:
school = "oldboy"
def __init__(obj, x, y, z):
obj.name = x
obj.age = y
obj.gender = z
def choose(self):
print("%s 正在选课" %self.name)
stu_obj1 = Student("冯疯子", 18, "female")
stu_obj2 = Student("郭靖", 19, "male")
stu_obj3 = Student("大雕", 200, "male")
print(id(stu_obj1.school))
print(id(stu_obj2.school))
print(id(stu_obj3.school))
print(id(Student.school))
Student.school="xxx"
print(stu_obj1.school)
print(stu_obj2.school)
print(stu_obj3.school)
print(Student.choose)
print(stu_obj1.choose)
print(stu_obj2.choose)
print(stu_obj3.choose)
Student.choose(123123123)
stu_obj3.choose()
stu_obj2.choose()
stu_obj1.choose()