Python练习问题解答:初心者はとても简単な継承【难易度:1级】--景越Pythonプログラミング実例训练キャンプ、1000道上机题などに挑戦してください
38098 ワード
初心者はとても簡単な継承【難易度:1級】:
答え1:
答え2:
答え3:
答え4:
答え5:
答え6:
答え7:
答え8:
答え9:
答え10:
答え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