python 面向对象 多态 封装

什么是多态?

一个类表现出的多种状态,通过继承来实现

例如Animal类表现出了Dog,Cat两种形态

class Animal:pass
class Dog(Animal):pass
class Cat(Animal):pass

wc = Dog()

 

def func(name,age):
    print(name,age)
func(1,2)
class Payment:pass
class ApplePay(Payment):
    def pay(self):pass
class Alipay(Payment):
    def pay(self):pass

def pay(obj,money):    #在python中无需指定传输数据类型,随意传任意类型
    obj.pay()

app = ApplePay()
print(type(app))
pay(app,123)

多态在java中的表现:

在一个函数中需要给参数指定数据类型,如果这个地方可以接收两个以上类型的参数,那么这些类型应该有一个父类,这个父类是所有子类对象的类型。

def func(Cat mao):pass
def func(Dog gou):pass
def func(Animal gou|mao):pass

多态在python中的表现:

函数的参数不需要指定数据类型,所以我们也不需要通过继承的形式来统一一组类的类型,换句话说 所有的对象其实都是object类型,所以在python当中其实处处是多态

 

鸭子类型:

所有的对象都是鸭子类型,不是明确的通过继承实现的多态,而是通过一个模糊的概念来判断这个函数能不能接受这个类型的参数。

 

 什么是封装?

广义上的封装:把属性函数都放到类里

class 类名:
    def  方法(self):pass

是为了仅此类的对象才能使用定义在类中的方法。

 

狭义上的封装:定义私有成员

把一个名字(私有的静态变量)藏在类中:

class Goods:
    __discount = 0    # 私有的静态变量:在python中用双下划线开头的方式将属性隐藏起来
print(__discount) print(Goods.__discount) #此行运行会报错,因为在类的外部不能引用私有的静态变量 

 

类中的静态变量和方法名在程序加载的过程中就已经执行完了,不需要等待调用在这个类加载完成之前,Goods这个名字还没有出现在内存空间中私有的静态属性可以在类的内部使用,用来隐藏某个变量的值

  类中的私有成员:

    1.私有的静态属性

    2.私有的对象属性

    3.私有的方法

  

  为什么要定义一个私有变量?

    1.隐藏此值,无法被查看

    2.使此值无法被修改

    3.如果你硬要修改此值,也会有一些限制,这么做是为了保证数据的安全

    4.有些方法或属性不希望被子类继承

   

当变量私有化之后,其在内存当中的存储方式也会发生变化,我们称其为变形:

 

class Goods:
    __discount = 0   
print(Goods.__dict__)
print(Goods._Goods__discount)  #编程规范的角度上出发 我们不能在类的外部使用私有的变量

  #{'__module__': '__main__', '_Goods__discount': 0, '__dict__': <attribute '__dict__' of 'Goods' objects>,\
'__weakref__': <attribute '__weakref__' of 'Goods' objects>, '__doc__': None}
  #0

 

私有变量能否在外部被定义?

class A:
    __country = 'China'
    print(__country)    #_A__country
print(A.__dict__)
A.__language = 'Chinese'
print(A.__dict__)
print(A.__language) #不能在外部被定义,只能添加
a = A()
print(a.__language)

  

私有变量能不能被继承?

class A:
    __country = 'China'
    def __init__(self,name):
        A.__country = name
class B(A):
    def get_name(self):
        return self.__name
b = B('wuyi')
print(b.__dict__)    # 不可被继承

 

  # 一些实例

class Student:
    def __init__(self,name,age):
        self.__name = name
        self.age = age
    def name(self):
        return self.__name
clara = Student('Clara',19)
print(clara.name())
print(clara.age)
clara.age = 'young'
print(clara.age)

    #  Clara
    #  19
    #  young

 

class Goods:
    __discount = 0.8
    def __init__(self,name,price):
        self.name = name
        self.__price = price

    def price(self):
        return self.__price * Goods.__discount

    def change_price(self,new_price):
        if type(new_price) is int:
            self.__price = new_price
        else:
            print('价格修改失败')

apple = Goods('Apple',5)
print(apple.price())
apple.change_price('qwer')
print(apple.price())
apple.change_price(8)
print(apple.price())

    #  4.0
    # 价格修改失败
    #  4.0
    #  6.4

  

class User:
    def __init__(self,username,password):
        self.user = username
        self.__pwd = password
        self.pwd = self.__getpwd()

    def __getpwd(self):
        return hash(self.__pwd)

obj = User('wuyi','clara51')
print(obj.user,obj.pwd)

    #  wuyi 8536719674647458939

转载于:https://www.cnblogs.com/Clara51/p/9555107.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值