python面向对象-学习笔记

面向对象

面向对象&面向过程

面向过程——步骤化

依次分析实现需求的步骤,通过函数一步步实现

优点:性能较好

缺点:不易维护、拓展、复用

面向对象——行为化

把整个需求按功能划分,将有共性部分封装成类(类经过实例化后是对象)。创建对象不是为了完成单一步骤,而是描述在解决问题中的行为

优点:易维护、拓展、复用

缺点:性能较面向过程更低

类和对象

类是一个模板,对象是一个类的具体事例

class  Person:
    pass
xiao=Person()

类的构造

#构造方法: def __init__()
class Person:
    def __init__(self,name,age):   #属性
        self.name=name
        self.age=age
    def intro(self):       #方法
        print(f"{self.name}'s age is {self.age}")

对象的属性

class Person:
    def __init__(self,name,age):
        self.name=name
        self.age=age
stu1=Person("xiaomin",12)
stu2=Person("huihui",16)
可以给已有属性赋值,也可以新加属性
stu2.age=10
stu2.score="good"

对象的行为(方法)

class Person:
    def __init__(self,name,age):   
        self.name=name
        self.age=age
    def intro(self):
        print(f"{self.name}'s age is {self.age}")
stu1=Person("xiaomin",12)
stu2=Person("huihui",16)
stu1.intro()
stu2.intro()

最重要的三个特性

封装

决定属性和行为归属谁的问题,并隐藏对象中一些不希望被外部访问到的属性或方法,确保数据安全

#当有些东西不想被外界获得、修改,在成员属性或者成员方法前面加上__
class Account:
    def __init__(self,name,password):
        self.__pas=password
        self.name=name
    def __getpas(self):
        print(f"{self.name}'s password is {self.__pas}")
user1=Account("su",123)
print(user1.pas)
user1.getpas()

##>>> AttributeError: 'Account' object has no attribute 'pas'

若想访问私有属性、私有方法

class Account:
    def __init__(self,name,password):
        self.__pas=password
        self.name=name
    def __getpas(self):
        print(f"{self.name}'s password is {self.__pas}")
user1=Account("su",123)
print(user1._Account__pas)
user1._Account__getpas()

##>>> 123
#     su's password is 123

继承

让子类具有父类的特性,提高了代码的重用性,多个子类的相同代码可以放在父类中。在原有父类设计不变的情况下,可以增加新的功能,或改进已有的算法。

重写之覆盖
class Animal:
    def __init__(self,name):
        self.name=name
    def drink(self):
        print("喝")
    
class dog(Animal):
    def drink(self):
        print(f"{self.name}喝水")

wancai=dog("wancai")
wancai.drink()                  ### >>> wancai喝水
重写之拓展

使用super().父类方法 来调用父类方法的执行

class Animal:
    def __init__(self,name):
        self.name=name
    def drink(self):
        print("喝")
    
class dog(Animal):
    def drink(self):
        super().drink()
        print(f"{self.name}喝水")

wancai=dog("wancai")
wancai.drink()           ### >>> 喝
                         #       wancai喝水

多态

定义:指对象可以有多种形态(多态是方法的多态)

优点:不同的对象调用相同的方法,产生不同的执行结果,增加代码的灵活度

前提:以继承和重写父类方法为前提

class Fruit:
    def __init__(self,name):
        self.name=name
    def say_color(self):
        print(f"{self.name} is color")
class Apple(Fruit):
    def say_color(self):
        print(f"{self.name} is red")
class Orange(Fruit):
    def say_color(self):
        print(f"{self.name} is orange")    
class Banana(Fruit):
    def say_color(self):
        print(f"{self.name} is yellow")    
def what_color(fruit):
    if isinstance(fruit,Fruit):
        fruit.say_color()
        fruit.say_color()
f1=Apple("apple")
what_color(f1)
###>>> apple is red
#      apple is red
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值