类的特征:封装;继承;多态(同一类型对同一消息的不同的反应能力)
封装
如何计数(不要有self,这样就是全局的函数)
class book:
count = 0
def __init__(self, title, price=0, author=None):
self.title = title
self.price = price
self.author = author
book.count += 1
def __repr__(self):
return 'book:{}'.format(self.title)
def __str__(self):
return '[book:{}]'.format(self.title)
def a(self):
print(self.title, self.price, self.author)
book1 = book('hahaha')
book2 = book('xxx')
book3 = book('yy')
print(book.count)
3
repr
class haha:
def __init__(self,a,b = 0,c = None):
self.a = a
self.b = b
self.c = c
def f(self):
print(self.a,self.b,self.c)
import main
y = main.haha('papa')
y.a
'papa'
class haha:
def __init__(self,a,b = 0,c = None):
self.a = a
self.b = b
self.c = c
def __repr__(self):
return 'what is {}'.format(self.a)
def f(self):
print(self.a,self.b,self.c)
x = main.haha('bala')
x
<main.haha object at 0x000002A2501FFF08>
import importlib
importlib.reload(main)
<module 'main' from 'D:\\hello world\\pycharm\\venv\\main.py'>
x = main.haha('pp')
x
what is pp
str
class haha:
def __init__(self,a,b = 0,c = None):
self.a = a
self.b = b
self.c = c
def __repr__(self):
return 'what is {}'.format(self.a)
def __str__(self):
return 'str {}'.format(self.a)
importlib.reload(main)
<module 'main' from 'D:\\hello world\\pycharm\\venv\\main.py'>
x = main.haha('bb')
x
what is bb
print(x)
str bb
如何设置函数,使其只与类有关,与实例无关
class haha:
def __init__(self,a,b = 0,c = None):
self.a = a
self.b = b
self.c = c
def static_method():
print('静态函数,逻辑上与实例无关')
属性:代替某一类特殊的方法完成计算的功能
import datetime
class person:
def __init__(self,name,birthday):
self.name = name
self.birthday = birthday
@property
def age(self):
return datetime.date.today().year - self.birthday.year
@age.setter
def age(self,value):
raise AttributeError('禁止赋值年龄')
if __name__ == '__main__':
x = person('lily', datetime.date(1995, 8, 8))
print(x.age)
24
继承
import datetime
class person:
def __init__(self,name,birthday):
self.name = name
self.birthday = birthday
@property
def age(self):
return datetime.date.today().year - self.birthday.year
@age.setter
def age(self,value):
raise AttributeError('禁止赋值年龄')
def __repr__(self):
return 'he is {}'.format(self.name)
class humen(person):
def __init__(self,name,birthday,gender,job):
super().__init__(gender,job)
self.gender = gender
self.job = job
if __name__ == '__main__':
x = person('lily', datetime.date(1995, 8, 8))
print(x.age)
y = humen('tom',datetime.date(1995,8,9),'boy','heart')
print(y)
import datetime
class JOB:
def __init__(self,job,title,place):
self.job = job
self.title = title
self.place = place
def __repr__(self):
return '{}'.format(title)
class person:
def __init__(self,name,birthday):
self.name = name
self.birthday = birthday
@property
def age(self):
return datetime.date.today().year - self.birthday.year
@age.setter
def age(self,value):
raise AttributeError('禁止赋值年龄')
def __repr__(self):
return 'he is {}'.format(self.name)
class humen(person):
def __init__(self,name,birthday,gender,job:JOB):
super().__init__(gender,job)
self.gender = gender
self.job = job
if __name__ == '__main__':
x = JOB('heart','king','USA')
y = humen('tom',datetime.date(1995,8,9),'boy',x)
print(y.job)