一、面向对象
1、特征
(1)封装
(2)集成
(3)多态
例如:
class Book:
count =0
def init(self,title,price =0.0,author =None):
self.title =title
self.price =price
self.author =author
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’:
book =Book(‘Python经典’,price =29.0,author =‘Tom’)
Book.count +=1
book2 =Book(‘Flask’)
Book.count +=1
book3 =Book(‘ASP.net’)
Book.count +=1
print('图书数量:{}'.format(Book.count))
—————————————————————————————
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’:
book =Book(‘Python经典’,price =29.0,author =‘Tom’)
book2 =Book(‘Flask’)
book3 =Book(‘ASP.net’)
print('图书数量:{}'.format(Book.count))
———————————————————————————————————
销毁一个对象使用全局函数del()
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’:
book =Book(‘Python经典’,price =29.0,author =‘Tom’)
book2 =Book(‘Flask’)
book3 =Book(‘ASP.net’)
del(book3)
print('图书数量:{}'.format(Book.count))
—————————————————————————————
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 __del__(self):
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’:
book =Book(‘Python经典’,price =29.0,author =‘Tom’)
book2 =Book(‘Flask’)
book3 =Book(‘ASP.net’)
del(book3)
print('图书数量:{}'.format(Book.count))
—————————————————————————————
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 __del__(self):
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)
def cls_method(cls):
print('类函数')
if name ==‘main’:
book =Book(‘Python经典’,price =29.0,author =‘Tom’)
book2 =Book(‘Flask’)
book3 =Book(‘ASP.net’)
Book.cls_method(book2)
—————————————————————————————
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 __del__(self):
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)
def cls_method(cls):
print('类函数')
def static_method():
print('静态函数,逻辑上与实例无关')
if name ==‘main’:
book =Book(‘Python经典’,price =29.0,author =‘Tom’)
book2 =Book(‘Flask’)
book3 =Book(‘ASP.net’)
Book.cls_method(book2)
Book.static_method()
—————————————————————————————
import datetime
class Student:
def init(self,name,birthday):
self.name =name
self.birthday = birthday
def get_age(self):
return datetime.date.today().year -self.birthday.year
if name == ‘main’:
s = Student(‘Tom’,datetime.date(1992,3,1))
print(s.birthday)
print(s.get_age())
—————————————————————————————
import datetime
class Student:
def init(self,name,birthday):
self.name =name
self.birthday = birthday
@property #装饰器,表示属性,不受 __init__的影响
def age(self):
return datetime.date.today().year -self.birthday.year
if name == ‘main’:
s = Student(‘Tom’,datetime.date(1992,3,1))
print(s.birthday)
print(s.age)
来源:CSDN
作者:DAN_L
链接:https://blog.csdn.net/DAN_L/article/details/104167666