9. Object-oriented: class and object, instance variable, class variable, object-oriented instance

Source: Internet
Author: User

9. Object-oriented: class and object, instance variable, class variable, object-oriented instance

 

Class and object:
  • Class Definition: describes a set of objects with the same attributes and methods. It defines the attributes and Methods shared by each object in the set. The object is a class instance.
  • Class advantages: it integrates the functions of an object to facilitate operations and reduce code duplication.
  • Instantiation: creates an instance of a class and a specific object of the class.
  • Object: a data structure instance defined by a class. The object includes two data members (class variables and instance variables) and methods.

 

Use the class statement to create a new class:

Class Dog: # self indicates the instance of the class, representing the address of the current object def _ init _ (self, name): # It is called a class constructor, initialize the member variable self. name = name def bulk (self): ### custom function print ("% s: Wang! "% Self. name) d1 = Dog ("Obama") # create an object and input the required Variable _ init _. By default, self will automatically input d1.bulk () # Call the method print (d1)
Why is there self:

The class variables and instance variables are used as explanations. common attributes are generally defined in the common part and do not require _ init __. for example, because everyone has their own names, the name should be private. In order to save resources, class functions are not copied to every object. Every object must call methods from the class definition area. For methods that involve object-specific attributes, when calling a method, you must pass in your own object self to make the function obtain private data, while calling this private data uses "self. variable name ".

 

Appendix:

What's New: https://www.cnblogs.com/wenbronk/p/7141224.html

 

Instance variables and class variables:
  • Instance variables are unique data for each instance, and class variables are the data shared by all instances of this type.
  • The sequence of variables is: instance variables -- class variables. If the instance variables are not found in the class variables
Class Dog: age = 8 def _ init _ (self, name): self. name = name def bulk (self): print ("Wang") d1 = Dog ("steamed stuffed bun") d2 = Dog ("steamed bread") d1.age = 10 print ("Dog: ", Dog. age, "\ tD1:", d1.age, "\ tD2:", d2.age) ----------------------- result: Dog: 8 D1: 10 D2: 8
  • Saving Theory: For simplicity, python does not copy a copy of class data to each object. Each object has its own unique attributes. When you need to use attributes or methods, check whether you have them, if it is not found in the class.

 

 

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.