方法没有重载,方法的动态性,@property装饰器_get和set方法,类体

方法没有重载

Python 中没有方法的重载。定义多个同名方法,只有最好一个有效

方法的动态性

Python是动态语言,我们可以动态的为类添加新的方法,或者动态的修改类的已有的方法
class Person:

class Person:

    def work(self):
        print("努力上班!")


def play_game(s):
    print("{0}在玩游戏".format(s))


def work2(s):
    print("好好工作,努力上班!赚大钱,娶媳妇!")


Person.play = play_game
p = Person()
p.work()
p.play()  # Person.play(p)

Person.work = work2

p.work()

#输出:努力上班!
#<__main__.Person object at 0x000001FD80652608>在玩游戏
#好好工作,努力上班!赚大钱,娶媳妇!

测试私有属性

class Employee:
    __company = "百战程序员"

    def __init__(self, name, age):
        self.name = name
        self.__age = age  # 私有属性

    def __work(self):  # 私有方法
        print("好好工作,赚钱取媳妇")
        print("年龄:{0}".format(self.__age))


e = Employee("高淇", 18)

print(e.name)
# print(e.age)
print(e._Employee__age)
print(dir(e))
e._Employee__work()
print(Employee._Employee__company)
#高淇
#18
#['_Employee__age', '_Employee__company', '_Employee__work', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']
#好好工作,赚钱取媳妇
#年龄:18
#百战程序员
@property装饰器_get和set方法

property装饰器可以将一个方法的调用方式变成"属性调用"

测试@property的最简化用法:
class Employee:

    @property
    def salary(self):
        print("salary run...")
        return 10000


emp1 = Employee()
# empl.salary() 传统调用方法
print(emp1.salary)

#salary run……
#10000
测试@property装饰器的用法:
class Employee:

    def __init__(self, name, salary):
        self.__name = name
        self.__salary = salary

    def get_salary(self):
        return self.__salary

    def set_salry(self, salary):
        if 1000 < salary < 50000:
            self.__salary = salary
        else:
            print("录入错误!薪水在1000-50000这个范围")


emp1 = Employee("高琪", 300000)
print(emp1.get_salary())
emp1.set_salry(-20000)
print(emp1.get_salary())
#输出:300000
#录入错误!薪水在1000-50000这个范围
#300000
测试@property装饰器的用法:
class Employee:

    def __init__(self, name, salary):
        self.__name = name
        self.__salary = salary

    @property
    def salary(self):
        return self.__salary

    @salary.setter
    def salary(self, salary):
        if 1000 < salary < 50000:
            self.__salary = salary
        else:
            print("录入错误!薪水在1000-50000这个范围")


'''

    def get_salary(self):
        return self.__salary

    def set_salry(self, salary):
        if 1000 < salary < 50000:
            self.__salary = salary
        else:
            print("录入错误!薪水在1000-50000这个范围")
'''

emp1 = Employee("高琪", 300000)
# print(emp1.get_salary())
# emp1.set_salry(-20000)
# print(emp1.get_salary())

print(emp1.salary)
emp1.salary = 2000
print(emp1.salary)
#输出300000
#2000

#面向对象的三大特征说明(封装、继承、多态).

#继承格式:
class 子类类名(父类1[,父类2,…]):

类体

如果在类定义中没有指定父类,则默认父类是object类。也就是说,object 是所有类的父类,里面定义了一些所有的默认实现,比如:new().

#定义子类时,必须在其构造函数中调用父类的构造函数。调用格式:
#父类名.init(self,参数列表)

#测试继承的使用

class Person:
    pass

class Student(Person):
    pass

#student-->Person-->object类

测试继承的基本使用
class Person:

    def __init__(self, name, age):
        self.name = name
        self.__age = age  # 私有属性

    def say_age(self):
        print("年龄,年龄,我也不知道")


class Student(Person):

    def __init__(self, name, age, score):
        Person.__init__(self, age, score)  # 必须是式的调用父类初始化方法,不然解释器不会去调用
        self.score = score


# Student-->Person-->object类
print(Student.mro())

s = Student("高琪", 18, 60)
s.say_age()
print(s.name)
# print(s.age)

print(dir(s))
print(s._Person__age)

#输出:[<class ‘main.Student’>, <class ‘main.Person’>, <class ‘object’>]
年龄,年龄,我也不知道
18
[’_Person__age’, ‘class’, ‘delattr’, ‘dict’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘gt’, ‘hash’, ‘init’, ‘init_subclass’, ‘le’, ‘lt’, ‘module’, ‘ne’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘setattr’, ‘sizeof’, ‘str’, ‘subclasshook’, ‘weakref’, ‘name’, ‘say_age’, ‘score’]
60

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值