物件导向程序设计(Object Oriented Programming)简称OOP,主要针对大型软件设计而提出,它可以使软件设计更加灵活。
这是一个抽象的概念,世间万物皆为对象。比如一个人。
类是封装对象的属性及行为的载体,反过来说,具有相同属性和行为的类实体被称为类
封装
将对象的属性和行为封装起来,而将对象的属性和行为封装起来的截体就是类,类通常对客户隐藏实际细节。
继承
子能继承父的特性,但无法反过来
多态
一个父亲能有多个儿子,儿子能继承父亲的技能,但儿子们也有自身的技能
class Classname:
statement
class Geese:
pass
classname(parameterlist)
wildGeese = Geese()
print(wildGeese)
# <__main__.Geese object at 0x000002AD716B4DC0>
__init__是一个特殊的方法,每当创建一个新实例的时候,Pytho都会自动执行它。
__init__方法必须包含一个self参数,而且必须是第一个参数。
self参数是指象实例本身的引用,属於访问类的属性和方法。
在方法要调用时会传递实际self参数。
因此,当__init__方法只有一个参数时,就不需要只掉参数。
class Geese:
def __init__(self):
print("我是大雁类")
wildGeese = Geese()
# 我是大雁类
# 如果把self拿掉便会出现......
# TypeError: __init__() takes 0 positional arguments but 1 was given
# 所以说还是需要self的
class Geese:
def __init__(self, beak, wing, claw):
print("我是大雁类")
print(beak)
print(wing)
print(claw)
beak_1 = "嘴"
wing_1 = "翅膀"
claw_1 = "爪子"
wildGeese = Geese(beak_1, wing_1, claw_1)
# 我是大雁类
# 嘴
# 翅膀
# 爪子
def funcName(self, parametetlist):
block
instanceName.functionName(parametervalue)
class Geese:
def __init__(self, beak, wing, claw):
print("我是大雁类")
#print(beak)
#print(wing)
#print(claw)
def fly(self, state):
print(state)
beak_1 = "嘴"
wing_1 = "翅膀"
claw_1 = "爪子"
wildGeese = Geese(beak_1, wing_1, claw_1)
wildGeese.fly("我可以直冲云霄")
# 我是大雁类
# 我可以直冲云霄
class Geese:
neck = "脖子"
def __init__(self):
print(self.neck)
geese = Geese()
# 脖子
# 此写法等同於
self.neck = "脖子"
print(self.neck)
_foo : 以单下滑线开头表示protect(保护)类型的,只允许子类及本身,不能使from module import * 语句导入。
__foo():双下滑线到表私有,只允许定义该方法的类本身进行访问,可以透过"类的实例名.类名.__xxx"方式访问。
foo():代表特殊定义方法,一般表示系统定义名字,比如:init()
@property
def methodname(self):
block
class Reat:
def __init__(self, width, height):
self.width = width
self.height = height
@property
def area(self):
return self.width * self.height
print(Reat(100, 200).area)
# 2000
转化成属性後,就可以直接透过方法名来访问方法,就不需要添加小括号
# 想要建立可以读取但不能相改的属性可以用@property实践
class TVshow:
def __init__(self, show) :
self.__show = show
@property
def show(self):
return self.__show
tsShow = TVshow("正在播放<<8964>>")
print("默认: ", tsShow.show)
# 默认: 正在播放<<8964>>
# 那如果改成...
# tsShow = "正在播放<<爆打粉红>>" 修改属性值
# print("修改後: ", tsShow.show) 获取属性值
# AttributeError: 'str' object has no attribute 'show'
class className(baseclasslist):
Statement
class fruit:
color = "green"
def fruitcolor(self, color):
print("水果颜色是: ", color)
print("-----------")
print("原本的颜色是: ", fruit.color)
class Apple(fruit):
color = "red"
def __init__(self):
print("我是苹果")
Apple().fruitcolor(Apple.color)
# 我是苹果
# 水果颜色是: red
# -----------
# 原本的颜色是: green
在自身认为不适用的情况下,对def内部的函式内容作改写。
如果要在子类中引用父类的方法,但是又需要新增一些子类所特有的内容,可通过类名.方法()和super()来呼叫父类的方法,再个性化子类的对应函式。
直接使用类名.方法()来呼叫时,还是需要传入self为第一个引数,而使用super()呼叫则python自动将self传入,因此使用super()比较简洁。
class drive:
def __init__(self, name):
self.name = name
def getname(self):
return self.name
def displayVehicle(self):
print("厂牌: ", self.name)
class car(drive):
def __init__(self, name, mod):
super().__init__(name) # 呼叫父类建构函式
self.mod = mod
def display(self):
print(self.getname())
print(self.mod)
drive("BMV").displayVehicle()
car("totya", "GH333").display()
# 厂牌: BMV
# totya
# GH333
<<: Day.2 选择 - 关联式与非关联式 (SQL vs. NoSQL )
>>: [13th][Day2] 第一个 Container
半夜睡不着来更新XD Python属於「物件导向程序语言」(Object-oriented prog...
接下来我们来说明一下,在kibana要怎麽汇出和汇入查询资料?在实际的业务中,大家多少都有汇入、汇出...
Node.js前後端 前几天讲完了Docker的大致的使用说明,今明两天进入实作环节。我们来利用Do...
故事时间 在很久很久以前世界上诞生了C语言, 这是个非常灵活又高效的语言, 在不久之後他的後代C++...
【前言】 今天来介绍 we3.eth.contract 和 web3.utis,有一些概念还不太熟...