Here is a small series to bring you a comprehensive understanding of the Python class, objects, methods, properties. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
">
Everything in Python is an object, so-called object: I am an object, I play the computer is the object, sitting chair is the object, the home of the puppy is an object ...
We describe an object by describing attributes (characteristics) and behavior. For example, the puppy in the home, its color, size, age, weight, etc. is its properties or characteristics. It will bark, will 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 Python, the characteristics of an object are also known as attributes (attribute). The behavior it has is also called the method.
Conclusion: Object = attribute + method
In Python, classify objects with the same properties and methods as one class
such as humans, animals, plants, etc., these are the concepts of the class.
A class is a template or blueprint for an object, a class is an abstraction of an object, and an object is an instantiation of a 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 that defines a speak method, but does not define a property,
Because properties do not belong to classes, they belong to instances of each class. That is, 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
120
>>> Jack.age
39
'''
#初始化对象
When you create a class, you can define a specific method, called Init (), as long as you create an instance of the class
This method will be run. Parameters can be passed 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 the 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
...
>>>
When instantiating an object of this class:
>>> Zhangsan=peo ("Zhangsan", +, ' man ')
>>> print Zhangsan. Age
24
>>> print Zhangsan. Name
Zhangsan
>>> print Zhangsan. Sex
Mans
# ----------
>>> Print Zhangsan
<main.peo instance at 0x7fe5041ec248>
'''
The Str method is needed for print to be printed.
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 was:" + self. Age + ', ' + ' My sex is: ' +self. Sex
# msg= ' My name is: ' +self. Name+ "," + "My Age was:" + str (self. Age) + ', ' + ' My sex is: ' +self. Sex
Return msg
SHANGHAI=PEO (' Shanghai ', ' All ', ' man ')
# SHANGHAI=PEO (' Shanghai ', +, ' man ')
'''
Msg= ' My name is: ' +self. Name+ "," + "My Age was:" + self. Age + ', ' + ' My sex is: ' +self. Sex
Here 23 is age, but is turned into a string because self. Age defines a string
If you do not escape 23, you will get an error
If you want to escape in the program beforehand, you need to use STR (self. Age)
'''
Print Shanghai
'''
The formal parameter of self is used many times before
Class is like a blueprint, using a class can create multiple object instances,
When called, the Speak () method must know which object called it.
The self parameter here tells the method which object to invoke. This is called an instance reference.
Zhangsan. Speak () is like writing a peo.speak (Zhangsan)
'''