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




class_demo6.py


1
2
3
4
5
6
7
8
9
10
11
12
13
14
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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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

沒有留言: