1 # define a simple class 2 class Person (Object): . 3 # constructor argument passed receiving instantiation . 4 DEF the __init__ (Self, name): . 5 the self.name = name . 6 # class methods . 7 DEF the Hello (Self): . 8 Print ( " I'm DEFINE a Person class, My name iS% S " % the self.name)
. 9 # instantiated class and invoking the function 10 PERSON1 Person = ( " Test Person " ) . 11 #Examples of analytic class: 1, person1 = person ( " test person") is equivalent to first apply for a name for the memory person1, the instance name and argument to class person (person1, "test person" ), the name of the object is instantiated self, the parameters passed parameter is the constructor. 12 # 2, the constructor does person1.name = "test person" and self = person1 assignment, the contents written in the memory space PERSON1; in person1.Hello (self) rather person1.Hell0 (person1), wherein invoking self. the name is person1.name; is in step. 1 "Test Person" 13 is # example 3 process, the class will automatically call the constructor
14 person1.Hello () # object invokes its methods i'm define a person class, my name It is test person
_ Base class definitions and class instantiation parse
Guess you like
Origin www.cnblogs.com/flags-blog/p/12203798.html
Ranking