<1>The composition of the class
Class is composed of 3 parts
Class name: Class name
Class attributes: A set of data
Class method: allows operation method (behavior)
<2>definition class
class class name:
method list
class Student(object):
def run(self):
print('student running')
def sing(self):
print('student singing')
<3>create object
object name=class name()
stu =Student()
Attribute data owned by Student class
stu.name ='gxj'
stu.num ='123'
stu.run()
stu.sing()
print(stu.name)
print(stu.num)
< h4 id="initmethod"><4>init()method
class Student(object):
def __init__(self):
self .name ='gxj'
self.num ='123'
def run(self):
print('Students are running')
stu =Student()
print('Student's name:%s'%stu.name)
print('Student's seat number:%s'%stu.num)
The results are as follows:
Student's name: gxj
Student's seat number: 123
init_method to pass parameters
class Student(object):
def __init__(self,newName,newNum):
self.name =newName
self.num =newNum
def run(self):
print('Students are running')
stu =Student('gxj1','1234')
print( 'Student's name:%s'%stu.name)
print('Student's seat number:%s'%stu.num)
init_Method Summary
__init__() method is called by default when creating an object, no need to manually call
__init__(self), there is a parameter name by default For self, if two actual parameters are passed when creating an object, then in __init__(self), self is used as the first parameter and two parameters are required, for example, __init__(self,x,y)< br />The self parameter in __init__(self) does not need to be passed by the developer, the python interpreter will automatically pass the current object reference in
<5>define __str__() method
When using print to output the object, As long as you define the __str__(self) method yourself, the data returned from this method will be printed
class Student(object):
def __init__(self,newName,newAge):
self.name =newName
self.age =newAge
def __str__(self):
msg ='My name is:'+self.name+'Age:'+ self.age
return m sg
def run(self):
print('running')
stu =Student('gxj','17')
print(stu)
< pre>The result is as follows:
My name: gxjAge:17
<6>self’s usage
the so-called self, which can be understood as oneself
When an object calls its method, the python interpreter will pass this object to self as the first parameter, so the developer only needs to pass the following parameters
class Animal(object):
def __init__(self,name):
self.name =name
def printName(self):
print('Name is:%s' %self.name)
def myPrint(anamil):
anamil.printName()
dog1 =Animal('东东')
myPrint (dog1)
dog2 =Animal('西西')
myPrint(dog2)
<7>Protection object properties
< h5 id="Object name. Attribute name-data-----modify directly"><1>Object name. Attribute name= data—->modify directly
<2>Object name. Method name() —->Indirect modification
in order to save the attribute security better, that is, it cannot be modified at will, the general processing method is
<1>define the attribute as a private attribute
h5>
<2>Add a callable method for calling
class Student1(object): def __init__(self,name):
self.__name =name
def getName(self):
return self.__name
def setName(self,newName):
if len(newName)>=3:
self.__name =newName
else:
print('error: The length of the name needs to be greater than 3 or equal to 3')
xiaogao =Student1('xiaogao')
xiaogao.setName('xiaoniu')
print(xiaogao.getName())
xiaogao.setName('xiaosong')
print (xiaogao.getName())
Summary:
There are no keywords like public and private in C++ to distinguish between public
has attributes and private attributes
It is It is distinguished by the method of attribute naming. If two underscores'__' are added in front of the attribute name, then
indicates that the attribute is a private attribute, otherwise it is a public attribute (the method is the same, the method name is added with 2 underscores Means that the
method is private, otherwise it is public).