Object oriented

Source: Internet
Author: User

Object oriented

tags (space delimited): Object-oriented



Any thing classified as a kind, is a series of similar characteristics and skills of the combined body;

  • It is emphasized that the classification obtained from different angles is not the same;
Objects and classes:

In the real world: first there are objects, then there are summed up classes
In the program: First class, then call the class to produce the object;

  • The class is equivalent to the template, and then the object is created;
    For example:
    Wang er ya:
    Lie triple
    Similar characteristics:
    Study, Eat, sleep

So: Define the class first:

#先定义类class Student:    schoole='home'    #功能---用方法表示    def learn(self):        print('learn')    def sleep(self):        print('sleep')    def eat(self):        print('eat')#产生对象,最终程序的运行就是对象调用的;stu1=Student()#实例化对象stu2=Student()
How to use classes:

Classes and functions are distinguished, classes are executed when they are defined, but functions are executed only when they are called;

In memory, when the class is executed, there is a namespace to hold the namespace of the class;
To view the namespace of a class:


A class is a set of features and skills that are similar to a series of objects: A feature is used in a program: A variable representation, a skill is represented by a function: The inside of a class is nothing more than a definition of a variable and a definition of a function, but Python does not restrict, write other code in the class;

The use of the class: nothing more than a name, skill, for these things, the use of the class name space to do these operations: The following code:

class Student:    schoole='home'    #功能---用方法表示    def learn(self):        print('learn')    def sleep(self):        print('sleep')    def eat(self):        print('eat')print(Student.__dict__['school'])print(Student.__dict__['eat'])
How to use objects:

such as the code:
STU1, is an instantiated object, this object has a two functions, learning, eating, Zodiac is the same school;

  • But what about special objects? Is that some objects have personalized settings:
    __INIT__ is used to define objects unique to the object;
class luffStudent:    school='luffcity'    def __init__(self,name,age,sex):        self.Name=name        self.Age=age        self.Sex=sex    def learn(self):        print('is learning')    def eat(self):        print('is sleeping')stu1=LuffyStudent()

Follow the code above:
The result will be an error:

Users here will be very confused force: Obviously we just created a class, and did not call the __init__ method, the above error indicates that Python calls itself, that is, when you instantiate, it will call this method,

For the above code: we modified it to:
Stu1=luffystudent (' Wang ', ' NV ', ' 45 ')

Execute the above code at the time, will not error;

  • Now, with the __init__ method, at the time of instantiation:
    1. First create an object,
    2. Trigger __init__ will return the function properties of the class, there are several parameters to pass several parameters, in fact, Self is luffystudent () empty object, and self is the empty object, he will be the first argument to __init__,
    3. Then pass: ' Wang ', ' NV ', ' 45 '
  • Access properties:
    Print (STU1. Name)
  • We can also add and revise attributes to check:

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.

Tags Index: