面向对象编程OOP 02

时光总嘲笑我的痴心妄想 提交于 2020-02-04 22:20:17

一、面向对象
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)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!