Python 入門指南 V2.00 - 單元 9 範例及練習程式碼



class_demo.py


class Demo:
   def set_att(self, a, b):
      self.a = a
      self.b = b
   
   def do_something(self):
      return self.a + self.b

d = Demo()
d.set_att(11, 22)
print(d.do_something())
 
# 檔名: class_demo.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

class_demo2.py


class Demo:
   def set_att(self, a=22, b=33):
      self.a = a
      self.b = b
   
   def do_something(self):
      return self.a + self.b

d = Demo()
d.set_att()
print(d.do_something())
 
# 檔名: class_demo2.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

class_demo3.py


class Demo:
   a = 11
   b = 22   

   @classmethod
   def do_something(cls):
      cls.a += 1
      cls.b += 1
      return cls.a + cls.b

print(Demo.do_something())
 
# 檔名: class_demo3.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0901.py


class IntegerDemo:
   def set_value(self, v):
      self.value = v

   def add(self, p):
      self.value += p

i = IntegerDemo()
i.set_value(25)
i.add(24)
print(i.value)
 
# 檔名: exercise0901.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0902.py


class IntegerDemo:
   def set_value(self, v):
      self.value = v

   def add(self, p):
      self.value += p

i = IntegerDemo()
i.set_value(int(input("v: ")))
i.add(int(input("+: ")))
print(i.value)
 
# 檔名: exercise0902.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0903.py


class IntegerDemo:
   def set_value(self, v):
      self.value = v

   def add(self, p):
      self.value += p
   
   def subtract(self, p):
      self.value -= p

i = IntegerDemo()
i.set_value(int(input("v: ")))
i.add(int(input("+: ")))
i.subtract(int(input("-: ")))
print(i.value)
 
# 檔名: exercise0903.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0904.py


class IntegerDemo:
   def set_value(self, v):
      self.value = v

   def add(self, p):
      self.value += p
   
   def subtract(self, p):
      self.value -= p

   def multiply(self, p):
      self.value *= p

i = IntegerDemo()
i.set_value(int(input("v: ")))
i.add(int(input("+: ")))
i.subtract(int(input("-: ")))
i.multiply(int(input("*: ")))
print(i.value)
 
# 檔名: exercise0904.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0905.py


class IntegerDemo:
   def set_value(self, v):
      self.value = v

   def add(self, p):
      self.value += p
   
   def subtract(self, p):
      self.value -= p

   def multiply(self, p):
      self.value *= p

   def divide(self, p):
      self.value /= p

i = IntegerDemo()
i.set_value(int(input("v: ")))
i.add(int(input("+: ")))
i.subtract(int(input("-: ")))
i.multiply(int(input("*: ")))
i.divide(int(input("/: ")))
print(i.value)
 
# 檔名: exercise0905.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0906.py


class FactorialDemo:
   def set_value(self, n):
      i = 1
      p = 1
      while i <= n:
         p *= i
         i += 1

      self.value = p

i = FactorialDemo()
i.set_value(int(input("n: ")))
print(i.value)
 
# 檔名: exercise0906.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise0907.py


class FibonacciDemo:
   def set_value(self, n):
      n1 = 0
      n2 = 1
      i = 3
      while i <= n:
         n3 = n1 + n2
         n1 = n2
         n2 = n3
         i += 1

      self.value = n3

i = FibonacciDemo()
i.set_value(int(input("n: ")))
print(i.value)
 
# 檔名: exercise0907.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

the end

沒有留言: