class rectangle:
def __init__(self, c, k):
self.__c = c
self.__k = k
def area(self):
"""面積"""
return self.__k * self.__c
def Perimeter(self):
"""周長"""
return 2 * (self.__k + self.__c)
rectangle1 = rectangle(10, 20)
print("面積", rectangle1.area())
print("周長", rectangle1.Perimeter())
class countscore:
def __init__(self, java, sql, web):
self.java = java
self.sql = sql
self.web = web
def jisuan(self):
a = self.web + self.sql + self.java
b = a / 3
return a, b
countscore = countscore(80, 90, 70)
a, b = countscore.jisuan()
print(f"總成績:{a} 平均分:{b}")
class score:
def __init__(self,name, python,c,java):
self.name =name
self.python = python
self.java = java
self.c = c
def panduan(sco):
if sco >= 90:
print("A")
elif sco >= 80:
print("B")
elif sco >= 60:
print("C")
else:
print("D")
ls =[]
for i in range(1):
sc = "s"+str(i)
name = input("請輸入名字:")
python,c,java = map(int, input("請輸入python,c,java成績(空格隔開):").split())
sc = score(name,python,c,java)
ls.append(sc)
for i in ls:
print(i.name)
print("python:",end=" ")
score.panduan(i.python)
print("java:", end=" ")
score.panduan(i.java)
print("c:",end=" ")
score.panduan(i.c)
print()
銀行卡: 卡號、密碼、姓名、余額
方法:取款
本行卡:
方法:取款、轉賬
其它行卡:
方法:取款(加收2元手續費)
實例化對象進行測試
ls = [(123,123456,"hu",99999),(789,147258,"q",888888)]
class bank:
def __init__(self,kars,passed):
self.kars = kars
self.passwd = passed
self.yu =99999
def qu(self,money):
if self.jiaoyin():
if money<self.yu:
self.yu -= money
print(f"取錢:{money}")
print(f"余額為:{self.yu}")
else:
print("余額不足")
def jiaoyin(self):
tu = (self.kars,self.passwd)
for i in ls:
if tu == i[0:2]:
print(f"{i[2]}登錄成功")
return True
else:
print("卡號或密碼錯誤")
return False
def cun(self,money):
self.yu += money
def cha(self):
print("余額為:",self.yu)
class self_bank(bank):
def __init__(self,kars,passed):
super(self_bank,self).__init__(kars,passed)
def turn(self,money,name):
if money < self.yu:
self.yu -= money
print(f"轉賬:{money}")
print(f"余額為:{self.yu}")
print(f"向{name}轉賬成功")
else:
print("余額不足")
class other_bank(bank):
def __init__(self,kars,passed):
super(other_bank, self).__init__(kars,passed)
def qu(self,money):
if self.jiaoyin():
if money<self.yu:
self.yu -= (money+2)
print(f"取錢:{money}")
print("手續費2元")
print(f"余額為:{self.yu}")
else:
print("余額不足")
print("歡迎來到登錄頁面")
kars = int(input("請輸入卡號:"))
pwd = int(input("請輸入密碼:"))
bank1 = self_bank(kars,pwd)
bank2 = other_bank(kars,pwd)
q = bank1.jiaoyin()
def d(j):
print("**********************************************")
print("1、查詢余額 2、存款 3、取款 4、轉賬 5、退出")
print("**********************************************")
a = int(input("請輸入:"))
if a == 1:
j.cha()
elif a == 2:
money = int(input("請輸入存款金額:"))
j.cun(money)
elif a== 3:
money = int(input("請輸入取款金額:"))
j.qu()
elif a == 4:
money = int(input("請輸入轉賬金額:"))
person = input("請輸入轉入的用戶名:")
j.turn(money,person)
elif a == 5:
q = False
while q:
d(bank1)
主人Master:請好友吃飯,陪好友玩 好友Friend: 中國朋友:吃餃子,表演太極拳 美國朋友:吃披薩,踢足球 試用程序模擬
class Master:
def __init__(self):
pass
def play(self):
print("請好友吃飯,陪好友玩")
class China_Friend(Master):
def __init__(self):
pass
def play(self):
print("吃餃子,表演太極拳")
class American_Friend(Master):
def __init__(self):
pass
def play(self):
print("吃披薩,踢足球")
def friend(friend:Master):
friend.play()
ch = China_Friend()
am = American_Friend()
friend(ch)
friend(am)