面向对象高级及一些方法

property的应用

# 例1

# class People:

#     def __init__(self, name, height, weight):

#         self.name = name

#         self.height = height

#         self.weight = weight

#

#     @property

#     def bmi(self):

#         return self.weight / (self.height ** 2)

#
#

# p = People('egon', 1.81, 70)

# p.height = 1.84

print(p.bmi())

 

例2:

class Student:
  __school = "oldboy" # _Student__school = "oldboy"

  def __init__(obj, x, y, z):
      obj.__name = x
      obj.__age = y
      obj.gender = z
   
  @property
  def name(self):
      print("访问控制")
      return self.__name
   
  @name.setter
  def name(self, x):
      print("赋值控制")
      self.__name = x
   
  @name.deleter
  def name(self):
      print("删除控制")
      del self.__name


stu_obj1 = Student("冯疯子", 18, "female")

stu_obj1.name
# 1\在子类派生的新方法中重用父类的功能

# 方式一:指名道姓地调用某一个类的函数

特点:不依赖于继承关系

#

# class OldboyPeople:

#     school = "oldboy"

#     #             空对象,"艾利克斯",73,'male'

#     def __init__(self,name,age,gender):

#         self.name = name

#         self.age = age

#         self.gender = gender

#

#     def f1(self):

#         print('1111111')

#

# class Student(OldboyPeople):

#     #           空对象,"艾利克斯",73,'male',1001,"python全栈开放"

#     def __init__(self,name,age,gender,stu_id,course):

#         OldboyPeople.__init__(self,name,age,gender) # OldboyPeople.__init__(空对象,"艾利克斯",73,'male')

#         self.stu_id = stu_id

#         self.course = course

#
#

#     def choose(self):

#         print('%s 正在选课' %self.name)

#

#     def f1(self):

#         OldboyPeople.f1(self)

#         print("22222")

#

# stu1=Student("艾利克斯",73,'male',1001,"python全栈开放")

# # tea1=Teacher("egon",18,'male',2000,10)

#
#

# stu1.f1()

 

# 2 在子类派生的新方法中重用父类功能的方式二

# 方式一:调用super(自己的类名,self)会返回一个特殊的对象,super(自己的类名,self).属性,会参照属性查找发起的那个类的mro列表去它父类中查找属性

# 特点:严格依赖于继承关系

 

# class OldboyPeople:

#     school = "oldboy"

#     #             空对象,"艾利克斯",73,'male'

#     def __init__(self,name,age,gender):

#         self.name = name

#         self.age = age

#         self.gender = gender

#

#     def f1(self):

#         print('1111111')

#

# class Student(OldboyPeople):

#     def __init__(self,name,age,gender,stu_id,course):

#         # OldboyPeople.__init__(self,name,age,gender) # OldboyPeople.__init__(空对象,"艾利克斯",73,'male')

#         super(Student,self).__init__(name,age,gender)

#         self.stu_id = stu_id

#         self.course = course

#
#

#     def choose(self):

#         print('%s 正在选课' %self.name)

#

#     def f1(self):

#         # OldboyPeople.f1(self)

#         # super().f1()

#         print("22222")

#

# # print(Student.mro())

# stu1=Student("艾利克斯",73,'male',1001,"python全栈开放")

# # print(stu1.__dict__)

# stu1.f1()


#3:

# class A:

#     def test(self):

#         super().test()

# class B:

#     def test(self):

#         print('from B')

# class C(A,B):

#     pass

# c=C()

# print(C.mro())

# c.test()

# obj=A()

# obj.test()

#4:
class A:
    def test(self):
        print('A---->test')
        super().aaa()
class B:
    def test(self):
        print('B---->test')

def aaa(self):
  print('B---->aaa')

class C(A,B):
    def aaa(self):
        print('C----->aaa')

c=C()
print(C.mro())
c.test()
"""
A---->test
B---->aaa
"""

对象组合

# 组合:把另外一个类的对象赋值给当前对象的属性

# 组合表达的是一种有的关系

class Teacher:
    def init(self, name, age, gender, level):
        self.name = name
        self.age = age
        self.gender = gender
        self.level = level

def tell(self):
  print("%s:%s" % (self.name, self.age))

class Student:
    def init(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender

class Course:
    def init(self, name, price, period):
        self.name = name
        self.price = price
        self.period = period

def tell(self):
  print('<%s:%s:%s>' % (self.name, self.price, self.period))

tea1 = Teacher("egon", 18, "male", 10)
stu1 = Student("xxx", 19, "male")

python = Course("python开放", 30000, "3mons")
linux = Course("linux课程", 30000, "3mons")

tea1.courses = [python,linux]
stu1.course = python

# tea,stu  # 超级对象

# stu1.course.tell()

for course_obj in tea1.courses:
    course_obj.tell()

内置方法

内置方法都是在满足某种条件下自动触发的

# 1 __str__

# class People:

#     def __init__(self, name, age):

#         self.name = name

#         self.age = age

#

#     def __str__(self):

#         # print('===>')

#         return "<%s:%s>" %(self.name,self.age)

#

# obj = People("egon", 18)

#

# print(obj) # print(obj.__str__())


# 2 __del__

class People:
  def __init__(self, name, age,f):
      self.name = name
      self.age = age
      self.f = f

  def __del__(self):
      print('===>')
      # 回收资源
      self.f.close()

obj = People("egon", 18,open("a.txt",'w',encoding='utf-8'))

del obj
print('运行完毕...')
posted @ 2020-08-16 11:05  Οo白麒麟оΟ  阅读(94)  评论(0编辑  收藏  举报