python Everything is the object, the so-called object: I am an object, I play the computer is the object, sitting chair is the object, the family raised puppy is also an object ...
We describe an object by describing its attributes (characteristics) and behavior. For example, the family dog, its color, size, age, weight, etc. are its attributes or characteristics. It will bark, wag its tail and so on is its behavior.
We include two aspects when describing a real object (object):
What it can do (behavior)
What it is (attributes or characteristics).
in the python , the characteristics of an object are also called attributes ( attribute ). The behavior it has is also known as the method
Conclusion: The object = Property + Method
in the python , the objects with the same properties and methods are grouped into a class ( class )
such as human beings, animals, plants, etc., these are the concepts of class.
A class is an object's template or blueprint, and a class is an abstraction of the object, which is an instantiation of the class. A class does not represent a specific thing, but an object represents something specific.
>>> class people:
... def speak (self):
... Print ("hello!")
...
'''
Defines a people class, defines a speak method, but does not define a property,
Because attributes do not belong to the class, they belong to instances of each class. That is, it belongs to the object.
So we can set different properties for each instance.
"'
>>> class people: #类
... def speak (self): #方法
... Print ("hello!")
...
>>>
>>> jack = people () #创建jack实例
>>> tom = people () #创建tom实例
>>> Import tab #导入table键功能模块
>>> Jack. #输入jack., you can see the following methods
jack.__class__ jack.__doc__ jack.__module__ jack.speak (
>>> Jack.speak () #引用speak方法
hello!
>>> jack.age=39 #添加age属性
>>> jack.height=120 #添加height属性
>>> Jack.
jack.__class__ jack.__module__ jack.height
jack.__doc__ jack.age jack.speak (
>> > Jack.height
>>> jack.age
#初始化对象
When you create a class, you can define a specific method named __init__ (), as long as you create an instance of the class
will run this method. You can pass parameters to the __init__ () method,
This allows you to set the property to the value you want when you create the object
__init__ () This method completes initialization when the object is created.
"'
>>> class Peo:
... def __init__ (self,name,age,sex):
... Self. Name = Name
... Self. Age = Age
... Self. Sex = Sex
... def speak (self):
... Print "My name" + self. Name
...
>>>
instantiating objects of this class:
>>> zhangsan=peo ("Zhangsan", ",", "Man")
>>> Print Zhangsan. Age
>>> print Zhangsan. Name
zhangsan
>>> print Zhangsan. Sex
Mans
#----------
>>> print Zhangsan
<__main__.peo instance at 0x7fe5041ec248>
For print to be printed, you have to use the __str__ method
__str__ () This method tells Python exactly what to display when printing an object (print)
"'
#!/usr/bin/python
class PEO:
def __init__ (self,name,age,sex):
self. Name = name
self. Age = Age
self. Sex = Sex
def speak (self):
print "My name" + self. Name
def __str__ (self):
msg= ' My name is: ' +self. Name+ "," + "My Age is:" + self. Age + ', ' + ' I sex is: ' +self. Sex
# msg= ' My name is: ' +self. Name+ "," + "My Age is:" + str (self. Age) + ', ' + ' I sex is: ' +self. Sex return
msg
shanghai=peo (' Shanghai ', ' the ', ' Man ')
# SHANGHAI=PEO (' Shanghai ', ', ') '
msg= ' My name is: ' +self. Name+ "," + "My Age is:" + self. Age + ', ' + ' I sex is: ' +self. Sex
Here 23 is the age, but is turned into a string because of self. The age defines a string
If you do not escape 23, you will get an error
If you want to escape in advance in your program, you need to use STR (self. Age)
'''
Print Shanghai
'''
The self is used as a parameter many times before
A class is like a blueprint, using a class to create multiple object instances.
When invoked, the speak () method must know which object called it.
Here the Self argument tells the method which object to invoke. This is called an instance reference.
Zhangsan. Speak () is like writing a peo.speak (Zhangsan)
'''
The above comprehensive understanding of Python classes, objects, methods, attributes are small to share all the content of the people, hope to give you a reference, but also hope that we support the cloud-dwelling community.