程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

8、 Python learning notes - object oriented - member modifier

編輯:Python
""" Member modifier
Members of the public : Externally, you can call , Inherit , A subclass can call the public member of the parent class
Private member : Cannot call... Externally , Can only be called indirectly ( Through method calls inside the class ), Cannot inherit , A subclass cannot call a private member of a parent class . Use '__' Start with two underscores
"""
# Example 1, Public and private variables ,name It's a public member ,age It's a private member ,bar Calling private members internally __age
class Foo1:
def __init__(self, name, age):
self.name = name
self.__age = age
def bar(self):
return self.__age
# Instantiation
foo1 = Foo1('abc', 20)
# Public members can call directly from outside
print(foo1.name)
# Private members cannot be called directly from outside , The following command will report an error
# print(foo1.__age)
# Private variables are simply renamed , The following method can be used to call , Never call like this
print(foo1._Foo1__age)
# Private fields can be accessed indirectly
print(foo1.bar())
# Example 2、 Public and private of methods ,bar1 It's a public member ,__bar2 It's a private member ,bar3 Calling private members inside a class __bar2.
class Foo2:
def bar1(self):
return 'hello bar1'
def __bar2(self):
return 'hello bar2'
def bar3(self):
return self.__bar2()
# Instantiation
foo2 = Foo2()
# Public members can call directly from outside
print(foo2.bar1())
# Private members cannot be called directly from outside , The following command will report an error
# print(foo1.__bar2())
# Private members can indirectly call
print(foo2.bar3())
# The private method just renames the method , The following method can be used to call , Never call like that
print(foo2._Foo2__bar2())
# Example 3、 Inherit
class F:
def __init__(self):
self.age1 = 18
self.__age2 = 20
class S(F):
def __init__(self):
super(S, self).__init__() # Executes the parent class __init__ Method
def bar1(self):
return self.age1
def bar2(self):
return self.__age2
# Instantiation
obj = S()
# Public members of the parent class , Inherit
print(obj.bar1())
# Private member of parent class , Cannot inherit , Will report a mistake
# print(obj.bar2())
"""
Add
Private members are not overridden by subclasses
"""
# Example
class A(object):
def __method(self):
print("I'm a method in A")
def method(self):
self.__method()
a = A()
a.method()
class B(A):
def __method(self):
print("I'm a method in B")
b = B()
b.method()
"""
As we can see ,B.method() Cannot call B.__method Methods . actually , It is "__" Normal display of two underlined functions .
therefore , Before we create one with "__" When two underscores begin , This means that this method cannot be overridden , It is only allowed inside the class .
stay Python Zhongru ? It's simple , It just renames the method , as follows :
"""
a = A()
a._A__method()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved