python
Image Not Showing
Possible Reasons
Learn More →
類別(class):
物件(object):
屬性(properties):
方法(functions):
定義(definition):
宣告(declaration):
公開(public):
私有(private):
定義一個類別叫做「車」,實作「汽車、卡車」物件
class Vehicle:
doors = 4
max_speed = 160
fuel = '95'
car = Vehicle()
print('car.doors: ', car.doors)
print('car.max_speed: ', car.max_speed)
print('car.fuel: ', car.fuel)
truck = Vehicle()
print('truck.doors: ', truck.doors)
print('truck.max_speed: ', truck.max_speed)
print('truck.fuel: ', truck.fuel)
__init__():
class Vehicle:
def __init__(self, doors, max_speed, fuel):
self.doors = doors
self.max_speed = max_speed
self.fuel = fuel
car = Vehicle(4, 200, '95')
print('car.doors: ', car.doors)
print('car.max_speed: ', car.max_speed)
print('car.fuel: ', car.fuel)
print()
truck = Vehicle(2, 160, '柴油')
print('truck.doors: ', truck.doors)
print('truck.max_speed: ', truck.max_speed)
print('truck.fuel: ', truck.fuel)
類別中的函數若要指定是該類別的屬性,記得要加self
沒加self會被認為是函數內的變數
class Car:
doors = 4
def set(self, abc):
doors = abc
car = Car()
print(car.doors) # 4
car.set(5)
print(car.doors) # 4
class Car:
doors = 4
def set(self, abc):
self.doors = abc
car = Car()
print(car.doors) # 4
car.set(5)
print(car.doors) # 5
class Mobile:
def __init__(self):
self.__password = '12345'
self.__alarm = None
def set_alarm(self, time):
if time > 9 and time < 23:
self.__alarm = time
return 'alarm is setting'
else:
return 'time is not accept'
def show_password(self):
name = input('input your name: ')
if name == 'amos':
return self.__password
else:
return 'your are not owner'
myphone = Mobile()
time = int(input('alarm time: '))
print(myphone.set_alarm(time))
繼承(inheritance)
class Animal:
def __init__(self, head, hand, foot):
self.head = head
self.hand = hand
self.foot = foot
def run(self):
return '使用身體快速移動'
class Human(Animal):
def run(self):
return '使用腳奔跑'
class Bird(Animal):
def __init__(self, head, wing, foot):
self.head = head
self.wing = wing
self.foot = foot
def run(self):
return '使用雙腳奔跑'
def fly(self):
return '使用翅膀飛翔'
animal = Animal(1, 0, 4)
animal.run()
human = Human(1, 2, 2)
human.run()
bird = Bird(1, 2, 2)
bird.run()
bird.fly()
引用上一輩的初始化方法
class Animal:
def __init__(self, head, hand, foot):
self.head = head
self.hand = hand
self.foot = foot
def run(self):
return '使用身體快速移動'
class Monkey(Animal):
def __init__(self, head, hand, foot, food='banana'):
super().__init__(head, hand, foot) # 呼叫上一輩的初始化方法
self.food = food
animal = Animal(1, 0, 4)
print(animal.run())
monkey = Monkey(1, 2, 2, 'fruit')
print(monkey.food)
print(monkey.head)
print(monkey.run())
鍊狀繼承(chained inheritance)
class Animal:
def __init__(self, head, hand, foot):
self.head = head
self.hand = hand
self.foot = foot
def run(self):
return '使用身體快速移動'
class Human(Animal):
def run(self):
return '使用腳奔跑'
class Bird(Animal):
def __init__(self, head, wing, foot):
self.head = head
self.wing = wing
self.foot = foot
def run(self):
return '使用雙腳奔跑'
def fly(self):
return '使用翅膀飛翔'
class Penguin(Bird):
def fly(self):
return '無法使用翅膀飛翔'
def swim(self):
return '使用翅膀游泳'
animal = Animal(1, 0, 4)
print(animal.head, animal.hand, animal.foot)
print(animal.run())
human = Human(1, 2, 2)
print(human.head, human.hand, human.foot)
print(human.run())
bird = Bird(1, 2, 2)
print(bird.head, bird.wing, bird.foot)
print(bird.run())
print(bird.fly())
penguin = Penguin(1, 2, 2)
print(penguin.head, penguin.wing, penguin.foot)
print(penguin.run())
print(penguin.fly())
print(penguin.swim())
多重繼承(multiple inheritance)
class mobile:
def __init__(self, phone_number):
self.phone_number = phone_number
def call(self, number):
return '撥打' + number
def receive(self, number):
return '接通' + number + '來電'
class PDA:
def __init__(self, note):
self.note = '備忘錄'
def install(self, app_name):
return '安裝' + app_name + '軟體'
def uninstall(self, app_name):
return '移除' + app_name + '軟體'
class SmartPhone(mobile, PDA):
pass
smartPhone = SmartPhone('12345')
print(smartPhone.phone_number)
print(smartPhone.call('77788899944455'))
print(smartPhone.receive('444551122'))
print(smartPhone.install('台北等公車'))
print(smartPhone.uninstall('台北等公車'))
class Account:
def __init__(self, number, name):
self.number = number
self.name = name
self.balance = 0
def deposit(self, amount): #存款動作: amount代表存入金額
if amount <= 0:
raise ValueError('must be positive')
self.balance += amount
def withdraw(self, amount): #取款動作: amount代表取款金額
if amount <= self.balance:
self.balance -= amount
else:
raise RuntimeError('balance not enough')
acct1 = Account('123–456–789', 'Justin') #開一個帳戶
acct1.deposit(100)
acct1.withdraw(30)
print(acct1.balance) #餘額是 70
軟體:
class Software:
def __init__(self):
self.__name = 'admin'
self.__password = 'admin'
self.__article = None
self.__logined = False
self.__content = ''
def login(self):
name = input('請輸入帳號: ')
password = input('請輸入密碼: ')
if self.__name == name and self.__password == password:
self.__logined = True
print('you are login!', self.__logined)
else:
print('帳號或密碼錯誤,請重新輸入')
def logout(self):
self.__logined = False
print('you are logout', self.__logined)
def change_pw(self):
if self.__logined:
new_password = input('請輸入新密碼: ')
new_password2 = input('請再輸入新密碼: ')
if new_password == new_password2:
self.__password = new_password
print(self.__password)
else:
print('上下密碼不一致,請重新輸入')
else:
self.login()
def post(self):
if self.__logined:
self.__content = input('請輸入文章內容: ')
else:
self.login()
def read(self):
if self.__logined:
print(self.__content)
else:
self.login()
mydictionary = Software()
mydictionary.login()
# mydictionary.logout()
# mydictionary.change_pw()
mydictionary.post()
mydictionary.read()