140 lines
3.3 KiB
Python
140 lines
3.3 KiB
Python
|
|
# ------------------- 繼承 -------------------------
|
|
print(f'------------------- 繼承 -------------------------')
|
|
class Lab_location:
|
|
def __init__(self):
|
|
self.location = "EL125"
|
|
|
|
|
|
class Lab_student(Lab_location):
|
|
def __init__(self,student):
|
|
super().__init__()
|
|
self.student = student
|
|
|
|
|
|
def lab_detail(self):
|
|
print(f'student : {self.student}') # 原本定義的
|
|
print(f'Lab_位置 : {self.location}') # 可以從Lab_location 抓出物件
|
|
|
|
student1 = Lab_student("M11112030")
|
|
student1.lab_detail()
|
|
|
|
# ---------------------封裝---------------------------
|
|
print(f'------------------- 封裝 -------------------------')
|
|
|
|
class Lab_student:
|
|
def __init__(self,name):
|
|
self.name = name
|
|
self.profession = "警察"
|
|
|
|
def student_name(self):
|
|
print(f'name : {self.name}')
|
|
|
|
|
|
def crime(self):
|
|
print(f'{self.name} 要來逮捕你了')
|
|
|
|
def __no_crime(self):
|
|
print(f'{self.name} 從你身邊路過')
|
|
|
|
|
|
|
|
|
|
studen1 = Lab_student("王希名")
|
|
studen1.student_name()
|
|
studen1.crime()
|
|
#studen1.__no_crime()
|
|
|
|
|
|
# ----------------------多型-----------------------
|
|
|
|
print(f'------------------- 多型 -------------------------')
|
|
class Lab_location:
|
|
def __init__(self):
|
|
self.location = "EL125"
|
|
|
|
class A1_student(Lab_location):
|
|
def __init__(self):
|
|
super().__init__()
|
|
def lab_detail(self):
|
|
print(f'M11112030 在 {self.location}')
|
|
|
|
class A2_student(Lab_location):
|
|
def __init__(self):
|
|
super().__init__()
|
|
def lab_detail(self):
|
|
print(f'M11112031 在 {self.location}')
|
|
|
|
class A3_student(Lab_location):
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
|
|
def lab_detail(self):
|
|
print(f'M11112032 在 {self.location}')
|
|
|
|
student1 = A1_student()
|
|
student2 = A2_student()
|
|
student3 = A3_student()
|
|
|
|
student1.lab_detail()
|
|
student2.lab_detail()
|
|
student3.lab_detail()
|
|
|
|
# ----------------多型---------------
|
|
print(f'------------------- 多型 -------------------------')
|
|
class Lab_location:
|
|
def __init__(self):
|
|
self.location = "EL125"
|
|
|
|
class Lab_student(Lab_location):
|
|
def __init__(self,name):
|
|
super().__init__()
|
|
self.name = name
|
|
def lab_detail(self):
|
|
print(f'{self.name} 在 {self.location}')
|
|
|
|
student1 = Lab_student("A")
|
|
student2 = Lab_student("B")
|
|
student3 = Lab_student("C")
|
|
|
|
student1.lab_detail()
|
|
student2.lab_detail()
|
|
student3.lab_detail()
|
|
|
|
# --------------------------------class-----------------------
|
|
print(f'------------------- class -------------------------')
|
|
class Person():
|
|
def __init__(self,name,age,weight,high):
|
|
self.eye = 2
|
|
self.hand = 2
|
|
self.noise = 1
|
|
self.leg = 2
|
|
self.money =0
|
|
self.name = name
|
|
self.age = age
|
|
self.weight = weight
|
|
self.high = high
|
|
def person_body_detail(self):
|
|
print(f'眼睛數量 : {self.eye}')
|
|
print(f'手數量 : {self.hand}')
|
|
print(f'鼻子數量 : {self.noise}')
|
|
print(f'腿數量 : {self.leg}')
|
|
def person_introduce(self):
|
|
high = float(self.high/100)
|
|
print(f'{self.name} {self.age}歲 {self.weight}公斤 {high}米 總資產{self.money}')
|
|
|
|
|
|
test_1 = Person(name='王希銘',age = 37,weight=105,high=168)
|
|
test_1.money = 20000
|
|
test_1.person_body_detail()
|
|
test_1.person_introduce()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|