python 06.18
1.面向对象基础
面向对象编程(抽象)
对象:特征和技能的结合体
面向对象编程:对象之间的交互,优点:扩展性非常强;缺点:逻辑非常复杂
2.类与对象
类:一系列具有相同特征和技能的对象
在现实世界中先有对象,后有类
name:students1
id:210310810110
school:kmust
skills:running,Course_selection
name:students2
id:210310810111
school:kmust
skills:swimming,Course_selection
name:students3
id:210310810112
school:kmust
skills:swimming,Course_selection
name:teacher1
school:oldboy
skills:teaching,reading
name:teacher2
school:oldboy
skills:teaching,fishing
python 中先定义类,后衍生出对象
class 类名(驼峰体):#定义类,会直接运行类的代码
#定义一系列特征
#定义一系列方法
对象1 = 类名() #实例化出对象
3.定义对象特有的特征
class OldBoyStudent:
school = 'oldboy'
count = 0
def __init__(self,id,name,age=15):
self.id = id
self.name = name
self.age = age
OldBoyStudent.count += 1
def choose_course(self):
print('is choosing course')
stu1 = OldBoyStudent(222222,'朱瑞鑫')
print(OldBoyStudent.__dict__)age
print(stu1.id,stu1.name,stu1.)
4.属性查找顺序
先从对象本身查找,然后从类中查找,类中也没有则报错
5.类与对象的绑定方法
class OldBoyStudent:
school = 'oldboy'
def __init__(self,name,age):
self.name = name
self.age = age
def choose_course(self):
print(id(self))
print(self.school)
print(f'{self.name} is choosing course ')
stu1 = OldBoyStudent('a',1)
stu1.choose_course() #self是对象本身
print(id(stu1))
OldBoyStudent.choose_course(stu1)
6.类与数据类型
lis = list([1,2,3])
lis2 = list([1,2,3])
lis.append(4)
#list.append(lis,4)
7.对象的高度整合
对象会有一大推属性/数据,还会有一大推方法来操作这些属性,然后对于不同的对象,这些属性又不同。
class animal:
def __init__(self,species,height,weight)
self.species = species
self.height = height
self.weight = weight
def sleep(self):
print(sef.species,'睡着了')
def eat(self):
print(self.species,'开始吃饭了')
dog = animal('狗',50,50)
dog.eat()

浙公网安备 33010602011771号