类和对象基础,js的基础对象有哪些
类和对象基础,js的基础对象有哪些
目录
- 面向对象编程介绍
- 面向过程编程
- 面向对象编程
- 类和对象
- 定义类
- 对象的创建和使用
- 对象与类的查找顺序
- 对象绑定方法
- 小练习
面向对象编程介绍
面向过程编程
核心是“过程”二字,过程指的是做事情的步骤,即先做什么再做什么。
基于该编程思想写程序,就好比一条工厂流水线,一种机械式的思维方式。
优点:逻辑清晰,复杂的问题流程化,进而简单化。
缺点:可扩展性差。
面向对象编程
面向对象编程的核心是对象二字,对象指得是特征与技能的结合体。基于该编程思想写程序,就好比在创造世界,一种上帝式的思维方式。
优点:可扩展性高。
缺点:编写程序的复杂程度远高于面向过程。
类和对象
什么是类?类型,分类
先定义类,后调用类产生对象。
在现实世界中:对象是一个个具体存在的事物,类是由人类文明的发展抽象总结出来的。
在程序中:必须遵循先有类,再有对象
总结:
对象是特征与技能的结合体,类是一系列对象相同的特征与技能的结合体。
定义类
# 类名使用驼峰命名法
class Student:
school = 'oldboy'
def choose_course(self):
print('choose course')
def learn(self):
print('learn')
在定义类发生的事情:
注意:类在定义阶段就已经产生好了名称空间,执行python文件时会执行类内部的代码
class Student:
school = 'oldboy'
def choose_course(self):
print('choose course')
def learn(self):
print('learn')
print(Student.__dict__) # 查看类的名称空间中所有名字
'''
{'__module__': '__main__', 'school': 'oldboy', 'choose_course': <function Student.choose_course at 0x0000010EF2FD3510>, 'learn': <function Student.learn at 0x0000010EFA1A0950>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None}
'''
对象的创建和使用
类的操作
class Student:
school = 'oldboy'
def choose_course(self): # self就是对象本身
print('choose course')
def learn(self):
print('learn')
print(Student.school) # 查找
Student.school = 'oldgirl' # 修改
print(Student.school)
'''
oldgirl
'''
Student.address = '上海' # 增加
print(Student.address)
del Student.address # 删除
Student.choose_course(123) # 括号里面需要参数
名称空间的产生:
class Student():
school = 'oldboy'
def choose_course(self):
print('choose course')
def learn(self):
print('learn')
stu1 = Student()
stu2 = Student()
stu3 = Student()
print(stu1.school)
stu1.choose_course()
stu1.learn()
在类内部定义__init__
函数
__init__
会在调用类时自动触发该函数,为对象初始化某些属性
class Student():
school = 'oldboy'
def __init__(self):
print('此处是__init__')
def choose_course(self):
print('choose course')
def learn(self):
print('learn')
stu1 = Student()
stu2 = Student()
stu3 = Student()
print(stu1.school)
stu1.choose_course()
stu1.learn()
调用类发生的事情:
class Student():
school = 'oldboy'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def choose_course(self):
print('choose course')
def learn(self):
print('learn')
stu1 = Student('cwz', 23, 'male')
print(stu1.name)
print(stu1.age)
print(stu1.sex)
'''
cwz
23
male
'''
对象与类的查找顺序
class Student():
school = 'oldboy'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def choose_course(self):
print('choose course')
def learn(self):
print('learn')
stu1 = Student('cwz', 23, 'male')
print(stu1.__dict__)
print(stu1.name, stu1.school)
'''
{'name': 'cwz', 'age': 23, 'sex': 'male'}
cwz oldboy
'''
class Student():
school = 'oldboy'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def choose_course(self):
print('choose course')
def learn(self):
print('learn')
stu1 = Student('cwz', 23, 'male')
print(stu1.__dict__)
print(stu1.name, stu1.school)
'''
{'name': 'cwz', 'age': 23, 'sex': 'male'}
cwz oldboy
'''
对象与类的查找顺序:
对象绑定方法
class Student():
school = 'oldboy'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
print(self)
def choose_course(self):
print('choose course')
def learn(self):
print('learn')
# print(Student.learn)
# Student.learn(133)
stu1 = Student('qwe', 20, 'male')
stu1.learn()
class Student():
school = 'oldboy'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
print(self)
def choose_course(self):
print('choose course')
def learn(self):
print('learn')
# print(Student.learn)
# Student.learn(133)
stu1 = Student('qwe', 20, 'male')
stu1.learn()
类内部的函数主要是给对象用的:
class Student():
school = 'oldboy'
def __init__(self, name, age, sex):
self.name = name
self.age = age
self.sex = sex
print('stu:', self)
def choose_course(self):
print('choose course')
def learn(self):
print('learn')
# print(Student.learn)
# Student.learn(133)
stu1 = Student('qwe', 20, 'male')
stu2 = Student('cwz', 21, 'male')
stu1.learn()
print('stu1', stu1)
stu2.learn()
print('stu2', stu2)
print(stu1.learn)
'''
stu: <__main__.Student object at 0x0000019E249E2128>
stu: <__main__.Student object at 0x0000019E249E2198>
learn
stu1 <__main__.Student object at 0x0000019E249E2128>
learn
stu2 <__main__.Student object at 0x0000019E249E2198>
<bound method Student.learn of <__main__.Student object at 0x0000019E249E2128>>
'''
小练习
'''
人狗互咬大战
'''
class Person:
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
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, person):
if person.life < 0:
return True
person.life -= self.aggr
print(
f'''
狗 {self.name}咬人 {person.name}
人掉血 {self.aggr}
人还剩的血量 {person.life}
'''
)
p1 = Person('neo', 300, 400)
d1 = Dog('大黄', '土狗', 200, 500)
while True:
flag1 = p1.bite(d1)
if flag1:
break
flag2 = d1.bite(p1)
if flag2:
break
'''
人狗互咬大战
'''
class Person:
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
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, person):
if person.life < 0:
return True
person.life -= self.aggr
print(
f'''
狗 {self.name}咬人 {person.name}
人掉血 {self.aggr}
人还剩的血量 {person.life}
'''
)
p1 = Person('neo', 300, 400)
d1 = Dog('大黄', '土狗', 200, 500)
while True:
flag1 = p1.bite(d1)
if flag1:
break
flag2 = d1.bite(p1)
if flag2:
break
相关内容
- 暂无相关文章
评论关闭