Python概述(8)
面向对象
特征
- 封装
- 继承
- 多态
# 默认值参数放在命名参数后面
def _init_(self,title,price=0.0,author=None):
self.title = title
self.price = price
self.author = author
def _repr_(self):
# 0x表示16进制,id(self)表示全局变量
return '<图书:{} at 0x{}>'.format(self.title,id(self))
def _str_(self):
return '<图书:{},定价:{}>'.format(self.title,self.price)
def print_info(self):
print(self.title,self.price,self.author)
# if _name_ == '_main_':
# book = Book('Python经典',price=29.9,author='Tom')
# book.print_info()
b = main.Book('C#')
print(b)
Output: <图书:C# at 0x51767664>
Class Book:
count = 0
def _init_(self,title,price=0.0,author=None):
self.title = title
self.price = price
self.author = author
Book.count += 1
def _repr_(self):
return '<图书:{} at 0x{}>'.format(self.title,id(self))
def _str_(self):
return '<图书:{},定价:{}>'.format(self.title,self.price)
def print_info(self):
print(self.title,self.price,self.author)
if _name_ == '_main_':
book1 = Book('Python')
book2 = Book('C#')
book3 = Book('Java')
del(book3)
print('图书数量:{}'.format(Book.count))
Output: 图书数量:3
import datetime
class Student:
def _init_(self,name,birthdate):
self.name = name
self.birthdate = birthdate
@property
def age(self):
return datetime.date.today().year - self.birthdate.year
@age.setter
def age(self,value):
raise AttributeError('禁止赋值年龄!')
@age.deleter
def age(self):
raise AttributeError('年龄不能删除')
if _name_ == '_main_':
s = Student('Tom',datetime.date(1992,3,1))
print(s.birthdate)
print(s.age)
s.birthdate = datetime.date(1992,8,2)
## name可以删除,但是age不可以
# del(s.name)
# print(s.name)
# Output: AttributeError: 'Student' object has no attribute 'name'
del(s.age)
print(s.age)
Output: AttributeError: 年龄不能删除
import math
class Circle:
def _init_(self,radius):
self.radius = radius
@property
def area(self):
return math.pi * self.radius ** 2
# def get_area(self):
# return math.pi * self.radius ** 2
c = Circle(4.0)
print("圆的面积是:{}".format(c.area))
Example:
import datetime
class Employee:
def _init_(self,department,name,birthdate,salary):
self.department = department
self.name = name
self.birthdate = birthdate
self.salary = salary
@property
def age(self):
return datetime.date.today().year - self.birthdate.year
def give_raise(self,percent,bonus=.0):
self.salary = self.salary * (1 + percent + bonus)
def _repr_(self):
return '<员工:{}>'.format(self.name)
def working(self):
print('员工:{},在工作...'.format(self.name))
# 继承
class Programer(Employee):
def _init_(self,department,name,birthdate,salary,specialty,project):
super()._init_(department,name,birthdate,salary)
self.salary = specialty
self.project = project
def working(self):
print('程序员:{}在开发项目:{}...'.format(self.name,self.project))
class HR(Employee):
def _init_(self,department,name,birthdate,salary,specialty,qualification_level = 1):
Employee._init_(self,department,name,birthdate,salary)
self.qualification_level = qualification_level
def working(self):
print('人事:{}正在面试新员工...'.format(self.name))
if _name_ == '_main_':
p = Programer('技术部',‘Peter’,datetime.date(1990,3,3),8000,'Flask','CRM')
print(p.department)
print(p.salary)
p.give_raise(.2,.1)
print(p.salary)
p.working()
print(p.page)
hr = HR('人事部','Marry',datetime.date(1992,4,4),6000,qualification_level=1)
hr.give_raise()
print(hr.salary)
hr.working()
Output:
<员工:Peter>
技术部
8000
10400.0
程序员:Peter在开发项目: CRM…
26
6600.000000000001
人事: Marry正在面试新员工…
来源:CSDN
作者:晨曦skyyyy
链接:https://blog.csdn.net/mingxisky/article/details/103483342