类与对象:
创建对象:
class book:
title = '绘卷'
price = 1680
def printPrice(self, num):
print(self.title+ ':', num, '册',self.price *num, '日元')
book1 = book()
book1.printPrice(2)
绘卷: 2 册 3360 日元
转换器:
class Book:
def __init__(self, t, p):
self.title = t
self.price = p
def printPrice(self, num):
print(self.title+ ":", num, "册", self.price * num, '日元')
book1 = Book('绘卷', 1680)
book1.printPrice(2)
绘卷: 2 册 3360 日元
类的继承:
类具有继承其他类成员的功能,这个能力被称为类的对象。被继承的类称为超类(父类),而继承一方的类为子类
继承的定义:
class Book:
def __init__(self, t, p):
self.title = t
self.price = p
def printPrice(self, num):
print(self.title+ ':', num, "册", self.price *num, '日元')
class ColorBook(Book):
color = '紫'
book2 = ColorBook('绘卷', 1680)
book2.printPrice(2)
绘卷: 2 册 3360 日元
方法重写:听过记叙一段与继承的方法完全同名的方法,将这个继承过里啊的方法覆写的操作
class Book:
def __init__(self, t, p):
self.title = t
self.price = p
def printPrice(self, num):
print(self.title+ ":", num, self.price *num, '日元')
class ColorBook(Book):
color ='紫'
# 覆写的方法
def printPrice(self, num):
print(self.title+':', num, self.price *num, '日元')
print(self.color)
book2 = ColorBook('绘卷', 1680)
book2.printPrice(2)
超类:上边的代码,子类里也使用着与超类相同的代码,这样效率不高,因此引入超类super()
class Book:
def __init__(self, t, p):
self.title = t
self.price = p
def printPrice(self, num):
print(self.title+ ":", num,'册', self.price *num, '日元')
class ColorBook(Book):
color ='紫'
def printPrice(self, num):
super().printPrice(num)
print(self.color)
book2 = ColorBook('绘卷', 1680)
book2.printPrice(2)
属性:设计一个可以获取、修改、删除对象内部成员变量的值得方法,拥有这些功能的变量被称之为属性
隐藏变量:由于Python的成员变量可以从对象外部调用或者带入,所以请尽量百年外籍直接访问在属性中使用的值。如果想实现这样的一个需求,需要在变量名前面添加“__”的前缀
属性定义方式1:property()函数
class Book:
def __init__(self, t, p):
self.title = t
self.__price = p # 通过添加"__"前缀隐藏
# 获取器方法
def getPrice(self):
return self.__price
# 设定器方法
def setPrice(self):
self.__price = p
# 删除器方法
def delPrice(self):
self.__price = 0
price = property(fget = getPrice, fset = setPrice, fdel = delPrice, doc = '价格的属性')
book1 = Book('绘卷', 1680)
book1.__price = 2000 # 属性的设定 = 设定器方法的带调用
print(book1.price) # 显示属性的状态 = 获取器方法的调用
del(book1.price) # 属性的删除 = 删除器方法的调用
2000
属性定义方式2:装饰模式 @
class Book:
def __init__(self, t, p):
self.title = t
self.__price = p # 通过添加"__"前缀隐藏
# 声明属性并定义获取器方法
@property
def price(self):
return self.__price
# 定义设置器
@price.setter
def price(self, p):
self.__price = p
# 定义删除器
@price.deleter
def price(self):
self.__price = 0
book1 = Book('绘卷', 1680)
book1.price = 2000
print(book1.price)
del(book1.price)
类方法:
类方法:是一种不依赖对象的,迫使相同的类拥有相同的行为的方法
class Book:
@classmethod
def printMaxNum(cls):
print(20)
Book.printMaxNum()
类变量:生成对象后
class Book:
title = '绘卷'
price = '1680'
def printPrice(self, num):
print(self.title+ ":", num, "册", self.price *num, '日元')
book1 = Book()
# title price 是不依赖对象变量 称之为类变量
book1.title = '辞典'
book1.price = 2000
book1.printPrice(2)
print(Book.title)
print(Book.price)
辞典: 2 册 4000 日元
绘卷
1680
水果分类:创建表示水果的Fruit类及其子类Apple类、Orange类
# Fruit类
class Fruit():
taste = '好吃'
def __init__(self):
self.name = '水果'
self.weight = 0
self.color = '?'
def printData(self):
print('{}: 颜色={} 重量={}g'.format(self.name, self.color, self.weight))
# 苹果 类
class Apple(Fruit):
def __init__(self, name, weight, color):
self.name = name
self.weight = weight
self.color = color
@classmethod
def printTaste(cls):
print('甜甜的'+cls.taste)
# Orange 类
class Orange(Fruit):
def __init__(self, weight):
self.name = '橙子'
self.weight = weight
self.color = '橙子'
@classmethod
def printTaste(cls):
print('酸酸的' +cls.taste)
fruit = Fruit()
fruit.printData()
red_apple = Apple(name='红苹果', weight = 280, color='红')
red_apple.printData()
Apple.printData()
green_apple = Apple(name='青苹果',weight = 250, color ='绿')
green_apple.printData()
Apple.printData()
orange = Orange(160)
orange.printData()
orange.printTaste()
水果: 颜色=? 重量=0g
红苹果: 颜色=红 重量=280g
甜甜的好吃
青苹果: 颜色=绿 重量=250g
甜甜的好吃
橙子: 颜色=橙子 重量=160g
酸酸的好吃