Python Learning note 12-python Object-oriented

Source: Internet
Author: User

Python Learning note 12-python Object-oriented

Python all objects


First, the basic concept

1. Object-oriented and process-oriented

Object-oriented Programming: C++,java,python

Process-oriented programming: Functional programming, C programs, etc.


2. Classes and objects

Class: Is the abstraction of things, such as: human, ball

Object: Is an instance of a class, such as: Football, basketball, object is the instantiation of the class

Properties: Facial, eye, nose, understood as a variable, static property

Methods: For people, the clothing and shelter to live, understood as a function, dynamic method


Example: Ball can abstract the characteristics and behavior of the ball, and then can instantiate a real ball entity.


3. Why should I use object-oriented?

The main ideas of object-oriented are:

Packaging

Inherited

Polymorphic

This idea facilitates the solution of more complex projects and is easier to maintain.


4. Python class definition

(1) class definition (encapsulation)

Class consists of the variables and functions that are required, which are called "encapsulation"

Class A (object):

A is a class name: A number, a letter and an underscore, which cannot be the beginning of a number, the first letter capitalized

variable, lowercase, two words, middle underline

function, if it is two words, first letter lowercase =, the second character is capitalized after the first letter


(2) The structure of the class:

Class name

Member Variables-Properties

member functions-Methods

The method of invoking an object is actually a function of invoking the object


(3) Creation of classes

Class MyClass (object):

def fun (self):

Print "I am function"

There is at least one parameter in the class's method self


(4) Creating an instance of a class

[[Email protected] class]# vim class1.py#!/usr/bin/pythonclass people (object):     color =  ' Yello '            # Defines a static property     def think (self):             #定义动态方法, there must be a self        self.color =  ' black '     #函数内也可以调用类的属性 can also be re-assigned, but need to use self        print  " i am a %s " % self.color        print " I  am a thinker "People = people ()   #类的实例化, is to assign a class to a variable, which is an object, through this object--people, Go to call his properties and methods print people.color  #color是属性 without parentheses () People.think ()      #think是方法, need braces () [[Email protected] class]# python class1.py yelloi am a blacki am  a thinker


(5) Creation of objects

The process of creating an object is called instantiation, and when an object is created, it contains three aspects of attributes, object handles, properties, and methods

Handles are used to distinguish between different objects people

The properties of an object color and method think correspond to member variables and long-term functions in a class

obj = MyClass ()//Creates an instance (object) of the class, invoking methods and properties through an object


Class, which is divided into public and private properties according to the scope of use.

Public properties, which can be called outside of classes and classes

Private properties, which can no longer be called outside the class, and other functions outside the class, a member variable that starts with a double underscore is a private property

can be accessed via Instance._classname_attribute mode. The front is a single underline, followed by a double underline.


Built-in properties: By default when the system defines the class, it is composed by the front and rear double underscore, __dict__, __module__.

[[Email protected] class]# vim class1.py#!/usr/bin/pythonclass people (object):     color =  ' Yello '     __age = 30        #添加了私有属性__age  = 30    def think (self):         self.color =  ' Black '          print  "i am a %s"  % self.color         print  "I am a thinker"         print self.__ age   #私有属性__age the  = 30 class can call People = people () Print people.colorpeople.think ( people.__age  #私有属性__age  = 30 class cannot be used outside the [[email protected] class]# python  class1.py yelloi am a blacki am a thinker30traceback  (most recent  call last):  File  "class1.py",  line 15, in <module>    people.__ ageattributeerror:  ' People '  object has no attribute  ' __age ' #报错, object people no attributes __age [[Email protected] class]# vim class1.py#!/usr/bin/pythonclass people (object):     color =  ' Yello '     __age = 30     def think (self):        self.color =  ' black '         print  "i am a %s"  % self.color         print  "I am a thinker"          print self.__agepeople = people () print  People.colorpeople.think () print people._people__age    #在类外调用类的私有属性, but this is not recommended, the class people is preceded by a single underline , followed by a double underline [[Email protected] class]#&nbsP;vim class1.py#!/usr/bin/pythonclass people (object):    color =  ' Yello '   #内置属性     __age = 30    def think (self):         self.color =  ' Black '          print  "i am a %s"  % self.color         print  "I am a thinker"          Print self.__agepeople = people () Print people.colorpeople.think () print people.__dict_ _   #通过对象调用内置属性, different classes call built-in properties, [[Email protected] class]# python class1.py yelloi  am a blacki am a thinker30{' color ':  ' black '}   #生成字典, built-in properties [[email  protected] class]# vim class1.py#!/usr/bin/python#coding:utf-8    #支持中文, or coding:utf8   or E.ncoding:utf-8  or  # -*- encoding:utf-8 -*-class people (object):     color =  ' Yello '     __age = 30    def  Think (self):        self.color =  ' black '          print  "i am a %s"  % self.color         print  "I am a thinker"          print self.__agepeople = people () people.color =  ' Caucasian ' print  People.colorpeople.think () print people.__dict__print  ' # '  *30print people.color[[email  protected] class]# python class1.py  White People    I am a  blacki am a thinker30{' color ':  ' black '}############################# #yello [[email  Protected] class]# vim clAss1.py#!/usr/bin/python#coding:utf-8class people (object):    color =  ' Yello '     __age = 30    def think (self):         self.color =  ' Black '          print  "i am a %s"  % self.color         print  "I am a thinker"         print  Self.__agepeople = people () people.color =  ' Caucasian ' Print people.colorpeople.think () print  people.__dict__print  ' # '  *30print people.colorprint people.__dict__  # Using a class to invoke built-in properties will print out the system's built-in properties, very much [[email protected] class]# python class1.py    Caucasian i am a blacki am a thinker30{' color ':  ' black '}########################## # # # #yello {' __module__ ':  '__main__ ',  ' color ':  ' yello ',  ' __doc__ ': none,  ' __dict__ ': <attribute  ' __ dict__ '  of  ' People '  objects>,  ' _people__age ': 30,  ' __weakref__ ': < attribute  ' __weakref__ '  of  ' people '  objects>,  ' think ': <function  Think at 0x7f7c3b5c6758>}



This article comes from "Plum blossom fragrance from bitter cold!" "Blog, be sure to keep this provenance http://daixuan.blog.51cto.com/5426657/1850762

Python Learning note 12-python Object-oriented

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.