python基础—面向对象(2)

基础用法:

class user:
    def __init__(self,name,pasaword):
        self.name = name
        self.password = pasaword
user_1 = user("usera","416431131")
print(user_1.name,user_1.password)

私有化:

使用属性名前面加__来使属性名进行私有化处理,防止其他人进行调用

私有化可以是私有属性,私有方法,私有静态变量,私有的内容不能被继承

私有化的目的:

让用户不在外部调用类中的某个名字

class user:
    def __init__(self,name,pasaword):
        self.name = name
        self.__password = pasaword
    def get_password(self):
        return  self.__password
    def change_password(self):
        pawd = input("输入原密码:")
        if pawd == self.__password:
            paw = input("输入新密码:")
            self.__password = paw
        else:
            print("原密码错误")
user_1 = user("user","416431131")
user.change_password(user_1)
print(user_1.name,user.get_password(user_1))

 property装饰器:

使用property装饰器可以把方法伪装成属性进行使用

"使用property进行修饰"
from math import pi
class circle:
    def __init__(self,r):
        self.r = r
    @property
    def area(self):
        return  pi*self.r**2
circle_1 = circle(4)
print(circle_1.r,circle_1.area)

"不使用property进行修饰"
from math import pi
class circle:
    def __init__(self,r):
        self.r = r
    def area(self):
        return  pi*self.r**2
circle_1 = circle(4)
print(circle_1.r)
print(circle_1.area())

使用setter和deleter改变被property修饰的方法:

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

    @price.setter
    def price(self,new_price):
        self.__price = new_price
    @price.deleter
    def price(self):
        del Goods.price
apple = Goods("apple",5)
print(apple.price) 
apple.price = 9              #调用setter改变单价
del apple.price              #调用delter删除类中的price属性进而删除price方法0
print(apple.price)

反射:

用字符串类型的名字来操作名字对用的函数/实例变量/绑定方法/各种方法

使用getattr()函数

反射对象的实例变量

class user:
    def __init__(self,name,age):
        self.name = name
        self.age = age
user_1 = user("哆啦A梦",'18')
while True:
    s = input("请输入一个变量名:")
    print(getattr(user_1,s))

反射类的静态方法/绑定方法/其他方法

class user:
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def get_age(self):
        return "调用了get__name函数,年龄为{}",self.age
user_1 = user("哆啦A梦",'18')
ret = getattr(user_1,'get_age')
print(ret())

反射模块中的变量

import time
print(getattr(time,"time"))

classmethod装饰器

类方法

使类中的方法可以不用创建对象直接进行使用,在方法中仍可以使用类中的静态方法

class Goods:
    __discount = 0.8
    def __init__(self):
        self.__price = 5
        self.price = self.__price * self.__discount
    @classmethod
    def change_discount(cls,new_discount):
        cls.__discount = new_discount
Goods.change_discount(0.5)
apple = Goods()
print(apple.price)

staticmethod装饰器

静态方法

被装饰的方法变成一个静态方法

将普通函数挪到类的内部执行,使用的时候加上类名

class User:
    def __init__(self,name,password):
        self.name = name
        self.password = password
    @staticmethod
    def login():
        pass

可以定义在类中的内容:

静态变量   所有对象共享的变量  由对象/类调用,但是不能重新赋值

绑定方法   自带self参数的函数  由对象调用

类方法       自带cls参数的函数  由对象/类调用

静态方法   啥也不带的普通函数 由对象/类调用 

property属性  伪装成属性的方法 由对调用,但是不加括号

  • 22
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值