初始Python类

时间:2022-03-03 08:36:14

一、定义类、子类、类的实例化、子类的实例化、继承、实例属性和实例方法

示例:

class Fruit():
'''
定义一个水果类,包含水果的一些属性和一些方法。
'''
def __init__(self,name,color,shape,taste):
self.name = name
self.color = color
self.shape = shape
self.taste = taste
print(self.name+"的小时候是这样的,它的颜色是:"+self.color+",它的形状是:"+self.shape+",它的味道是:"+self.taste)
def colorChange(self,new_color):
self.color = new_color
print(self.name + "的颜色变成了:"+self.color)
def sizeChange(self,new_size):
self.shape = new_size
print(self.name + '的大小变成了:'+self.shape)
def tasteChange(self,new_taste):
self.taste = new_taste
print(self.name + '的味道变成了:'+self.taste)
def growUp(self):
print("慢慢的它长大了...") class waterFruit(Fruit):
'''
定义一个水分多的水果类,包含多水分的属性和一些方法。
'''
def __init__(self,name,color,shape,taste,water_pencent):
# Fruit.__init__(self,name,color,shape,taste)
self.name = name
self.color = color
self.shape = shape
self.taste = taste
self.water_pencent = water_pencent
print(self.name+"的小时候是这样的,它的颜色是:"+self.color+",它的形状是:"+self.shape+",它的味道是:"+self.taste+",它的水分是:"+self.water_pencent)
def waterChange(self,new_water):
self.water_pencent = new_water
print(self.name + "的水分变成了:" + self.water_pencent)
banana = Fruit('香蕉','绿色','长条形','微甜')
banana.growUp()
banana.colorChange('黄色')
banana.sizeChange('椭圆形')
banana.tasteChange('很甜')
watermelon = waterFruit('西瓜','绿色','圆形','甜的','90%')
watermelon.growUp()
watermelon.waterChange("95%")
watermelon.tasteChange("超级甜")

二、类属性

1、类的数据属性:它是静态的类属性,直接绑定在类上而不是某个实例上,在使用时通过类+"."操作符+属性来调用。如下例:

>>> class foo():
foo = 100
>>>
>>> print(foo.foo)
100
>>> foo.foo += 1
>>> print(foo.foo)
101
>>>

2、方法(也是类的属性):必须通过实例去调用,类不能直接调用。

>>> class foo():
foo = 100
def doNothing(self):
print('Do nothing!')
>>>
# 必须先实例化对象:
>>> fooAction = foo()
>>> fooAction.doNothing()
Do nothing!
# 直接用类调用方法时报错:
>>> foo.doNothing()
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
foo.doNothing()
TypeError: doNothing() missing 1 required positional argument: 'self'

3、查看类的属性:

# 1:通过内建函数dir()查看类的内部属性,返回的是一个属性列表
>>> dir(foo)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'doNothing', 'foo']
# 2:通过类的__dict__属性查看,返回的是一个字典,key是属性名,value是具体的值。
>>> foo.__dict__
mappingproxy({'foo': 100, 'doNothing': <function foo.doNothing at 0x0000029E039C7F28>, '__weakref__': <attribute '__weakref__' of 'foo' objects>, '__dict__': <attribute '__dict__' of 'foo' objects>, '__doc__': None, '__module__': '__main__'})
>>> print(foo.__dict__)
{'foo': 100, 'doNothing': <function foo.doNothing at 0x0000029E039C7F28>, '__weakref__': <attribute '__weakref__' of 'foo' objects>, '__dict__': <attribute '__dict__' of 'foo' objects>, '__doc__': None, '__module__': '__main__'}

4、一些类的特殊属性

# 类的名字:
>>> print(foo.__name__)
foo
# 类说明
>>> foo.__doc__
>>> print(foo.__doc__)
None
>>>
>>> class fooo(foo):
pass
# 类的所有父类构成的元组
>>> print(foo.__bases__)
(<class 'object'>,)
>>> print(fooo.__bases__)
(<class '__main__.foo'>,)
>>>
# 类属性的字典查看方法
>>> print(foo.__dict__)
{'foo': 100, 'doNothing': <function foo.doNothing at 0x0000029E039C7F28>, '__weakref__': <attribute '__weakref__' of 'foo' objects>, '__dict__': <attribute '__dict__' of 'foo' objects>, '__doc__': None, '__module__': '__main__'}
# 定义类foo所在的模块:
>>> print(foo.__module__)
__main__
>>>
# 实例foo1所对应的类:
>>> print(foo1.__class__)
<class '__main__.foo'>
>>>