新聞中心
Python 是一種完全面向?qū)ο蟮恼Z言。從這些教程開始,您就一直在使用類和對象。Python 程序中的每個元素都是一個類的對象。數(shù)字、字符串、列表、詞典等。,在程序中使用的是相應(yīng)內(nèi)置類的對象。您可以使用 type() 方法檢索變量或?qū)ο蟮念惷缦滤尽?/p>

我們提供的服務(wù)有:網(wǎng)站建設(shè)、成都網(wǎng)站制作、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、武鄉(xiāng)ssl等。為超過千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的武鄉(xiāng)網(wǎng)站制作公司
Example: Python Built-in Classes
>>> num=20
>>> type(num)
>>> s="Python"
>>> type(s)
定義類
Python 中的類可以使用class關(guān)鍵字來定義。
class :
.
.
按照上面的語法,一個類是用class關(guān)鍵字后跟類名和類名后面的:運(yùn)算符定義的,這允許您在下一個縮進(jìn)行繼續(xù)定義類成員。 以下是班級成員。
- 職業(yè)屬性
- 建造師T2】
- 實(shí)例屬性
- 屬性
- 課堂方法
也可以在沒有任何成員的情況下定義類。以下示例使用pass關(guān)鍵字定義了一個空類。
Example: Define Python Class
class Student:
pass 類實(shí)例化使用函數(shù)表示法。要創(chuàng)建一個類的對象,只需調(diào)用一個類,就像一個無參數(shù)的函數(shù),返回該類的一個新對象,如下所示。
Example: Creating an Object of a Class
std = Student() 上圖中,Student()返回Student類的一個對象,該對象被分配給一個本地變量 std。 Student類是一個空類,因?yàn)樗话魏纬蓡T。
類別屬性
類屬性是直接在類中定義的變量,由類的所有對象共享。可以使用類名和對象來訪問類屬性。
Example: Define Python Class
class Student:
schoolName = 'XYZ School' 上圖中schoolName是一個類內(nèi)部定義的類屬性。除非明確修改,否則所有對象的schoolName值將保持不變。
Example: Define Python Class
>>> Student.schoolName
'XYZ School'
>>> std = Student()
>>> std.schoolName
'XYZ School' 可以看到,一個類屬性被Student.schoolName和std.schoolName訪問。 使用類名更改類屬性的值會在所有實(shí)例中改變它。但是,使用實(shí)例更改類屬性值不會反映到其他實(shí)例或類。
Example: Define Python Class
>>> Student.schoolName = 'ABC School' # change attribute value using class name
>>> std = Student()
>>> std.schoolName
'ABC School' # value changed for all instances
>>> std.schoolName = 'My School' # changing instance's attribute
>>> std.schoolName
'My School'
>>> Student.schoolName # instance level change not reflectd to class attribute
'ABC School'
>>> std2 = Student()
>>> std2.schoolName
'ABC School' 下面的例子演示了類屬性count的使用。
Example: Student.py
class Student:
count = 0
def __init__(self):
Student.count += 1 在上例中,count是 Student 類中的一個屬性。 每當(dāng)創(chuàng)建新對象時,count的值增加 1。 創(chuàng)建對象后,您現(xiàn)在可以訪問count屬性,如下所示。
Example:
>>> std1=Student()
>>> Student.count
1
>>> std2 = Student()
>>> Student.count
2
構(gòu)造器
在 Python 中,每當(dāng)類的新對象被實(shí)例化時,都會自動調(diào)用構(gòu)造器方法,與 C# 或 Java 中的構(gòu)造器相同。構(gòu)造器必須有一個特殊的名稱__init__()和一個特殊的參數(shù)self。
*Note:*The first parameter of each method in a class must be the self , which refers to the calling object. However, you can give any name to the first parameter, not necessarily self. *下面的示例定義了一個構(gòu)造器。
Example: Constructor
class Student:
def __init__(self): # constructor method
print('Constructor invoked') 現(xiàn)在,無論何時創(chuàng)建Student類的對象,都會調(diào)用__init__()構(gòu)造器方法,如下所示。
Example: Constructor Call on Creating Object
>>>s1 = Student()
Constructor invoked
>>>s2 = Student()
Constructor invoked Python 中的構(gòu)造器用于定義實(shí)例的屬性并為其賦值。
實(shí)例屬性
實(shí)例屬性是附加到類實(shí)例的屬性。實(shí)例屬性在構(gòu)造器中定義。
以下示例在構(gòu)造器中定義實(shí)例屬性name和age。
Example: Instance Attributes
class Student:
schoolName = 'XYZ School' # class attribute
def __init__(self): # constructor
self.name = '' # instance attribute
self.age = 0 # instance attribute 實(shí)例屬性可以使用點(diǎn)符號[instance name].[attribute name]來訪問,如下所示。
Example:
>>> std = Student()
>>> std.name
''
>>> std.age
0 您可以使用點(diǎn)符號設(shè)置屬性值,如下所示。
Example:
>>> std = Student()
>>> std.name = "Bill" # assign value to instance attribute
>>> std.age=25 # assign value to instance attribute
>>> std.name # access instance attribute value
Bill
>>> std.age # access value to instance attribute
25 您可以通過構(gòu)造器指定實(shí)例屬性值。除self參數(shù)外,以下構(gòu)造器還包括名稱和年齡參數(shù)。
Example: Setting Attribute Values
class Student:
def __init__(self, name, age):
self.name = name
self.age = age 現(xiàn)在,您可以在創(chuàng)建實(shí)例時指定這些值,如下所示。
Example: Passing Instance Attribute Values in Constructor
>>> std = Student('Bill',25)
>>> std.name
'Bill'
>>> std.age
25 Note:*You don't have to specify the value of the self parameter. It will be assigned internally in Python. *您也可以為實(shí)例屬性設(shè)置默認(rèn)值。下面的代碼設(shè)置構(gòu)造器參數(shù)的默認(rèn)值。因此,如果在創(chuàng)建對象時沒有提供值,這些值將被分配給后者。
Example: Setting Default Values of Attributes
class Student:
def __init__(self, name="Guest", age=25)
self.name=name
self.age=age 現(xiàn)在,您可以創(chuàng)建一個具有默認(rèn)值的對象,如下所示。
Example: Instance Attribute Default Value
>>> std = Student()
>>> std.name
'Guest'
>>> std.age
25 更多信息請?jiān)L問 Python 中的類屬性與實(shí)例屬性。
類別屬性
在 Python 中,類中的一個屬性可以使用屬性()函數(shù)來定義。
Python 中的property()方法提供了實(shí)例屬性的接口。 它封裝實(shí)例屬性并提供屬性,與 Java 和 C# 相同。
property()方法以 get、set 和 delete 方法為參數(shù),返回一個property類的對象。
以下示例演示了如何使用property()函數(shù)在 Python 中創(chuàng)建屬性。
Example: property()
class Student:
def __init__(self):
self.__name=''
def setname(self, name):
print('setname() called')
self.__name=name
def getname(self):
print('getname() called')
return self.__name
name=property(getname, setname) 在上例中,property(getname, setname)返回屬性對象,并將其分配給name。 因此,name屬性隱藏了私有實(shí)例屬性 __name。 直接訪問name屬性,但是在內(nèi)部它將調(diào)用getname()或setname()方法,如下所示。
Example: property()
>>> std = Student()
>>> std.name="Steve"
setname() called
>>> std.name
getname() called
'Steve' 建議使用物業(yè)裝飾代替property()方法。
類方法
您可以使用def關(guān)鍵字在一個類中定義任意多的方法。 每個方法必須有第一個參數(shù),一般命名為self,指的是調(diào)用實(shí)例。
Example: Class Method
class Student:
def displayInfo(self): # class method
print('Student Information') Self只是類中方法的第一個參數(shù)的常規(guī)名稱。 對于類的對象x,定義為mymethod(self, a, b)的方法應(yīng)該稱為x.mymethod(a, b)。
上面的類方法可以作為普通函數(shù)調(diào)用,如下所示。
Example: Class Method
>>> std = Student()
>>> std.displayInfo()
'Student Information' 方法的第一個參數(shù)不需要命名為self。您可以給出任何引用調(diào)用方法實(shí)例的名稱。 下面的displayInfo()方法將第一個參數(shù)命名為obj而不是self,效果非常好。
Example: Class Method
class Student:
def displayInfo(obj): # class method
print('Student Information') 在類中定義沒有self參數(shù)的方法會在調(diào)用方法時引發(fā)異常。
Example: Class Method
class Student:
def displayInfo(): # method without self parameter
print('Student Information')
>>> std = Student()
>>> std.displayInfo() Traceback (most recent call last):
std.displayInfo()
TypeError: displayInfo() takes 0 positional arguments but 1 was given 該方法可以使用self參數(shù)訪問實(shí)例屬性。
Example: Class Method
class Student:
def __init__(self, name, age):
self.name = name
self.age = age
def displayInfo(self): # class method
print('Student Name: ', self.name,', Age: ', self.age) 您現(xiàn)在可以調(diào)用該方法,如下所示。
Example: Calling a Method
>>> std = Student('Steve', 25)
>>> std.displayInfo()
Student Name: Steve , Age: 25
刪除屬性、對象、類
您可以使用del關(guān)鍵字刪除屬性、對象或類本身,如下所示。
Example: Delete Attribute, Object, Class
>>> std = Student('Steve', 25)
>>> del std.name # deleting attribute
>>> std.name
Traceback (most recent call last):
File "", line 1, in
std.name
AttributeError: 'Student' object has no attribute 'name'
>>> del std # deleting object
>>> std.name
Traceback (most recent call last):
File "", line 1, in
std.name
NameError: name 'std' is not defined
>>> del Student # deleting class
>>> std = Student('Steve', 25)
Traceback (most recent call last):
File "", line 1, in
std = Student()
NameError: name 'Student' is not defined 本文名稱:Python類
網(wǎng)頁網(wǎng)址:http://fisionsoft.com.cn/article/djggpgp.html


咨詢
建站咨詢
