面向对象编程设计练习题(2) 11.请简单解释Python中 staticmethod(静态方法)和 classmethod(类方法), 并分别补充代码执行下列方法  12.请执行以下代码,解释错误原因,并修正错误。 TypeError: 'NoneType' object is not callable 因为eat方法添加了@property装饰器,将函数属性装饰后可以像数据属性一样被用户访问。 修改:d.eat()---->d.eat   14.多重继承的执行顺序,请解答以下输出结果是什么?并解释   15 请编写一段符合多态特性的代码  20 编写程序, 编写一个学生类, 要求有一个计数器的属性, 统计总共实例化了多少个学生 21 编写程序, A 继承了 B, 俩个类都实现了 handle 方法, 在 A 中的 handle 方法中调用 B 的 handle 方法

  staticmethod(静态方法):又称为非绑定方法,不与类和对象绑定,就是一个普通方法,不会自动传值。
  classmethod(类方法):是绑定到类的方法,自动将类作为第一个参数传入

class A(object):

   def foo(self, x):
       print("executing foo(%s, %s)" % (self,x))

   @classmethod
   def class_foo(cls, x):
       print("executing class_foo(%s, %s)" % (cls,x))

   @staticmethod
   def static_foo(x):
       print("executing static_foo(%s)" % (x))

a = A()
class A(object):
    def __init__(self,name):
        self.name = name
    def foo(self, x):
       print("executing foo(%s, %s)" % (self,x))

    @classmethod
    def class_foo(cls, x):
       print("executing class_foo(%s, %s)" % (cls,x))

    @staticmethod
    def static_foo(x):
       print("executing static_foo(%s)" % (x))

a = A('lys')
a.foo('lys')
a.static_foo('lys')
A.static_foo('LYS')
A.class_foo('LYS')

 12.请执行以下代码,解释错误原因,并修正错误。

class Dog(object):

   def __init__(self,name):
       self.name = name

   @property
   def eat(self):
       print(" %s is eating" %self.name)

d = Dog("ChenRonghua")
d.eat()


TypeError: 'NoneType' object is not callable

因为eat方法添加了@property装饰器,将函数属性装饰后可以像数据属性一样被用户访问。

修改:d.eat()---->d.eat

 

13.下面这段代码的输出结果将是什么?请解释

class Parent(object):
   x = 1

class Child1(Parent):
   pass

class Child2(Parent):
   pass

print(Parent.x, Child1.x, Child2.x)
Child1.x = 2
print(Parent.x, Child1.x, Child2.x)
Parent.x = 3
print(Parent.x, Child1.x, Child2.x)

# 1 1 1 继承自父类的类属性x,所以都一样,指向同一块内存地址
# 1 2 1 更改Child1,Child1的x指向了新的内存地址
# 3 2 3 更改Parent,Parent的x指向了新的内存地址

14.多重继承的执行顺序,请解答以下输出结果是什么?并解释

class A(object):
   def __init__(self):
       print('A')
       super(A, self).__init__()

class B(object):
   def __init__(self):
       print('B')
       super(B, self).__init__()

class C(A):
   def __init__(self):
       print('C')
       super(C, self).__init__()

class D(A):
   def __init__(self):
       print('D')
       super(D, self).__init__()

class E(B, C):
   def __init__(self):
       print('E')
       super(E, self).__init__()

class F(C, B, D):
   def __init__(self):
       print('F')
       super(F, self).__init__()

class G(D, B):
   def __init__(self):
       print('G')
       super(G, self).__init__()

if __name__ == '__main__':
   g = G()
   f = F()

# G
# D
# A
# B
#
# F
# C
# B
# D
# A
super()表示的是 子类的mro()列表中的下一个
    print(G.mro())
        [<class '__main__.G'>, <class '__main__.D'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>]
    print(F.mro())
        [<class '__main__.F'>, <class '__main__.C'>, <class '__main__.B'>, <class '__main__.D'>, <class '__main__.A'>, <class 'object'>]

 

15 请编写一段符合多态特性的代码

import abc
class Animals(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def talk(self):
        pass

class People(Animals):
    def talk(self):
        print("hello")

class Dog():
    def talk(self):
        print('wowow')
def func(msg):
    msg.talk()
a=People()
b=Dog()
a.talk()
b.talk()
func(a)

 20 编写程序, 编写一个学生类, 要求有一个计数器的属性, 统计总共实例化了多少个学生

class Student:
    count = 0
    def __init__(self, name):
        self.name = name
        Student.count += 1

    def show_count(self):
        print(self.count)

stu1 = Student('aa')
stu2 = Student('bb')
stu3 = Student('cc')
stu1.show_count()

21 编写程序, A 继承了 B, 俩个类都实现了 handle 方法, 在 A 中的 handle 方法中调用 B 的 handle 方法

class B:
    def handle(self):
        print('B')

class A(B):
    def handle(self):
        super().handle()

a = A()
a.handle()