Python练习题答案: 初学者非常简单的继承【难度:1级】--景越Python编程实例训练营,1000道上机题等你来挑战

初学者非常简单的继承【难度:1级】:

答案1:

class Add(Operation):
    def execute(self, v1, v2):
        self.result = v1+v2
class Subtract(Operation):
    def execute(self, v1, v2):
        self.result = v1-v2
class Multiply(Operation):
    def execute(self, v1, v2):
        self.result = v1*v2
class Divide(Operation):
    def execute(self, v1, v2):
        try:
            self.result = v1 / v2
        except ZeroDivisionError:
            self.result = None

答案2:

class Add(Operation):
    def __init__(self):
        pass
    
    def execute(self, v1, v2):
        Operation.result = v1 + v2

class Subtract(Operation):
    def __init__(self):
        pass
    
    def execute(self, v1, v2):
        Operation.result = v1 - v2

class Multiply(Operation):
    def __init__(self):
        pass
    
    def execute(self, v1, v2):
        Operation.result = v1 * v2

class Divide(Operation):
    def __init__(self):
        pass
    
    def execute(self, v1, v2):
        Operation.result = v1 / v2 if v2 != 0 else None

答案3:

# TODO: add classes here
class Add(Operation):
    '''add two operands'''
    def execute(self, v1, v2):
        self.result = v1 + v2
    
class Subtract(Operation):
    '''subtract two operands'''
    def execute(self, v1, v2):
        self.result = v1 - v2
    
class Multiply(Operation):
    '''multiply two operands'''
    def execute(self, v1, v2):
        self.result = v1 * v2

class Divide(Operation):
    '''divide two operands'''
    def execute(self, v1, v2):
        if v2 == 0:
            self.result = None
        else:
            self.result = v1 / v2​

答案4:

class Add(Operation):
    def __init__(self):
        Operation.__init__(self)
    
    def execute(self, augend, addend):
        self.result = augend + addend


class Subtract(Operation):
    def __init__(self):
        Operation.__init__(self)
        
    
    def execute(self, minuend, subtrahend):
        self.result = minuend - subtrahend
        
        
class Multiply(Operation):
    def __init__(self):
        Operation.__init__(self)
        
    
    def execute(self, multiplicand, multiplier):
        self.result = multiplicand * multiplier
        

class Divide(Operation):
    def __init__(self):
        Operation.__init__(self)
        
    
    def execute(self, dividend, divisor):
        if divisor != 0:
            self.result = dividend / divisor
    
    
    ​

答案5:

class Add(Operation):
    def execute(self,a,b):
        self.result = a+b
        pass
        

class Subtract(Operation):
    def execute(self,a,b):
        self.result = a-b


class Multiply(Operation):
    def execute(self,a,b):
        self.result = a*b
        
        
class Divide(Operation):
    def execute(self,a,b):
        self.result = a/b if b!= 0 else None

答案6:

class Add(ABC):
    def __init__(self):
        self.result = None
    
    def execute(self, v1, v2):
        self.result = v1+v2    
        
class Subtract(ABC):
    def __init__(self):
        self.result = None
    
    def execute(self, v1, v2):
        self.result = v1-v2    
        
class Multiply(ABC):
    def __init__(self):
        self.result = None
    
    def execute(self, v1, v2):
        self.result = v1*v2
        
class Divide(ABC):
    def __init__(self):
        self.result = None
    
    def execute(self, v1, v2):
        ret = None
        if v2 != 0:
            ret = v1 / v2
            
        self.result = ret
        ​

答案7:

class Operation():
    def __init__(self):
        self.result = None
    @abstractmethod
    def execute(self,v1,v2):pass
class Add(Operation):
    def execute(self,v1,v2):
        self.result =  v1+v2
class Subtract(Operation):
    def execute(self,v1,v2):
        self.result = v1-v2
class Multiply(Operation):
    def execute(self,v1,v2):
        self.result = v1*v2
class Divide(Operation):
    def execute(self,v1,v2):
        if v1 != 0 and v2 != 0:
            self.result = v1/v2​

答案8:

class Add(Operation):
    
    def execute(self, a, b):
        self.result = a+b


class Subtract(Operation):
    
    def execute(self, a, b):
        self.result = a-b
      
      
class Divide(Operation):
    
    def execute(self, a, b):
        try:
            self.result = a/b
        except:
            return None
        
        
class Multiply(Operation):
    
    def execute(self, a, b):
        self.result = a*b
        ​

答案9:

class Operation(ABC):
    def __init__(self):
        self.result = None

    @abstractmethod
    def execute(self, v1, v2):
        pass

class Add(Operation):
    def execute(self, v1, v2):
        self.v1 = v1
        self.v2 = v2
        self.result = self.v1 + self.v2

class Subtract(Operation):
    def execute(self, v1, v2):
        self.v1 = v1
        self.v2 = v2
        self.result = self.v1 - self.v2

class Multiply(Operation):
    def execute(self, v1, v2):
        self.v1 = v1
        self.v2 = v2
        self.result = self.v1 * self.v2

class Divide(Operation):
    def execute(self, v1, v2):
        self.v1 = v1
        self.v2 = v2
        try:
            self.result = self.v1 / self.v2
        except:
            self.result = None

答案10:

class Add(Operation):
    def _init_(self):
        pass
    def execute(self,v1,v2):
        self.result = v1+v2

class Subtract(Operation):
    def _init_(self):
        pass
    def execute(self,v1,v2):
        self.result = v1-v2

class Multiply(Operation):
    def _init_(self):
        pass
    def execute(self,v1,v2):
        self.result = v1*v2

class Divide(Operation):
    def _init_(self):
        pass
    def execute(self,v1,v2):
        try:
            self.result = v1/v2
        except ZeroDivisionError:
            self.result= None

你可能感兴趣的:(Python编程高级练习题)