程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python object oriented

編輯:Python

1、 Define a rectangular class , Find perimeter and area

class rectangle:
def __init__(self, c, k):
self.__c = c
self.__k = k
def area(self):
""" area """
return self.__k * self.__c
def Perimeter(self):
""" Perimeter """
return 2 * (self.__k + self.__c)
rectangle1 = rectangle(10, 20)
print(" area ", rectangle1.area())
print(" Perimeter ", rectangle1.Perimeter())

2、 Custom class , Calculation java,sql,web Total score and average score of the three courses

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" Total score :{a} average :{b}")

3、 Create a student class , Store student's name ,python、c、java achievement , Then define a list store 5 A student , Enter student information in turn , Output the information of all students , Grades are displayed in grades (90 The above is A;80-90 by B,60-80 by C;60 The following is a D

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(" Please enter a name :")
python,c,java = map(int, input(" Please enter python,c,java achievement ( Whitespace separated ):").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()

4、 Inheritance practice

Bank card : Card number 、 password 、 full name 、 balance
Method : Withdraw money
Bank card :
Method : Withdraw money 、 Transfer accounts
Other bank cards :
Method : Withdraw money ( charge an additional 2 Dollar handling fee )
Instantiate the object for testing

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" Withdraw money :{money}")
print(f" The balance is :{self.yu}")
else:
print(" Lack of balance ")
def jiaoyin(self):
tu = (self.kars,self.passwd)
for i in ls:
if tu == i[0:2]:
print(f"{i[2]} Login successful ")
return True
else:
print(" Wrong card number or password ")
return False
def cun(self,money):
self.yu += money
def cha(self):
print(" The balance is :",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" Transfer accounts :{money}")
print(f" The balance is :{self.yu}")
print(f" towards {name} Transfer succeeded ")
else:
print(" Lack of balance ")
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" Withdraw money :{money}")
print(" Service Charge 2 element ")
print(f" The balance is :{self.yu}")
else:
print(" Lack of balance ")
print(" Welcome to the login page ")
kars = int(input(" Please enter the card number :"))
pwd = int(input(" Please input a password :"))
bank1 = self_bank(kars,pwd)
bank2 = other_bank(kars,pwd)
q = bank1.jiaoyin()
def d(j):
print("**********************************************")
print("1、 Check the balance 2、 deposit 3、 Withdraw money 4、 Transfer accounts 5、 sign out ")
print("**********************************************")
a = int(input(" Please enter :"))
if a == 1:
j.cha()
elif a == 2:
money = int(input(" Please enter the deposit amount :"))
j.cun(money)
elif a== 3:
money = int(input(" Please enter the withdrawal amount :"))
j.qu()
elif a == 4:
money = int(input(" Please enter the transfer amount :"))
person = input(" Please enter the user name of the transfer in :")
j.turn(money,person)
elif a == 5:
q = False
while q:
d(bank1)

5、 Polymorphic exercises

master Master: Invite friends to dinner , Play with friends Good friends Friend: Chinese friends : Eat dumplings , Perform Taijiquan American friends : Eat pizza , play football Try program simulation

class Master:
def __init__(self):
pass
def play(self):
print(" Invite friends to dinner , Play with friends ")
class China_Friend(Master):
def __init__(self):
pass
def play(self):
print(" Eat dumplings , Perform Taijiquan ")
class American_Friend(Master):
def __init__(self):
pass
def play(self):
print(" Eat pizza , play football ")
def friend(friend:Master):
friend.play()
ch = China_Friend()
am = American_Friend()
friend(ch)
friend(am)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved