目录
1. 课程目标
2. 属性
2.1 类属性
lesson为类属性,类属性可以通过类名称调用
类本身是对象
实例也是对象
所有的类都属于type类型
执行类的过程就是创建实例的过程,即类的实例化
类属性为静态属性
>>> class PyClass:
lesson = 'python'
def __init__(self, name):
self.name = name
>>> PyClass.lesson
'python'
>>> p = PyClass('laoqi')
>>> type(p)
<class '__main__.PyClass'>
>>> type(PyClass)
<class 'type'>
>>>
增加类属性
通过dir()函数查看发现类的属性中添加了一个book属性
>>> PyClass.book = 'learn python with laoqi'
>>> dir(PyClass)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'book', 'lesson']
>>>
2.2 实例属性
创建好的实例中比类属性多了个name属性,该属性为实例属性。
>>> dir(p)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'book', 'lesson', 'name']
>>>
在创建类时,初始化类中的self.name为实例属性
实例属性可以不同,
所有实例中的类属性一定是相同的,实例属性可以不同
>>> class PyClass:
lesson = 'python'
def __init__(self, name):
self.name = name
>>>
>>> p = PyClass('laoqi')
>>> p.book
'learn python with laoqi'
>>> p.lesson
'python'
>>> p.name
'laoqi'
>>> h = PyClass('laozhang')
>>> h.book
'learn python with laoqi'
>>> h.lesson
'python'
>>> h.name
'laozhang'
>>>
在实例中修改类属性的值时,只在该实例中修改了类属性的值
而类中的类属性值不变,其他实例的类属性值也不变
>>> p.book
'learn python with laoqi'
>>> PyClass.book
'learn python with laoqi'
>>> p.book = 'python book'
>>> p.book
'python book'
>>> PyClass.book
'learn python with laoqi'
>>> h.book
'learn python with laoqi'
>>>
当删除修改后的实例的类属性时,相当于将该实例的类属性还原为默认的类属性
>>> p.book
'python book'
>>> del p.book
>>> p.book
'learn python with laoqi'
>>>
删除类中的类属性
>>> del PyClass.book
>>> PyClass.book
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
PyClass.book
AttributeError: type object 'PyClass' has no attribute 'book'
>>> p.book
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
p.book
AttributeError: 'PyClass' object has no attribute 'book'
>>>
实例属性的属性会把同名的类属性遮盖掉,
3. 方法
3.1 方法与函数
初始化方法不要写return ,可以写return None
调用方法时,以f.method(2)为例,实际上传递了self 和2两个参数,self是隐式传递
>>> f = Foo()
>>> dir(f)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'method']
>>> f.method(2,3)
Traceback (most recent call last):
File "<pyshell#39>", line 1, in <module>
f.method(2,3)
TypeError: method() takes 2 positional arguments but 3 were given
>>> f.method(2)
4
>>>
显式传递参数
>>> Foo.method(f, 3)
9
>>> Foo.method(3)
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
Foo.method(3)
TypeError: method() missing 1 required positional argument: 'x'
>>>
类的方法不能单独调用
>>> method(f,3)
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
method(f,3)
NameError: name 'method' is not defined
>>>
4. 例题一
4.1 题目:写一个类,用阿拉伯数字实例化后,能够得到相应的罗马数字
4.2 程序代码
'''
写一个类,用阿拉伯数字实例化后,能够得到相应的罗马数字
'''
class Transformation_Number:
import numbers
numbers.Number
ROMAN = [
(1000, 'M'),
( 900, 'CM'),
( 500, 'D'),
( 400, 'CD'),
( 100, 'C'),
( 90, 'XC'),
( 50, 'L'),
( 40, ''),
( 10, 'Ⅹ'),
( 9, 'Ⅸ'),
( 5, 'Ⅴ'),
( 4, 'Ⅳ'),
( 1, 'Ⅰ')
]
def __init__(self, n):
self.n = n
def Arabic_exchange_Roman(self):
number = self.n
result = ""
for (arabic, roman) in self.ROMAN:
(factor, number) = divmod(number, arabic)
result += roman * factor
return result
# def Roman_exchange_Arabic(self, y):
a = Transformation_Number(1024)
print(a.Arabic_exchange_Roman())
4.3 运行结果
C:\Users\邢程\AppData\Local\Programs\Python\Python38\python.exe "D:/Python 项目/入门/2.1.2_example01.py"
MⅩⅩⅣ
Process finished with exit code 0
5. 作业
5.1 题目
网上购物,除了要支付商品总额,还要支付快递费用(暂不包括“包邮区”)。
假设某网上书店与某快递公司签订了固定的快递费用,每件(无论大小)都是5元。
而对于卖家,商家往往会在购买商品总额超过一定金额时免快递费。
编写类,根据图书的单价、购买数量及快递费,计算买家应支付的总额。
5.2 程序代码
我的代码
'''
网上购物,除了要支付商品总额,还要支付快递费用(暂不包括“包邮区”)。
假设某网上书店与某快递公司签订了固定的快递费用,每件(无论大小)都是5元。
而对于卖家,商家往往会在购买商品总额超过一定金额时免快递费。
编写类,根据图书的单价、购买数量及快递费,计算买家应支付的总额。
'''
class Book:
postage = 5
FreePostagePrice = 100
def __init__(self, unit_price, num):
self.unit_price= unit_price
self.num = num
def compute_total_price(self):
total_price = self.unit_price * self.num
if total_price >= self.FreePostagePrice:
return total_price
else:
return total_price+self.postage*self.num
a = Book(8,40)
print(a.compute_total_price())
c = Book(5,3)
print(c.compute_total_price())
5.3 运行结果
C:\Users\邢程\AppData\Local\Programs\Python\Python38\python.exe "D:/Python 项目/入门/2.1.2_job.py"
320
30
Process finished with exit code 0