程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Classes and objects (Python)

編輯:Python

Properties and methods

Attributes are used to describe some characteristics of a class , Such as color , brand , Models belong to mobile phone properties .

Methods are used to express some functions of this class , Such as taking photos , Calling is the way of mobile phone .

class

Format

Use keywords class Create a class , Here is a case :

class Phone():
def call(self,who):
return f" Calling {who}"
def sendmessage(self,who,txt):
return f" Sending text message to {who}, The content is {txt}" 

Be careful

  1. Phone Refers to the name of the class , In order to distinguish between classification and function , The initials of class names are recommended to be capitalized .
  2. The colon after the brackets indicates that the next indented code belongs to this class .
  3. def There are four spaces behind Represents an indentation , Indicates that the code block belongs to this class .
  4. Two special functions in class , Called method . Methods are also called member functions , Used to represent some functions of this class . The first parameter of the member function is self

Special parameters self

self Parameters , Parameters automatically passed in by the program , Refers to the object that calls the method . The first parameter is zero self, Is a fixed way to define instance methods .

object

Concept

Objects are created with classes as templates .

example

Here we use Phone Class to instantiate two objects to represent your mobile phone and my mobile phone .

class Phone():
def call(self,who):
return f" Calling {who}"
def sendmessage(self,who,txt):
return f" Sending text message to {who}, The content is {txt}"
myphone=Phone() //myphone Variable name , The name set for the object
yourphone=Phone() // Parentheses mean calling you Phone Instantiate an object
ret=myphone.call("Tony") //Tony It belongs to the argument
print(ret)
ret2=yourphone.sendmessage("Jenney"," Noon to eat anything ?")
print(ret2) 

Be careful

  1. Inside “.” The method used to connect the object name with the object , It is called period notation
  2. When calling object methods , It's not necessary for self Parameter passing parameter
  3. self Automatically transmitted by the program , There is no need to pass arguments manually

Class properties and initialization

Initialization method __init__

init There are two underscores on the left and right , That is, the whole name has four underscores .

Initialization here is similar to factory settings , Express “ Be prepared at the beginning .” It will be called automatically when the object is created .

example

class Phone():
def __init__(self,bd,clr):
print(" When creating an instance object , Call this method automatically ")
self.brand=bd // Because there is no object created here , All object names use self Parameters instead of
self.color=clr
myphone=Phone(" Huawei "," white ")
yourphone=Phone(" Apple "," black ")
print(f" I have one {myphone.color} Of {myphone.brand} mobile phone ")
print(f" You have one {yourphone.color} Of {yourphone.brand} mobile phone ")

Be careful :

  1. self Parameters are parameters automatically passed in by the program when calling the method , Refers to the instantiated object
  2. Different from the variables created before ,brand And color Is a variable specific to this type of object , Can only be used by objects of this class .
  3. bd and clr Are two formal parameter names of initialization parameters , Accept the passed data and assign it to the two properties of the object brand And color

Through the previous examples , We can know , The attribute and method of an object need to connect the object name and method name with a period representation . But when defining classes , Because we don't know which objects to define , therefore self The function of is to instantiate the object name ( quote ) Pass it on to the method . For example myphone In the object ,self.brand It stands for myphone.brand

In fact, the string we usually create 、 list 、 Tuples are essentially objects of this type . So it's called directly print()、range() function , Expressed by a period append()、keys() Wait for a method of an object .

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved