Python9-封装-day26(大年初三)

class Room:
    def __init__(self,name,length,width):
        self.__name = name
        self.__length = length
        self.__width = width
    def get_name(self):
        return self.__name
    def set_name(self,newName):
        if type(newName) is str and newName.isdigit() == False:
            self.__name = newName
    def area(self):
        return  self.__length * self.__width

jin = Room('金老板',2,1)
print(jin.area())
jin.set_name('2')
print(jin.get_name())
会用到私有属性这个概念的场景
隐藏起一个属性,不想让类的外部调用
我想保护这个属性,不想让属性随意被改变
我想保护这个属性不被子类继承

# property 内置装饰器函数,只在面向对象中使用
from math import  pi
class Circle:
    def __init__(self,r):
        self.r = r
    @property
    def perimeter(self):
        return 2*pi*self.r
    @property
    def area(self):
        return self.r**2*pi
c1 = Circle(5)
print(c1.area)  #圆的面积
print(c1.perimeter)   #圆的周长


78.53981633974483
31.41592653589793
class Person:
    def __init__(self,name):
        self.__name = name
    @property
    def name(self):
        return self.__name + 'sb'
    @name.setter
    def name(self,new_name):
        self.__name = new_name
tiger = Person('tim')
print(tiger.name)
tiger.name = '全班'
print(tiger.name)

timsb
全班sb

class Goods:
    discount = 0.8
    def __init__(self,name,price):
        self.name = name
        self.__price = price
    @property
    def price(self):
        return self.__price * Goods.discount

apple = Goods('apple',5)
print(apple.price)
#删除属性
class Person:
    def __init__(self,name):
        self.__name = name
    @property
    def name(self):
        return self.__name
    @name.deleter
    def name(self):
        del self.__name
teng = Person('tim')
print(teng.name)
del teng.name
print(teng.name)

class——method

class Goods:
    __discount = 0.8
    def __init__(self,name,price):
        self.name = name
        self.__price = price
    @property
    def price(self):
        return self.__price * Goods.__discount
    @classmethod   #把一个方法变成一个类中的方法,这个方法就直接可以被类调用,不需要依托任何对象
    def change_discount(cls,new_discount):  #修改折扣
        cls.__discount = new_discount
apple = Goods('apple',5)
print(apple.price)
Goods.change_discount(0.5)
print(apple.price)
#当这个方法的操作,只涉及静态属性的时候,就应该使用classmethod

4.0
2.5

staticmethod

# staticmethod
class Login:
    def __init__(self,name,password):
        self.name = name
        self.pwd = password
    def login(self):pass
    @staticmethod
    def get_usr_pwd():
        usr = input('用户名: ')
        pwd = input('密码: ')
        Login(usr,pwd)

Login.get_usr_pwd()
#在完全面向对象的过程中
# 如果一个函数,既和对象没有关系,也和类没有关系,那么就用staticmethod将这个函数变成一个静态方法
# 类方法和静态方法,都是类调用的
# 对象可以调用类方法和静态方法 ,一般情况下,推荐用类名调用
# 类方法 有个默认参数 cls 代表这个类
# 静态方法 没有默认的参数 就像函数一样
 

 反射

class Teacher:
    dic = {'查看学生信息':'show_student','查看讲师信息':'show_teacher'}
    def show_student(self):
        print('show_student')
    def show_teacher(self):
        print('show_teacher')
    @classmethod
    def func(cls):
        print('haha')
tim = Teacher()
for k in Teacher.dic:
    print(k)
key = input('pls input :')
# print(Teacher.dic[key])
func = getattr(tim,Teacher.dic[key])
func()
# tim = Teacher()
# tim.show_student()
# func = getattr(tim,'show_student')
# func()
# hasattr()  getattr()  delattr()
# if hasattr(Teacher,'dic'):
#     ret = getattr(Teacher,'dic')  #Teacher.dic
# ret = getattr(Teacher,'dic')  #Teacher.dic
# ret2 = getattr(Teacher,'func')
# ret2()
#     print(ret)

猜你喜欢

转载自www.cnblogs.com/zhangtengccie/p/10354456.html
今日推荐