Python defines class learning 1

Source: Internet
Author: User

此时的d1就是类Dog的实例化对象

实例化,其实就是以Dog类为模版,在内存里开辟一块空间,存上数据,赋值成一个变量名
#Defining class TemplatesclassDog (object):#defining method functions for a class    #speech function Self must have    defSayhi (self):Print("helo")#instantiating an object through a classD1 = Dog ("Xiao Tao")Print(D1)#<__main__.dog object at 0x0000000001ecd208>#invoking a method in a class through an objectD1.sayhi ()#helo

#Defining class TemplatesclassDog (object):#defining the properties of a class    def __init__(self, name):#equivalent to creating a variable NAMESelf.name =name#defining method functions for a class    #speech function Self must have    defSayhi (self):Print("helo")#instantiating an object through a classD1 = Dog ("Xiao Tao") D2= Dog ("Xiao Feng")Print(D1)#<__main__.dog object at 0x0000000001ecd208>#invoking a method in a class through an objectD1.sayhi () d2.sayhi ( )#helo

The characteristic local variables of a function that is spelled out by multiple functions are also

With self it is possible to call the local variables between functions, various string formatting formats

#Defining class TemplatesclassDog (object):#defining the properties of a class                #D1    def __init__(self, name):#equivalent to creating a variable NAMESelf.name =name#d1.name =name    #defining method functions for a class    #speech function Self must have    defSayhi (self):Print("helo, i m is {name}". Format (name=self.name)) Print("helo, i m is {0}". Format (self.name))Print("helo, i m is%s"%self.name)Print("helo, i m is", Self.name)#instantiating an object through a class D1 D2D1 = Dog ("Xiao Tao")#Dog (D1, "Xiao Tao") Pass parametersD2 = Dog ("Xiao Feng")Print(D1)#<__main__.dog object at 0x0000000001ecd208>#invoking a method in a class through an objectD1.sayhi () d2.sayhi ( )#helo, i m is Xiao Tao#helo, i m is Xiao Feng

No instantiation of the print class proves that the class already exists in memory after the class is created

#Defining class TemplatesclassDog (object):#defining the properties of a class                #D1    def __init__(self, name):#equivalent to creating a variable NAMESelf.name =name#d1.name =name    #defining method functions for a class    #speech function Self must have    defSayhi (self):Print("helo, i m is {name}". Format (name=self.name)) Print("helo, i m is {0}". Format (self.name))Print("helo, i m is%s"%self.name)Print("helo, i m is", Self.name)#instantiating an object through a class D1 D2#D1 = Dog ("Little Tao") # Dog (D1, "Little Tao") Pass parameters#D2 = Dog ("Xiao Feng")#print (D1)### <__main__.dog Object at 0x0000000001ecd208>### Invoke the method in the class through the object#D1.sayhi ()##D2.sayhi ()#helo, i m is Xiao Tao#helo, i m is Xiao FengPrint(dog)#Execution Results<class '__main__.dog'>

The object generated after instantiation is called an instance.  D1, D2 is an example, self  is an instance

__init__ is the method by which constructor constructor method functions are classes.

#Defining class TemplatesclassDog (object):#defining the properties of a class                #D1    #Self is an instance .    def __init__(self, name):#Constructor constructor Method = = Initialization Method        #equivalent to creating a variable NAMESelf.name =name#d1.name =name    #defining method functions for a class    #speech function Self must have    defSayhi (self):#methods of the class        Print("helo, i m is {name}". Format (name=self.name)) Print("helo, i m is {0}". Format (self.name))Print("helo, i m is%s"%self.name)Print("helo, i m is", Self.name)#instantiating an object through a class D1 D2D1 = Dog ("Xiao Tao")#Dog (D1, "Xiao Tao") Pass parametersD2 = Dog ("Xiao Feng")#an instance of an object called an instance D2 is an instance.

#Defining class TemplatesclassDog (object):#defining the properties of a class                #D1    #Self is an instance .    def __init__(self, name):#Constructor constructor Method = = Initialization Method        #equivalent to creating a variable NAMESelf.name =name#d1.name =name    #defining method functions for a class    #speech function Self must have    defSayhi (self):#methods of the class        Print("helo, i m is {name}". Format (name=self.name)) defEat (Self,food):Print("helo, my name is%s, I like eat%s"%(Self.name,food))#instantiating an object through a class D1 D2D1 = Dog ("Xiao Tao")#Dog (D1, "Xiao Tao") Pass parametersD2 = Dog ("Xiao Feng")#an instance of an object called an instance D2 is an instance.D1.eat ("Apple")#helo, my name is Xiao Tao, I like eat apple

Python defines class learning 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.