我想知道在使用子类时是否可以阻止用户调用父类的方法。同时,我希望这些方法可以用于子类的方法本身。
例如,假设我有一个链表的实现。然后我通过继承(不确定它是不是很好的设计......)来建立堆栈ADT实现。因此,我希望从Stack类的用户“隐藏”LinkedList类的方法。
链表:
class LinkedList(object):
class Node(object):
"""
Inner class of LinkedList. Contains a blueprint for a node of the LinkedList
"""
def __init__(self, v, n=None):
"""
Initializes a List node with payload v and link n
"""
self.value=v
self.next=n
def __init__(self):
"""
Initializes a LinkedList and sets list head to None
"""
self.head=None
def insert(self, v):
"""
Adds an item with payload v to beginning of the list
in O(1) time
"""
Node = self.Node(v, self.head)
self.head = Node
print("Added item: ", Node.value, "self.head: ", self.head.value)
def size(self):
"""
Returns the current size of the list. O(n), linear time
"""
current = self.head
count = 0
while current:
count += 1
current = current.next
return count
def search(self, v):
"""
Searches the list for a node with payload v. Returns the node object or None if not found. Time complexity is O(n) in worst case.
"""
current = self.head
found = False
while current and not found:
if current.value == v:
found = True
else:
current = current.next
if not current:
return None
return current
def delete(self, v):
"""
Searches the list for a node with payload v. Returns the node object or None if not found. Time complexity is O(n) in worst case.
"""
current = self.head
previous = None
found = False
while current and not found:
if current.value == v:
found = True
else:
previous = current
current = current.next
# nothing found, return None
if not current:
return None
# the case where first item is being deleted
if not previous:
self.head = current.next
# item from inside of the list is being deleted
else:
previous.next = current.next
return current
def __str__(self):
"""
Prints the current list in the form of a Python list
"""
current = self.head
toPrint = []
while current != None:
toPrint.append(current.value)
current = current.next
return str(toPrint)
堆栈:
from PythonADT.lists import LinkedList
class Stack(LinkedList):
def __init__(self):
LinkedList.__init__(self)
答案 0 :(得分:-1)
以下是在类中声明protected和private方法的官方语法。
引发的异常是:
# AttributeError: '<ChildClass>' object has no attribute '__<private_function>'
第1步 - 在class Parent
中声明私有和受保护的方法。
class Parent(object):
# a private method starts by 2 '_'
def __parent_private(self):
print('inside __parent_private()')
# a protected method starts by 1 '_'
def _parent_protected(self):
print('inside _parent_protected()')
第2步 - 声明函数从class Child
调用这两个函数。
class Child(Parent):
def call__parent_private(self):
self.__parent_private()
def call_parent_protected(self):
self._parent_protected()
第3步 - 创建class Child
的实例以检查私人和受保护的访问。
myChild = Child()
从Child类检查受保护的方法==&gt;允许访问
# internal access of protected method
myChild.call_parent_protected()
输出:“inside _parent_protected()
”
# direct access of protected method
myChild._parent_protected()
输出:“inside _parent_protected()
”
检查来自Child类的私有方法==&gt;访问被拒绝
# internal access of private method
myChild.call__parent_private()
错误:“AttributeError: 'Child' object has no attribute '_Child__parent_private'
”
# direct access of private method
myChild.__parent_private()
错误:“AttributeError: 'Child' object has no attribute '__parent_private'
”