python---面向对象

本文深入讲解了Python面向对象编程的基本概念,包括类、对象、继承、封装和多态等核心特性。通过具体代码示例,介绍了如何定义类,创建对象,以及如何实现类的方法和属性。同时,探讨了面向对象编程的三大特性,并通过实例展示了如何在Python中实现这些特性。

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()'''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值