python -- private property, inherited

Multiple inheritance # _*_ coding:utf-8 _*_ """ file: Multiple inheritance.py date: 2018-07_22 9:12 AM desc: """ # Mult...

Multiple inheritance

# _*_ coding:utf-8 _*_ """ file: Multiple inheritance.py date: 2018-07_22 9:12 AM desc: """ # Multiple inheritance and single inheritance class A(): def test(self): print 'This is A----test Method' def demo(self): print 'This is A------demo Method' class B(): def demo(self): print 'This is B----demo Method' def test(self): print 'This is B-----test Method' class C(A,B): # Multiple inheritance enables a subclass to have properties and methods of multiple parents at the same time # Yesterday's subclass had a parent called singleton inheritance # The properties and methods that multiple parents have at the same time. The subclass calls the previous one, which is called A. pass c = C() c.test() c.demo() # The new class inherits from object, class A(object): Classic class A

The private property or method of calling the parent class in the subclass.

Class properties and methods Private property of class __private_attrs: starts with two underscores, declaring that the property is private and cannot be used or directly accessed outside the class. Self. Private. Attrs when used in methods within a class. Class method Inside a class, you can define a method for the class by using the def keyword. Unlike the general function definition, the class method must contain the parameter self, which is the first parameter Private method of class __Private method: it starts with two underscores and declares that the method is private and cannot be called outside the class. Call self. Private methods inside the class

Python does not allow instantiated classes to access private data, but you can use object.classname \

# _*_ coding:utf-8 _*_ """ file: The private property or method of calling the parent class in a subclass..py date: 2018-07_22 10:06 AM desc: """ class A(object): def __init__(self): # Two properties are defined in the initialization method, one is public and the other is private self.num1=100 self.__num2=200 # Define private methods def __test(self): print 'Private method%d %d' %(self.num1,self.__num2) # Call private property def test(self): print '%d' %self.__num2 # Call private method self.__test() class B(A): def demo(self): # The private property of the parent class cannot be accessed in the method of the child class print 'Access the private properties of the parent class %d' % self.__num2 pass # Create a subclass object b = B() print b # Can't directly access the private properties of the object / call private methods outside # print b.__num2 # b.__test # b.demo() b.test()

Inherit private properties and methods

# _*_ coding:utf-8 _*_ """ file: Inherited private properties and private methods.py date: 2018-07_22 9:43 AM desc: """ """ //Private property and private method of parent class 1 Subclass objects cannot directly access the private properties and private methods of the parent class within their own methods 2 Subclass objects can indirectly access private properties or private methods through the common methods of the parent class //Private property, private method is the privacy of the object, which is not open to the public, and cannot be directly accessed by the outside world or subclass //Private properties, private methods often do some internal things """ class A(object): def __init__(self): # Two properties are defined in the initialization method, one is public and the other is private self.num1=100 self.__num2=200 # Define private methods def __test(self): print 'Private method%d %d' %(self.num1,self.__num2) class B(A): def demo(self): # The private property of the parent class cannot be accessed in the method of the child class print 'Access the private properties of the parent class %d' % self.__num2 self.__test() pass # Create a subclass object b = B() print b # Can't directly access the private properties of the object / call private methods outside # print b.__num2 # b.__test b.demo()

31 January 2020, 20:02 | Views: 5850

Add new comment

For adding a comment, please log in
or create account

0 comments