封裝應用一:求圓的面積
import math
class circle:
definit(self,radius):
self.radius = radius
def get_area(self):
return math.pi * self.radius**2
c = circle(4.0)
print(『圓的面積是:{}』.format(c.get_area()))
返回的值為圓的面積是:50.26548245743669
—————————————————————————————
封裝應用二:求圓的面積
import math
class circle:
definit(self,radius):
self.radius = radius
@property #用屬性來進行宣告
def area(self):
return math.pi * self.radius ** 2
c = circle(4.0)
print(『圓的面積是:{}』.format(c.area))
返回的值為圓的面積是:50.26548245743669
—————————————————————————————
繼承應用一:
import datetime
class employee:
definit(self,department,name,birthday,salary):
self.department =department
self.name = name
self.birthday = birthday
self.salary = salary
@property
def age(self):
return datetime.date.today().year - self.birthday.year
def give_raise(self,percent,bonus=.0):
self.salary = self.salary * (1+percent+bonus)
defrepr(self):
return 『《員工:{}>』.format(self.name)
def working(self):
print(『員工:{},在工作…』.format(self.name))
class programer(employee): #繼承上面employee
definit(self,department,name,birthday,salary,specialty,project):
super().init(department,name,birthday,salary) #直接用上面__init__的資訊
self.specialty = specialty
self.project = project
def working(self):
print(『程式設計師:{}在開發專案{}…』.format(self.name,self.project))
class hr(employee):
definit(self,department,name,birthday,salary,qualification_level =1):
employee.init(self,department,name,birthday,salary)
self.qualification_level = qualification_level
def working(self):
print(『人事:{}正在面試新員工…』.format(self.name))
ifname== 『main』:
p = programer(『技術部』,『peter』,datetime.date(1990,3,3),8000,『flask』,『crm』)
print§ #__repr__return的值
print(p.department)
print(p.salary)
p.give_raise(.2,.1)
print(p.salary)
p.working()
print(p.age)
hr = hr(『人事部』,『marry』,datetime.date(1992,4,4),6000,qualification_level =3)
hr.give_raise(.1)
print(hr.salary)
hr.working()
————————————————————————————
繼承應用二:
import datetime
class department:
definit(self,department,phone,manager):
self.department = department
self.phone = phone
self.manager = manager
defrepr(self):
return 『《部門:{}>』.format(self.department)
class employee:
definit(self,department:department,name,birthday,salary):
self.department =department
self.name = name
self.birthday = birthday
self.salary = salary
@property
def age(self):
return datetime.date.today().year - self.birthday.year
def give_raise(self,percent,bonus=.0):
self.salary = self.salary * (1+percent+bonus)
defrepr(self):
return 『《員工:{}>』.format(self.name)
def working(self):
print(『員工:{},在工作…』.format(self.name))
class programer(employee): #繼承上面employee
definit(self,department,name,birthday,salary,specialty,project):
super().init(department,name,birthday,salary) #直接用上面__init__的資訊
self.specialty = specialty
self.project = project
def working(self):
print(『程式設計師:{}在開發專案{}…』.format(self.name,self.project))
class hr(employee):
definit(self,department,name,birthday,salary,qualification_level =1):
employee.init(self,department,name,birthday,salary)
self.qualification_level = qualification_level
def working(self):
print(『人事:{}正在面試新員工…』.format(self.name))
ifname== 『main』:
dep = department(『技術部』,『010-87718391』,『張大三』)
p = programer(dep,『peter』,datetime.date(1990,3,3),8000,『python,flask』,『xmall』)
p.give_raise(.2,.1)
print(p.salary)
print(p.department.phone)
print(p.department.manager)
物件導向程式設計(OOP)
物件導向程式設計 object oriented programming 作為一種新方法,其本質是以建立模型體現出來的抽象思維過程和物件導向的方法。模型是用來反映現實世界中事物特徵的。任何乙個模型都不可能反映客觀事物的一切具體特徵,只能對 事物特徵和變化規律的一種抽象,且在它所涉及的範圍內更普遍 更...
OOP物件導向程式設計
oop 達到了軟體工程的三個主要目標 重用性 靈活性和擴充套件性。元件 資料和功能一起在執行著的電腦程式中形成的單元,元件在 oop 電腦程式中是模組和結構化的基礎。抽象性 程式有能力忽略正在處理中資訊的某些方面,即對資訊主要方面關注的能力。封裝 也叫做資訊封裝 確保元件不會以不可預期的方式改變其它...
OOP 物件導向程式設計
作用 建立物件 初始化 基本概念 1.方法名和類名一致 2.沒有返回值宣告 3.呼叫 使用 new 關鍵字,本類中呼叫使用 this 4.預設有乙個無參構造,當宣告了乙個構造方法後,預設的不再提供 1.在乙個類中,方法名相同 2.引數列表不一樣 型別 順序 數量,跟引數名無關 3.跟返回值無關 三大...