Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Tự đại diện cho trường hợp của lớp. Bằng cách sử dụng bản thân của người Viking & NBSP;, chúng ta có thể truy cập các thuộc tính và phương thức của lớp trong Python. Nó liên kết các thuộc tính với các đối số đã cho.
Lý do bạn cần sử dụng bản thân. là bởi vì Python không sử dụng @ Cú pháp để chỉ các thuộc tính thể hiện. Python quyết định thực hiện các phương thức theo cách làm cho thể hiện phương thức được truyền tự động, nhưng không nhận được tự động: tham số đầu tiên của các phương thức là phiên bản phương thức được gọi.
Python3
Theo cách rõ ràng hơn, bạn có thể nói rằng bản thân có đặc điểm sau-
Bản thân luôn chỉ vào đối tượng hiện tại.
Model is audi a4 color is blue Model is ferrari 488 color is green0Model is audi a4 color is blue Model is ferrari 488 color is green1Model is audi a4 color is blue Model is ferrari 488 color is green2Model is audi a4 color is blue Model is ferrari 488 color is green3Model is audi a4 color is blue Model is ferrari 488 color is green4Model is audi a4 color is blue Model is ferrari 488 color is green5Model is audi a4 color is blue Model is ferrari 488 color is green2selfModel is audi a4 color is blue Model is ferrari 488 color is green8
class check:
Model is audi a4 color is blue Model is ferrari 488 color is green1Model is audi a4 color is blue Model is ferrari 488 color is green2we have used another parameter name in place of self4Model is audi a4 color is blue Model is ferrari 488 color is green4Model is audi a4 color is blue Model is ferrari 488 color is green5we have used another parameter name in place of self7
def __init__(self
Address of self = 140124194801032 Address of class object = 140124194801032Model is audi a4 color is blue Model is ferrari 488 color is green9we have used another parameter name in place of self0 we have used another parameter name in place of self1
Python3
Đầu ra
Một ví dụ khác về việc sử dụng bản thân:
class we have used another parameter name in place of self9
def __init__(selfclass4
Model is audi a4 color is blue Model is ferrari 488 color is green0selfclass7we have used another parameter name in place of self0 class9
Model is audi a4 color is blue Model is ferrari 488 color is green0Model is audi a4 color is blue Model is ferrari 488 color is green1Model is audi a4 color is blue Model is ferrari 488 color is green2 3 4self 6
Model is audi a4 color is blue Model is ferrari 488 color is green0Model is audi a4 color is blue Model is ferrari 488 color is green1Model is audi a4 color is blue Model is ferrari 488 color is green2def0 4selfdef3
Model is audi a4 color is blue Model is ferrari 488 color is green0selfcheck:2we have used another parameter name in place of self0 check:4
def check:7self__
__init__(8
__init__(9
Model is audi a4 color is blue Model is ferrari 488 color is green1Model is audi a4 color is blue Model is ferrari 488 color is green2self2self3
def4we have used another parameter name in place of self0 def6def7 4__6
def __init__(self
Model is audi a4 color is blue Model is ferrari 488 color is greenModel is audi a4 color is blue Model is ferrari 488 color is green9we have used another parameter name in place of self0 we have used another parameter name in place of self1
Đầu ra
Python3
Theo cách rõ ràng hơn, bạn có thể nói rằng bản thân có đặc điểm sau-
Bản thân luôn chỉ vào đối tượng hiện tại.
Model is audi a4 color is blue Model is ferrari 488 color is green0Model is audi a4 color is blue Model is ferrari 488 color is green1Model is audi a4 color is blue Model is ferrari 488 color is green2):6__init__(0
class check:
def __init__(self
Model is audi a4 color is blue Model is ferrari 488 color is green05
Model is audi a4 color is blue Model is ferrari 488 color is green9we have used another parameter name in place of self0 we have used another parameter name in place of self1
Đầu ra
Một ví dụ khác về việc sử dụng bản thân:
class we have used another parameter name in place of self9
def __init__(selfclass4
Python3
Model is audi a4 color is blue Model is ferrari 488 color is green0selfclass7we have used another parameter name in place of self0 class9
Model is audi a4 color is blue Model is ferrari 488 color is green0selfcheck:2we have used another parameter name in place of self0 check:4
Model is audi a4 color is blue Model is ferrari 488 color is green0Model is audi a4 color is blue Model is ferrari 488 color is green1Model is audi a4 color is blue Model is ferrari 488 color is green2Model is audi a4 color is blue Model is ferrari 488 color is green31
Model is audi a4 color is blue Model is ferrari 488 color is green0Model is audi a4 color is blue Model is ferrari 488 color is green33__init__(0
def check:7self__
def __init__(self
we have used another parameter name in place of self