class 8

类的特征:封装;继承;多态(同一类型对同一消息的不同的反应能力)

封装

如何计数(不要有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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值