Python基础学习day08

# 测试方法的动态性
'''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()'''
# 测试私有属性
class Employee:
    def __init__(self,name,age):
        self.name = name
        self.__age = age

e = Employee('高淇',18)
print(e.name)
# print(e.age)
print(e._Employee__age)
print(dir(e))

Python基础学习day08_第1张图片

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

    def __work(self):   #私有方法
        print('好好工作,赚钱娶媳妇!')


e = Employee('高淇',18)
print(e.name)
# print(e.age)
print(e._Employee__age)
print(dir(e))
e._Employee__work()
# @property装饰器的用法
class Employee:
    def __init__(self,name,salary):
        self.__name = name
        self.__salary = salary

    def get_salary(self,salary):
        self.__salary = salary
        if 1000 
  
# 测试继承的基本使用
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)
        self.score = score       # 必须显示调用父类初始化方法,不然解释器不会调用

print(Student.mro())

s = Student('高淇',18,60)
s.say_age()

Python基础学习day08_第2张图片

 

# 测试继承的基本使用
class Person:
    def __init__(self,name):
        self.name = name
    def __str__(self):
        return '名字是:{0}'.format(self.name)

p = Person('高淇')
print(p)

 Python基础学习day08_第3张图片

 

# 多态
class Man:
    def eat(self):
        print('饿了,吃饭了!')
class Chinese(Man):
    def eat(self):
        print('中国人用筷子吃饭')
class English(Man):
    def eat(self):
        print('英国人用叉子吃饭')
class Indian(Man):
    def eat(self):
        print('用右手吃饭')
def manEat(m):
    if isinstance(m,Man):
        m.eat()
    else:
        print('不能吃饭')
manEat(Chinese())
manEat(English())

Python基础学习day08_第4张图片

 

# 运算符重载
class Person:
    def __init__(self,name):
        self.name = name

    def __add__(self, other):
        if isinstance(other,Person):
            return '{0}--{1}'.format(self.name,other.name)
        else:
            return '不是同类对象,不能相加'
p1 = Person('高淇')
p2 = Person('高希希')
x = p1+p2
print(x)

你可能感兴趣的:(python,开发语言,后端)