[Python]小甲鱼Python视频第043课(魔法方法:算术运算2 )课后题及参考解答

# -*- coding: utf-8 -*-
"""
Created on Sun Mar 17 22:28:39 2019

 [[y ]]
@author: fengs
"""

"""
测试题:
0. 对象相加(a + b),如果 a 对象有 __add__ 方法,请问 b 对象的 __radd__ 会被调用吗?
    不会,若a不存在 __add__ 方法 才会调用b的 __radd__ 方法

1. Python 什么时候会调用到反运算的魔法方法?
    涉及到的实例对象没有正运算的魔法方法

2. 请问如何在继承的类中调用基类的方法?
    super().base_func()
3. 如果我要继承的基类是动态的(有时候是 A,有时候是 B),我应该如何部署我的代码,以便基类可以随意改变。
    ???
    继承一个变量,这个变量可以设置为A,也可以设置为B
4. 尝试自己举一个例子说明如何使用类的静态属性。(一定要自己先动手再看答案哦^_^)

class Test:
    obj_count = 0;
    
    def __init__(self):
        Test.obj_count += 1
    def __del__(self):
        Test.obj_count -= 1
        
obj_count 可以用来统计类对象的实例个数

5. 尝试自己举例说明如何使用类的静态方法,并指出使用类的静态方法有何有点和需要注意的地方?(一定要自己先动手再看答案哦^_^)
类的静态方法中不能访问对象实例的属性或方法

    加 @staticmethod 修饰符
"""

#测试题4
class static_property():
    static_filed1 = 1;
    #@staticmethod
    def method():
        print('I am static_method of static_property')
        #print(static_filed1)
    
#print(static_property.static_filed1)

#测试题5.
class static_method():
    
    static_filed2 = 2;
    #@staticmethod
    def method():
        print('I am static_method of static_property')
        #print(static_filed2)
        

#static_method.method()


#测试题3
base_class = static_property
class varBaseClass(base_class):
   # @staticmethod
    def method(self):
        base_class.method()
        

varBase1 = varBaseClass()
varBase1.method()
#base_class = static_method
#varBase2 = varBaseClass()
#varBase2.method()    #貌似这样不行,一经运行已经定了就不能改


#varBase.method()


"""

动动手:
0. 定义一个类,当实例化该类的时候,自动判断传入了多少个参数,并显示出来:
>>> c = C()
并没有传入参数
>>> c = C(1, 2, 3)
传入了 3 个参数,分别是:1 2 3

1. 定义一个单词(Word)类继承自字符串,重写比较操作符(参考自学:Python 魔法方法详解),当两个 Word 类对象进行比较时,根据单词的长度来进行比较大小。
加分要求:实例化时如果传入的是带空格的字符串,则取第一个空格前的单词作为参数。

"""

#动动手0
class C():
    def __init__(self,*args):
        length = len(args)
        if length == 0:
            print("并没有传入参数")
        else:
            print("传入了 %d 个参数,分别是: " % length,end = ' ')
            for each in args:
                print(each,end = ' ')


c = C()
c = C(1,2,3,[1,2,3])


#动动手1
class Word(str):
    
    def __init__(self,input_str):
        
        input_str = '  1234  12345'
        while(input_str.startswith(' ')):
            input_str = input_str[1:]   #去头部去尾       
        if ' ' in input_str:
            index = input_str.find(' ')
            input_str = input_str[0:index]
            
        self.str_value  = input_str
        self.str_length = len(input_str)
        
    def __lt__(self,other):
        if self.str_length < other.str_length:
            return True
        else:
            return False

    def __le__(self,other):
        if self.str_length <= other.str_length:
            return True
        else:
            return False    
    def __eq__(self,other):
        if self.str_length == other.str_length:
            return True
        else:
            return False
    def __ne__(self,other):
        if self.str_length != other.str_length:
            return True
        else:
            return False
    def __gt__(self,other):
        if self.str_length > other.str_length:
            return True
        else:
            return False
    def __ge__(self,other):
        if self.str_length >= other.str_length:
            return True
        else:
            return False        
print('-------------------------')   
word1 = Word(' 1234 2')
word2 = Word('1234 44')
print(word1 < word2)
print(word1 <= word2)
print(word1 == word2)
print(word1 != word2)
print(word1 > word2)
print(word1 >= word2)

  

posted @ 2019-03-21 23:22  Alimy  阅读(409)  评论(0编辑  收藏  举报