练习36(多态、封装、property)

练习36


# 什么是多态: Animal类表现出了Dog,Cat俩种形态
# def func(name,age):
#     print(name,age)
# func(1,2)

# class Animal:pass
# class Dog(Animal):pass
# class Cat(Animal):pass
#
# # 为什么要把多态单独列出来
# class Aapplepay():
#     def pay(self):pass
#
#     def pay(self):pass
#
# def pay(obj,money):
#     obj.pay()
#
# app = Aapplepay()
# print(type(app))
# # str 是一个类
# a = '123'
# a = str('123') # str是一个类,str() 就是实例化, a是一个对象,是str类的对象,a的type就是str


# 什么是多态呢?
# 一个类表现出的多种状态: 通过继承来实现的
# 在java当中的表现: 在一个函数中需要给参数指定数据类型,如果这个地方可以接收俩个以上类型的参数,
#                   那么这些类型应该有一个父类,这个父类是所有子类对象的类型

# class func(Cat mao):pass
# class func(Dog gou):pass
# class func(Animal gou|mao):pass

# 在python中: 函数的参数需要要指定数据类型,所以我们也不需要通过继承的形式来统一组类的类型
#           换句话说 所有的对象其实都是object类型,所以在python当中其实处处是多态
# def func(a):
#           a 既可以传猫也可以传狗类型,随便传任意类型

# 鸭子类型
# def len(obj):
# len() # str list dict tuple set range
# index() # str list tuple
# 不是明确的通过明确的继承实现的多态
# 而是通过一个模糊的概念来判断这个函数能不能接受这个类型的参数


# 广义上的封装
# class 类名:
#   def 方法1(self):pass
# 是为了只有这个类的对象才能使用定义在类中的方法

# 狭义上的封装
# 把一个i那个字藏在类中
# class Goods:
#     __discount = 0 # 私有的静态变量
#     print(__discount)

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

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

# class Student:
#     def __init__(self,name,age):
#         self.__name = name
#         self.age = age
#     def name(self):
#         return self.__name
#
# zhuge = Student('诸葛',20)
# print(zhuge.name())
# print(zhuge.age)
# zhuge.age = 'aaa'
# print(zhuge.age)

# class Goods:
#     __discount = 0.7 # 私有的静态变量
#     def __init__(self,name,price):
#         self.name = name   # apple
#         self.__price = price # 5
#
#     def price(self):
#         return self.__price *Goods.__discount  # 5 * 0.7
#
#     def change_price(self,new_price):
#         if type(new_price) is int:
#             self.__price = new_price  # 5 = 10
#         else:
#             print('本次价格修改不成功')
#
#
# apple = Goods('apple',5)
# print(apple.price())
# apple.change_price(10)
# print(apple.price())

# class User:
#     def __init__(self,username,password):
#         self.usr = username
#         self.__pwd = password
#         self.pwd = self.__getpwd()
#
#     def __getpwd(self):
#         return hash(self.__pwd)
# obj = User('alex','alex3714')
# print(obj.usr,obj.pwd)


# 类中的私有成员:
    # 私有的静态变量属性
    # 私有的对象属性
    # 私有的方法属性



# 为什么要定义一个私有变量:
    # 我不想让你看到这个值
    # 我不想让你改这个值
    # 我想让你在修改这个值的时候有一些限制
    # 有些方法或者属性不希望被子类继承

# 私有变量能不能在外部被定义?
# class A:
#     __country = 'china' # 在类的内部会发生变形
#     print(__country)    # _A__country
# print(A.__dict__)
# A.__language = 'chinese'
# print(A.__dict__)

# 私有变量能不能被继承??
# class A:
#     __country = 'china'
#     def __init__(self,name):
#         self.name = name  # '_A__name'
#
# class B(A):
#     # print(__country)
#     # name '_B__country' is not defined
#     def get_name(self):
#         return self.__name  # 'B' object has no attribute '_B__name'
# b = B('alex')
# print(b.__dict__)


# property 内置函数
# property是一个装饰器函数
# 所有的装饰器函数 在函数 方法 类的上面[一行直接@装饰器名字
# 装饰器的分类:
    # 装饰函数
    # 装饰方法: property
    # 装饰类

# class Student:
#     def __init__(self,name,age):
#         self.__name = name
#         self.age = age
#     @property  # 将一个方法伪装成一个属性
#     def name(self):
#         return self.__name
#
# zhuge = Student('诸葛',20)
# print(zhuge.name)

# from math import pi
# class Circle:
#     def __init__(self,r):
#         self.r = r
#     @property
#     def area(self):
#         return self.r ** 2 * pi
#     @property
#     def perimter(self):
#         return 2 * self.r * pi
#
# c1 = Circle(10)
# print(c1.area)
# print(c1.perimter)
# c1.r = 5
# print(c1.area)
# print(c1.perimter)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值