Introduction to Object-oriented technology
Class: Used to describe a collection of objects that have the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.
Class variables: Class variables are common throughout the instantiated object. Class variables are defined in the class and outside the body of the function. Class variables are not typically used as instance variables.
Data members: Class variables or instance variables are used to manipulate the data related to the class and its instance objects.
Method overrides: If the method inherited from the parent class does not meet the requirements of the subclass, it can be overridden, which is called the override of the method, also known as the override of the method.
Instance variable: A variable defined in a method that acts only on the class of the current instance.
Inheritance: A derived class (derived class) that inherits the fields and methods of the base class. Inheritance also allows the object of a derived class to be treated as a base class object. For example, there is a design where an object of type dog is derived from the animal class, which is the analog "is a (is-a)" Relationship (example, dog is a animal).
Instantiation: Creates an instance of a class, the concrete object of the class.
Method: A function defined in a class.
Object: an instance of a data structure defined by a class. The object consists of two data members (class variables and instance variables) and methods.
1. Classes and instances
The creation of a class
Class names are followed by classes, that is, student, where the class name is usually the first word in uppercase, followed by (object),
That is the class from which the class inherits, the concept of inheritance we'll talk about later, usually,
If you do not have an appropriate inheritance class, use the object class, which is the class that all classes will eventually inherit from
Class Student (object):
Pass
Once you have defined the Student class, you can create an instance of student based on the student class, and create an instance by using the class name + ()
Bart = Student ()
>>> Bart
<__main__. Student Object at 0x10a67a590>
>>> Student
<class ' __main__. Student ' >
Because a class can act as a template, you can force a number of attributes that we think must be bound to be filled in when creating an instance.
By defining a special __init__ method, when creating an instance, bind the attributes such as Name,score.
Class Student (object):
def __init__ (self, Name, score):
Self.name = Name
Self.score = Score
Access restrictions
If you want the internal properties to be inaccessible externally, you can add two underscores to the name of the property, in Python,
The variable name of the instance, if it starts with __, becomes a private variable (private).
Only internal access, external access not available
Class Student (object):
def __init__ (self, Name, score):
Self.__name = Name
Self.__score = Score
def print_score (self):
Print ('%s:%s '% (Self.__name, Self.__score))
Benefits: This ensures that external code cannot arbitrarily modify the state inside the object, so that the code is more robust through access-restricted protection
But what if external code gets name and score? You can add get_name and Get_score methods to the student class.
Class Student (object):
...
def get_name (self):
Return Self.__name
def get_score (self):
Return Self.__score
What if I want to allow external code to modify score? Can add Set_score method to student class again
Class Student (object):
...
def set_score (self, score):
Self.__score = Score
__slots__ function
The Dict dictionary is free to manipulate the data, keeping the instance's variables in the dict of the instance,
Properties for a class cannot be saved, and the properties of the class include variables and functions.
__slots__ is a tuple that includes the properties that are currently accessible.
When slots is defined, the variable defined in slots becomes the descriptor of the class, which is equivalent to the member variable declaration in java,c++.
An instance of a class can only have variables defined in slots, and no new variables can be added. Note: After defining the slots, there is no more dict
Class Student (object):
__slots__ = (' x ')
x = 9
def __init__ (self):
Pass
b = Student ()
b.x = 1
Print b.x
Error:
Traceback (most recent):
File "f:/python/python_test.py", line <module>
b.x = 1
Attributeerror: ' Student ' object attribute ' x ' is read-only
This article is from the "Django Code Pit" blog, so be sure to keep this source http://13435380.blog.51cto.com/13425380/1979223
Python Object-oriented