Python Classes and Objects _1

Source: Internet
Author: User
Tags class definition

A Programming paradigm
1. Object-oriented programming;
2. Process-oriented programming;
3. Functional programming
Two Object-oriented design
Object-oriented design: the data and actions of a particular kind of thing are all together;
1. We've all learned about functions, so how do we use functions to implement object-oriented design?
As shown in the following:

Code section:
#面向对象设计: The integration of data (features) and actions (methods) of the same type of specific things

def Door(size,color,type):    "门的函数,此处使用函数的嵌套来实现面向对象设计"    def open(door):        "门打开的动作"        print("这个%s门打开了"%door[‘type‘])    def off(door):        "门关闭的动作"        print("这个%s门关闭了" % door[‘type‘])    def init (size,color,type):        "初始化门的字典--含有门的特征(属性)及门的方法"        door={            "size":size,            "color":color,            "type":type,            "open":open,            "off":off        }        return  door    door = init(size,color,type)    return doordoor1 =Door(16,‘red‘,‘木门‘)  #调用门的函数print(door1 )ss=door1 [‘open‘]print(ss)door1[‘open‘](door1) #运行门的打开方法door2 =Door(34,‘white‘,‘铝合金‘)  #调用门的函数door2[‘off‘](door2) #运行门的打开方法

At this point we have implemented the object-oriented design method through the nested method of the function.
2. We started using classes to implement object-oriented design

The code blocks are as follows:

class Door():    "门的类"    def __init__(self,size,color,type):        "初始化门的数据"        self.size = size        self.color = color        self.type = type    def open(self):        "门打开的方法"        print("这个%s门打开了" %self.type)    def off(self):        "门关闭的方法"        print("这个%s门关闭了" %self.type)door1 = Door(16,‘red‘,‘木门‘) #门的实例化1door1.open()door2 = Door(34,‘white‘,‘铝合金‘)#门的实例化2door2.off()

3. Summary
From the implementation of the above two object-oriented design methods, we know:
① Object-oriented design does not require a programming language to implement the method, in fact, object-oriented design can be implemented by a purely structured language. However, if you want to construct data types with object properties and characteristics, you need to make more effort in the process.
② therefore, object-oriented is not only implemented by using the class definition, but also can be achieved using DEF-defined functions;
③ we can find that using class is more convenient for object-oriented design than using function, the code is more concise, the function is more professional;
Using object-oriented language to write programs, and a program is designed to use object-oriented, there is no relationship between the two (object-oriented language refers to class, but object-oriented design is not only using class can also use Def to achieve);
Three Object-Oriented Programming
Implement object-oriented design by defining class + instance/object. is to use object-oriented programming unique syntax class to implement object-oriented design. Such as:

The code blocks are as follows:

  class Door (): "Gate-type" Def __init__ (self,size,color,type): "Initialize gate data" self.size = size S Elf.color = Color Self.type = Type Def open (self): "Door open method" Print ("This%s gate opened"%self.type) def of F (self): "Door closed method" Print ("This%s door closed"%self.type) Door1 = Door (+, ' red ', ' wooden door ') #门的实例化1door1. Open () Door2 = Door (34 , ' white ', ' aluminum ') #门的实例化2door2. Off ()  

Note : In a class, the methods associated with the class are taken with the Self keyword. The int method in the class is executed first when the class is instantiated, and when we instantiate the class, the incoming argument does not take self, and it takes itself in the execution of the class. Why do I have to use the self parameter when defining a function in a class? The function method and the class method in object-oriented design (two subsections: Object-oriented design) can be found here: Self in this case is the pointer to the class itself, which is a reference to the class itself. Self.size = size meaning: The argument size is passed in and stored in self.size, and the variable in the class prefixed with self means that the variable can be used anywhere in the class (a global variable in the Class).
Four Defined
1. Class
A class is an abstract concept, a data structure that is like a model that is the same kind of transaction in life with the same attributes (features) and methods (actions) that can be used to produce real objects (instances);
2. Objects
Objects are specific things that have class properties and methods (that is, a specific thing created based on a class), and all objects in Python. For example, we see tables, doors, televisions and so on, as well as any data types in Python, database types have identity (ID), type, value and other attributes;
3. Class-To-object relationships:
Objects are generated by the class, for example: We in life through the model to produce different TV sets, all televisions have a brand, the color of the shell, the size of the display screen and other features, and all televisions have a power-on, shutdown, change the platform and other functions. At this point, we can think of the television as a whole, the name of the different brands of different sizes of television sets are objects.
4. Instantiation
The process of producing an object from a class is called instantiation
5. Instantiating an Object
A specific object produced by a class is called an instantiated object, that is, an object.
Five The format of the class
1. Format

"""class 类名(): #class关键字定义类,类的命名规范首字母大写,括号中可带参数可不带且只能带继承的基类名称    "类的说明文档"    代码块"""

As shown in the following:

2. Instantiation of a class
The instantiation process and function of a class are very similar:

Python Classes and Objects _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.