Object-oriented: Object-oriented basics of Python

Source: Internet
Author: User
Tags instance method

First, process-oriented, object-oriented contrast 1, process-oriented according to business logic from top to bottom code 2, the function of the idea of a function code encapsulated into the function, you will not need to repeat writing, only call the function 3, object-oriented function classification and encapsulation 1, 2, 31 step by step abstraction, function is the application scenario of moving toward an object-oriented intermediate step function--the application scenario where the functions are independent and without common data object-oriented programming: Each function is a common set of data 4, concurrency-based programming Erlang language proposed concurrency, single-process, single-threaded multi-process, multi-threaded programming 5 , generally in Python development, all using object-oriented or object-oriented and functional mixed use   second, object-oriented object-oriented programming is a way of programming, the landing of this method needs to use "class" and "object" to implement, so, object-oriented programming is actually the use of "class" and "object". 1, Class: Composed of properties and methods 2, Attribute 3, Method 4, instantiation: Create an instance of a class, the concrete object of the class. 5. Object: According to the instance created by the template, the instance object can execute the properties and methods in the Class 6, inherit: That is, a derived class (derived class) inherits the base class (base classes) of the field and Method 7, the method overrides: If the method inherited from the parent class does not meet the requirements of the subclass, It can be rewritten, a process called method overrides (override), also known as a method override.   Object objects are the data that describes their properties and the set of actions that are applied to the data to encapsulate together. 1, all things in the program are objects; 2. The object has state and behavior: the state is stored in a set of variables, and the behavior is implemented by means of the method. 3, the object of the method on the one hand, the object's internal variables are wrapped and protected, so that only the object's own method to operate these internal variables, on the other hand, the object's method is also an interface between the object and the external environment and other objects, communication interfaces, the external objects through these interfaces to drive the object to perform the specified behavior, provide   Class classes describe the data and behavior that the same class objects have. (1) The class is a template, the template can contain multiple functions, functions to implement some functions (2) object is created from the template instance, through the instance object can execute the function in the class  1, create Class (1) class Foo (object): class name first letter to uppercase, Inheriting the object class Empcount = 0empCount is a class property (also called a static property) whose value is shared among all instances of the class. You can use Employee.empcount to access def __init__ (self,name,age) in an inner class or an external class: constructor Self.name=nameThe constructor or initialization method of the Self.age=age class, as long as an instance of the class is created, this method is called. If you do not see the definition of this method, the default is to give an empty constructor method. def bar: Self: Represents the instance itself and all of the methods in the class must have self, and write at the first parameter position Obj=foo () instantiation, the function automatically assigns the instance value to the. Auto, does not need to manually assign value to create the object, The class name is followed by parentheses to Obj.bar () to execute Bar Method (2) def __inti__ (Self,*args): Using the meta-ancestor parameter, for variable indeterminate conditions if Len (args) ==2:self.name=nameself.age= Ageelse:print ' No salary '  2, creating an instance object to create an instance of a class, you can use the name of the class and accept the arguments by using the __init__ () method to create a specific object of a class. Every time an instance is generated from a class, Python automatically calls the method named __init__, the constructor of the class, for data initialization. The new instance will pass in the self parameter of the __init__ as usual. The method is also inherited in the class.   Five, class and object in memory call relationship 1, class and object storage in memory class and methods in the class in memory only one copy, and each object created by the class needs to save a copy of the class in memory: Store properties and Method objects: Store your own properties and class object pointers (when using methods in the object) To find a specific method in the memory of a class) when an object is created from a class, the object, in addition to encapsulating the value of name and age, holds a class object pointer that points to the class of the current object. When you execute a method in a class by using Obj1 (instance), the procedure is as follows: Find a method in the class based on the class object pointer in the current object to pass the object obj1 as a parameter to the first argument of the method self (the real purpose of the instance method plus self)  

Object-oriented: Object-oriented basics of Python

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.