本站電子書

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

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




class_demo6.py


class Demo:
   def __init__(self, v1=11, v2=22):
      self.__a = v1
      self.__b = v2
   
   def do_something(self):
      return self.__a + self.__b

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

class_demo7.py


class Demo:
   def __init__(self, v1=11, v2=22):
      self.__a = v1
      self.__b = v2

   def get_a(self):
      return self.__a

   def get_b(self):
      return self.__b

   def set_a(self, value):
      self.__a = value

   def set_b(self, value):
      self.__b = value
   
   def do_something(self):
      return self.__a + self.__b

d = Demo(11)
print(d.do_something())
d.set_a(5)
print(d.do_something())

# 檔名: class_demo7.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

class_demo8.py


class Demo:
   def __init__(self, v1=11, v2=None):
      if v2 is None:
         self.__a = v1
         self.__b = v1
      else:
         self.__a = v1
         self.__b = v2

   def do_something(self):
      return self.__a + self.__b

d1 = Demo()
print(d1.do_something())
d2 = Demo(13, 12)
print(d2.do_something())
 
# 檔名: class_demo8.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise1101.py


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

   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(int(input("v: ")))
i.add(int(input("+: ")))
i.subtract(int(input("-: ")))
i.multiply(int(input("*: ")))
i.divide(int(input("/: ")))
print(i.get_value())
 
# 檔名: exercise1101.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise1102.py


class FactorialDemo:
   def __init__(self, n):
      self.set_value(n)

   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(int(input("n: ")))
print(i.get_value())
 
# 檔名: exercise1102.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

exercise1103.py


class FibonacciDemo:
   def __init__(self, n):
      self.set_value(n)

   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(int(input("n: ")))
print(i.get_value())
 
# 檔名: exercise1103.py 
# 作者: Kaiching Chang 
# 時間: July, 2014

the end

沒有留言: