1 Object-oriented knowledge
- 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.
2
Many classes tend to create objects with an initial state. The class may therefore define a special method called __init__ (), which defines the __init__ () method, and the instantiation operation of the class automatically calls the __init__ () method. Of course, the __init__ () method can have parameters, and arguments are passed through __init__ () to the instantiation of the class. For example:
classStudent (object):#Customize a student class, including the number, name, score, gender and other information def __init__(Self,number, name, score, sex):#initialize student, parameter Number,name,score,sexSelf.number =Number Self.name=name Self.score=score Self.sex=SEXSTU1=student (3054,"Shen", 90,"male")Print(Stu1.name,stu1.score)# output Result: Shen
Self represents an instance of a class, not a class:
There is only one special difference between a method of a class and a normal function--they must have an extra first parameter name , according to the convention its name is self.
Method of Class 3
Inside the class, you define a method using the DEF keyword, which differs from the general function definition in that the class method must contain the parameter self, which is the first argument, and self represents an instance of the class.
#class definitionclasspeople:#Defining basic PropertiesName ="' Age=0#define private properties, private properties cannot be accessed directly outside the class __weight=0#Defining construction Methods def __init__(self,n,a,w): Self.name=N self.age=a self.__weight=WdefSpeak (self):Print("%s said: I am%d years old. "%(self.name,self.age))#Instantiating Classesp = People ('Runoob', 10,30) P.speak ()
Properties and methods for Class 4
1) Private properties of the class
__private_attrs: Two underscores begin with, declaring that the property is private and cannot be used outside of the class or accessed directly.
self.__private_attrswhen used in methods inside a class.
2) methods of the class
Inside the class, use the def keyword to define a method that, unlike a generic function definition, must contain the parameter self , which is the first argument, and self represents an instance of the class.
Self's name is not a rule of death, you can also use this, but it is best to use self as agreed .
3) Private methods of the class
__private_method: Two underscores, declares that the method is a private method, and cannot be called outside of a class.
Called Inside the class: self.__private_methods.
4) class-specific methods
- __init__: constructor, called when generating an object
- __del__: destructors, using when releasing objects
- __repr__: printing, converting
- __setitem__: assigning values by index
- __getitem__: getting values by index
- __len__: Get length
- __cmp__: comparison operation
- __call__: function call
- __add__: add operation
- __sub__: minus operation
- __mul__: multiply operation
- __div__: except operations
- __mod__: finding the remainder operation
- __pow__: exponentiation
5 Python classes can also inherit, multi-inherit, and override methods ...
Python Object-oriented