面向对象编程

面向对象

  • 步骤:
    OOA面向对象分析
    OOD面向对象设计
    OOP面向对象编程
  • 实现:
    1.分析对象特征行为
    2.写类描述对象模版
    3.实例化,模拟过程
  • 特征:
    封装
    继承
    多态
import datetime
class Book:
    def __init__(self,title,price,author,publisher,pubdate):
        self.title = title
        self.price = price
        self.author = author
        self.publisher = publisher
        self.pubdate = pubdate

book1 = Book('Python入门',29.9,'Tom','清华出版',datetime.date(2019,7,4))
print(book1.title)
print(book1.price)

Python入门
29.9

import datetime
class Book:
    def __init__(self,title,price,author,publisher,pubdate):
        self.title = title
        self.price = price
        self.author = author
        self.publisher = publisher
        self.pubdate = pubdate

    def print_info(self):
        print('当前这本书的信息如下:')
        print('标题:{}'.format(self.title))
        print('定价:{}'.format(self.price))
        print('作者:{}'.format(self.author))
        print('出版社:{}'.format(self.publisher))
        print('出版时间:{}'.format(self.pubdate))


book1 = Book('Python入门',29.9,'Tom','清华出版',datetime.date(2019,7,4))
book1.print_info()

当前这本书的信息如下:
标题:Python入门
定价:29.9
作者:Tom
出版社:清华出版
出版时间:2019-07-04

#新创建一个views.py文件,内容如下
class Book:
    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)

import views
import importlib
importlib.reload(views)

<module ‘views’ from ‘D:\CARD\untitled\views.py’>

b = views.Book('C#')  #调用的是def __repr__(self)
b

《图书:C# at 0x2062416746536》

print(b)  #调用的是def __str__(self)

[图书:C#,定价:0.0]

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 print_info(self):
        print(self.title,self.price,self.count)

if __name__ == '__main__':
    book = Book('Python经典', price=29.0, author='Tom')
    book2 = Book('Flask')
    book3 = Book('ASP.net')
    print('图书数量:{}'.format(Book.count))

图书数量: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 ArithmeticError('禁止赋值年龄!')
    @age.deleter
    def age(self):
        raise ArithmeticError('年龄不能删除')

if __name__ == '__main__':
    s  = Student('Tom', datetime.date(1992,3,1))
    print(s.birthdate)
    print(s.age)

1992-03-01
27

# 更改年龄
if __name__ == '__main__':
    s  = Student('Tom', datetime.date(1992,3,1))
    print(s.birthdate)
    print(s.age)
    s.birthdate = datetime.date(1982,8,2)
    s.age = 20
    print (s.birthdate)
    print(s.age)

1992-03-01
File “D:/CARD/untitled/1111.py”, line 23, in
27
s.age = 20
File “D:/CARD/untitled/1111.py”, line 13, in age
raise ArithmeticError(‘禁止赋值年龄!’)
ArithmeticError: 禁止赋值年龄!

if __name__ == '__main__':
    s  = Student('Tom', datetime.date(1992,3,1))
    print(s.birthdate)
    print(s.age)
    del(s.age)
    print(s.age)

1992-03-01
Traceback (most recent call last):
27
File “D:/CARD/untitled/1111.py”, line 26, in
del(s.age)
File “D:/CARD/untitled/1111.py”, line 16, in age
raise ArithmeticError(‘年龄不能删除’)
ArithmeticError: 年龄不能删除

if __name__ == '__main__':   #但是name可以被删除
    del(s.name)
    print(s.name)

File “D:/CARD/untitled/1111.py”, line 27, in
print(s.name)
AttributeError: ‘Student’ object has no attribute ‘name’

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.get_area()))

圆的面积是:50.26548245743669

import datetime
class Employee:
    def __int__(self,department,name,birthdate,salary):
        self.department = department
        self.name = name
        self.birthdate = birthdate
        self.salary = salary

    def give_raise(self,percent,bouns = 0):
        self.salary = self.salary* (1 + percent + bouns)

    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))

if __name__ =='main':
    p = Programer ('技术部','Peter',datetime.date(1990,3,3),8000,'Flask','CRM')
    print (p)
    print(p.department)
    print(p.salary)
    p.give_raise(.2,.1)
    print(p.salary)
    p.working()

<员工:Peter>
技术部
8000
10400.0
程序员:Peter在开发项目:CRM

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值