catalogue
2. The difference between inheriting the object class or not
4, Class method, static method and attribute method
6, Does python have private variables in a substantive sense?
1, Inherit
Different from java inheritance, python supports multiple inheritance. For example, the Person class inherits the Animal class and the specifications class at the same time. It can be written as follows:
class Animal(object): def __init__(self): pass class Species: def __init__(self): pass class Person(Animal, Species): country = "CN" def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex def printString(self): print("name:" + self.name + "\n" + "age:" + str(self.age) + "\n" + "sex:" + self.sex) person = Person("Jack", 12, "male") print(person.printString())
The in brackets is the parent class. The priority of multi class inheritance is as follows:
2. The difference between inheriting the object class or not
After inheriting object, some attributes will be added to the class, which will not affect the basic use.
3, Encapsulation
Unlike java, python does not have a private keyword. It is used by adding "_" before methods or variables
For example:
class Person(Animal, Species): __country = "CN" def __init__(self, name, age, sex): self.name = name self.age = age self.__sex = sex def __breath(self): print(self.name+"Breathing.......") def printString(self): self.__breath() print("name:" + self.name + "\n" + "age:" + str(self.age) + "\n" + "sex:" + self.__sex) person = Person("Jack", 12, "male") print(person.printString()) print(person._Person__sex) print(person._Person__country) person._Person__breath()
Set "_" Then it is expressed as a private variable or method.
But there are ways to crack private access in python:
Namely: object_ Class name__ Method name () or variable name
Confidentiality is as follows:
__foo__: Special methods are defined. Generally, system defined names are similar __init__() Or something. _foo: Those beginning with a single underscore indicate protected Type variables, i.e. protected types, can only be accessed by themselves and subclasses, and cannot be used for from module import * __foo: Double underlined indicates a private type(private)Variable of, You can only allow access to the class itself. foo:namely public method
4, Class method, static method and attribute method
Class methods cannot call instance variables or methods, only class methods or variables can be called. When using, @ classmethod needs to be added to the method, as follows:
class Person(object): _number = 0 def __init__(self, name, age, sex): self.name = name self.age = age self._number = 2 # The operation here is the variable of the instance object Person._number += 1 # Perform static operations on the variables here self.__sex = sex def __breath(self): print(self.name + "Breathing.......") @classmethod def lucky(cls): # cls represents the class itself. Unlike self or this, self and this represent instance objects person2 = Person("Alex", 13, 'male') person2.printString() print(cls._number) # Output class variable def printString(self): self.__breath() print("name:" + self.name + "\n" + "age:" + str(self.age) + "\n" + "sex:" + self.__sex) person = Person("Jack", 12, "male") person.printString() # Use instance object when calling Person.lucky() # Class used when calling
You can also use the annotation "@ staticmethod" class of static methods
Static method, cannot call any variable of class or instance
The attribute method uses the annotation "@ property", which is similar to calling the attribute variable.
class Person(object): def __init__(self, name, age, sex): self.name = name self.age = age self.__sex = sex @property def lucky(self): # cls represents the class itself. Unlike self or this, self and this represent instance objects print("233") @lucky.setter def lucky(self, num): # cls represents the class itself. Unlike self or this, self and this represent instance objects print("233") self.num = num person = Person("Jack", 12, "male") person.lucky # When called, it is called as if it were a property variable person.lucky = 1 # Parameter passing is similar to variable parameter passing print(person.num)
Attribute methods can call classes and attribute variables, and parameter passing is similar to variable parameter passing.
5, Polymorphism
class Animal(object): #Relative to abstract classes def show(self): print("abstract class must be rewrite!") raise Exception class Person(Animal): def __init__(self, name, age, sex): self.name = name self.age = age self.__sex = sex def show(self): print("hello") person = Person("Jack", 12, "male") print(person.show())
6, Does python have private variables in a substantive sense?
answer:
Nonexistent
There is no protection mechanism in python that prohibits access to a member of a class. Java is a very engineering language. Its philosophy is to serve engineering and reduce the chance of programmers writing wrong code as much as possible through various restrictions. On the contrary, Python's philosophy is to trust coders and give programmers the least restrictions, but programmers must be responsible for the code they write.
Does that mean Python doesn't think object-oriented programming needs encapsulation? The answer is No. Python completes encapsulation through coding specification rather than language mechanism. Specifically, python stipulates a convention on variable naming, which specifies what kind of variable name means that the variable is private and should not be accessed (rather than not accessible).