17. Object Oriented (a)

A. Object-oriented concepts

Object-oriented programming (Object-oriented Programming, referred OOP), is a method of packaging code

Why have Object Oriented: in order to better simulate real-world things, descriptions of features and the data and code blocks (function) packaged together

For example, the data is thrown mess list, which is a simple package, the data package level; the common package as a function block, which is a package, a package level statement.

class the Person:
 # For example, people have a number of attributes, name, gender, age, etc. 
    name = " Mewtwo " 
    Sex = " M " 
    Age = 21 # people have some behavioral action or method DEF RUN (Self):
         Print ( " people will run " ) # will run DEF shop (Self):
         Print ( " people shopping " ) # 'll Shopping DEF sLEEP (Self):
         Print ( " people will go to bed " ) # 'll sleep
 

    
    
    

Object-oriented, common terms include:

  • Class: You can understand a template, you can create an unlimited number of specific examples through it. For example, the preparation of the previous tortoise turtle represented only this species, through which you can create an unlimited number of instances to represent a variety of different characteristics of the turtle (this process is also known instance of the class).
  • Object: class and can not be directly used to create the instance of the class by (also known as objects) can be used. It's a bit like the relationship between the car and the car drawing, drawing itself (class) and not for people to use, create by drawing a car (objects) can be used.
  • Properties: All variables in the class called attributes. For example, tortoise this class, bodyColor, footNum, weight, hasShell this class are owned by the property.
  • Method: All functions commonly referred to as class methods. However, all of the different functions and is a class method comprising at least the self parameter (follow-up will be described in detail). For example, class Tortoise, crawl (), eat (), sleep (), protect () methods of this class are owned, class methods can not be used alone, and can only be used with object classes.

Define two. Class

Python  order classes are used: create (define) class, and then create an instance of an object class, a specific function by way of example the object.

How to create a class:

  class class name (first letter of each word capitalized, not separated symbol between words):
     zero or more class property ... (in no particular order of points, you can call each other)
     zero or more class method ... (in no particular order of points, you can call each other)

Python's class is defined by the head of the class (the class keyword refers to the class name and section) and unified indent class body structure, the main body of the class members is two properties and methods. If you do not define any attributes and methods for the class, then the class is equivalent to an empty class, empty class if no other executable statement, pass statement may be used as a placeholder. But the empty class does not make sense, it is more less

The definition of a class

class Person: 
    '' 'This is a learning Python defined Person class' '' 
    # The following defines a class attribute 
    Hair = 'Black' 
    # The following defines a say method 
    DEF say (Self, Content): 
        Print (Content) 
class statement can create only one class, but can not create object classes, so in order to use the already created class, you need to manually create a class of objects, create a class object is instantiated process, also known as the class.
  Syntax: class name (parameters)
Case:
  
Person class: 
    '' 'defined by a Person class' '' 
    # 2 defines class variable 
    name = "Mewtwo" 
    Age = "22 is" 
    DEF the __init __ (Self, name, Age): 
        # 2 instance variables defined 
        self.name = name 
        self.age = Age 
        Print ( "this man's name is:", name, "age:", Age) 
    # define an instance method say 
    DEF say (Self, Content): 
        Print (Content) 
# the person object assigned to the variable p 
p = Person ( "Mewtwo", 22) 
using the class object
# Angle class name 
#      # action class Static variable 
#          # 1, all of the contents of Person .__ dict__ query class (not additions and deletions operation) 
# Print (Person .__ dict__) 
# Print (the Person .__ dict __ [ 'Faith ']) 
# the Person .__ dict __ [' Mind '] =' no brain ' 
# Print (the Person .__ dict __ [' Mind ']) 
        # 2, universal. class in a single variable CRUD, with everything point 
# Print (Person.mind) 
# Print (Person.animal) # check 
# Person.money = 'use of money' by # 
# Person.mind = 'no brain' change # 
# del Person.mind 
# Print (the Person. __dict__) 
# 
#      method # operation class (basically do not work in the class name to operate) 
#Person.work # (111) 
# 
# angular object # 
# RET = the Person () # + class name () This procedure: Examples of process (a process to create an object), 
#            # the Person () to instantiate objects, examples of object 
# Print (RET) 
# #. 1, as long as the class name + () generates an object, the __init__ method for automatically performing class. 



class the Person:
     '' ' class body: two parts: a variable portion, the method ( function) part of the '' ' 
    Mind = ' thinking '   # variables, static variables, static fields 
    animal = ' higher animals ' 
    faith = ' faith ' 

    DEF  __init__ (Self, name, Age, Hobby): 
        self.name = name   #  Person.money = 'use of money' 
        self.age = Age 
        self.hobby = Hobby 

    DEF Work (Self):   # methods, functions, dynamic variables 

        Print ( ' % S will work ... ' % self.name)
     DEF Shop ( Self): 

        Print ( ' humans can consume .... ' ) 

# point of view the class name 
    # operation class static variable 
        # 1, all content Person .__ dict__ query class (not additions and deletions to the operation) 
# Print ( .__ dict__ magic the Person) 
# Print (the Person .__ dict __ [ 'Faith']) 
# the Person .__ dict __ [ 'Mind'] = 'no brain' 
#Print (the Person .__ dict __ [ 'Mind']) 
        # 2, universal. class in a single variable CRUD, with a universal point 
# Print (Person.mind) 
# Print (Person.animal) check # 
# Person.money = 'use of money' by # 
# Person.mind = 'no brain' change # 
# del Person.mind 
# Print (the Person .__ dict__) 

    # method of operating a class (the working class in the name of basic need to operate) 
# Person.work (111) 

# angle of the object 
# RET = the Person ( 'Alex', 1000, 'oldwomen') + # class name () this procedure: examples of process (a process to create an object), 
          # the Person () to instantiate an object instance, the object. 
# Print (RET) 


# . 1, class name + () generates an instance (object, object space.) 
# 2, the __init__ method for automatically performing class, object transfer space to the self argument of the __init__,
# 3, to the subject package corresponding properties. 
# Print (RET .__ dict__ magic) 

# angular object 
    # operation target Static variable 
        # . 1, the __dict__ all content query object 
        # 2, omnipotent omnipotent point. 
# print (ret.name) # check 
# ret.high by # 175 = 
# del ret.name puncturing # 
# ret.age = # 73 is changed 
# Print (RET .__ dict__ magic) 
    # object operation static class variable: only query . 
# Print (ret.mind) 

    # object calls methods in the class (the working class by the object execution method, rather than by class name) 
# ret.shop () 
# Print (RET)

The code is to look at the video when I see, the above is my own summary, but feeling a bit ignorant to learn object-oriented force, not yet fully understand

Guess you like

Origin www.cnblogs.com/hpcz190911/p/11613852.html