本站電子書

您可以在這些電子書店找到本站電子書: Google Play 圖書iBooks StoreHyReadReadmooPubu
===>>>本站推出學習遊戲意見調查<<<===《如何自學程式設計》入選 Google Play 台灣地區2016年度最佳書籍

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



class_demo4.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())
d.a = 5
print(d.do_something())
 
# 檔名: class_demo4.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

class_demo5.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.__a)
print(d.do_something())
d.__a = 5
print(d.__a)
print(d.do_something())
 
# 檔名: class_demo5.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise1001.py


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

   def get_value(self):
      return self.__value

   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.get_value())
 
# 檔名: exercise1001.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise1002.py


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

      self.__value = p

   def get_value(self):
      return self.__value

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

exercise1003.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

   def get_value(self):
      return self.__value

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

the end

沒有留言: