2022.01.26

課時105

 

 


#测试方法的动态性

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

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

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

执行结果:
努力上班!
<__main__.Person object at 0x0000023F61155648>在玩游戏

#测试方法的动态性

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

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

def work2(self):
    print("不上班啦!我要赚钱!")

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

Person.work=work2 #修改原有的方法
p.work()

执行结果:
努力上班!
<__main__.Person object at 0x0000023FFE3AB9C8>在玩游戏
不上班啦!我要赚钱!

 课时106

 

 

class Test:
    def __init__(self,name,age):
        self.name=name
        self.__age=age


a=Test("一一",20)
print(a.name)
print(a._Test__age)

执行结果:
一一
20

课时107

class Test:
    def __init__(self,name,age):
        self.name=name
        self.__age=age
    def __work(self):#私有方法
        print("不想学习")

a=Test("一一",20)
print(a.name)
print(a._Test__age)
a._Test__work()

print(dir(a))

执行结果:
一一
20
不想学习
['_Test__age', '_Test__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']

 

class Test:
    __company="程序员"
    def __init__(self,name,age):
        self.name=name
        self.__age=age
    def __work(self):#私有方法
        print("{0}的年纪,不想学习".format(self.__age))
        print(Test.__company)

a=Test("一一",20)
a._Test__work()

执行方法:
20的年纪,不想学习
程序员

 课时108

#测试property的用法

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

em1=Employee()
print(em1.salary)

em1.salary=1000

执行结果:
Traceback (most recent call last):
  File "F:/python/python_pycharm/class_test/test01.py", line 12, in <module>
    em1.salary=1000
AttributeError: can't set attribute
salary run...
10000

#结果不能设置,装饰器可以将方法装饰为属性,但本质还是方法
#测试property的用法

class Employee:

    def __init__(self,name,salary):
        self.name=name
        self.salary=salary
    @property
    def salary(self):
        return self.__salary

    @salary.setter #对于salary里面的属性定义
    def salary(self,salary):
        if 1000<salary<50000:
            self.__salary=salary
        else:
            print("录入错误,薪水应该在1000至50000之间")

em1=Employee("老三",10000)
em1.salary=-30000

执行结果:
录入错误,薪水应该在1000至50000之间

课时109

 

 课时110

 

 

 

 

 

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

class Student(Person):
    pass
print(Student.mro())

执行结果:
[<class '__main__.Student'>, <class '__main__.Person'>, <class '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,name,age) #调用了Person类的构造器
        self.score=score

s1=Student("老三",20,99)
print(s1.name)
print(s1.age)#私有属性,子类不能直接调用
print(dir(s1))

执行结果:
Traceback (most recent call last):
  File "F:/python/python_pycharm/class_test/test01.py", line 17, in <module>
    print(s1.age)
AttributeError: 'Student' object has no attribute 'age'
老三

课时111

#测试继承的基本使用
class Person:
    def __init__(self,name,age):
        self.name=name
        self.age=age

    def say_age(self):
        print("我的年龄:",self.age)

    def introuduce(self):
        print("我的名字是:",self.name)

class Student(Person):
    def __init__(self,name,age,score):
        Person.__init__(self,name,age) #调用了Person类的构造器
        self.score=score
    def intrdouce(self):#重写父类的方法
        print("老师,我的名字是:",self.name)

s1=Student("老三",20,99)
s1.intrdouce()
s1.say_age()


执行结果:
老师,我的名字是: 老三
我的年龄: 20

 课时112

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值