python-----面向对象
编程语言的发展:机器语言 汇编语言 高级语言(面向过程的c) 面向对象(c++ Java python)
类:对具有相同属性和方法的抽象
对象:具有属性和方法的实物
床前明月光 疑是地上霜 举头望明月 低头思故乡
class person():
def __init__(self,name,age,sex,height):
self.name = name
self.age = age
self.sex = sex
self.height = height
def say(self):
print(self.name,self.age,self.sex,self.height,"hello")
@staticmethod
def run():
print("跑步")
person.run()
p = person("啦啦啦",18,"男",180)
p.say()
print(p.name)
#__init__构造函数
#self即将要出现的那个对象,临时指向新创建的对象
#方法(3)类方法(2) 对象方法
#person.say(p)
class person():
def __init__(self,name,age,sex,height):
self.name = name
self.age = age
self.sex = sex
self.height = height
def say(self):
print(self.name,self.age,self.sex,self.height,"hello")
@staticmethod
def run():
print("跑步")
p = person("一",15,"女",150)
p.say()
#p.run()
面向对象的三大特性: 继承 封装 多态
class name():
def __init__(self,name,age,sex,height):
self.name = name
self.age = age
self.sex = sex
self.height = height
def say(self):
print(self.name,self.age,self.sex,self.height,"hello")
@staticmethod
def run():
print("跑步")
name.run()
#p = name("二",11,"男",111)
#p.say()
#p.run()
class cat():
def __init__(self,name,weight,age):
self.name = name
self.weight = weight
self.age = age
def eat(self):
print("吃饭")
def run(self):
print("跑步")
def sleeo(self):
print("睡觉")
class dog():
def __init__(self,name,weight,age):
self.name = name
self.weight = weight
self.age = age
def eat(self):
print("吃饭")
def run(self):
print("跑步")
def sleep(self):
print("睡觉")
'''
'''
class animal():
def __init__(self):
print("这是父类构造函数")
def __init__(self,name,weight,age):
self.name = name
self.weight = weight
self.age = age
print(id(self.name))
def eat(self):
print("吃饭")
def run(self):
print("跑步")
def sleep(self):
print("睡觉")
'''
'''
继承:减少代码量 ,耦合程度太高
高内聚 低耦合
构造函数:没有显示声明,系统会默认提供一个
子类使用父类属性时,必须手动调用父类函数
重写和覆盖:当父类方法不能满足子类需求时
多继承:从左到右,一层一层寻找
'''
'''
class cat(animal):
def __init__(self):
super().__init__("小孩",2,2)#父类
print("这是子类构造函数")
def eat(self):
print("吃猫粮")
c = cat()
print(id(c.name))
c.eat()
'''
'''
class gfather():
def fun(self):
print("参军")
class father0(gfather):
def __init__(self):
print("这是亲的")
#def fun0(self):
#print("当大官")
class father1(gfather):
def __init__(self):
print("这是干爹1")
#def fun(self):
#print("很多钱")
class father2(gfather):
def __init__(self):
print("这是干爹2")
#def fun(self):
#print("找媳妇")
class son(father0,father1,father2):
def __init__(self):
print("这是子类")
s = son()
s.fun()'''