0
HashTable 클래스를 래핑 할 수 있어야합니다. 래퍼에 대해 읽은 후에 나는 이것이 래퍼의 나쁜 사용이라고 확신하지만, 그것은 나의 임무에있다. , 경우내 HashTable 클래스 래핑
x = HashTableWrapper("linearProbe", 100)
모든하지만 괜찮 : 나는
from HashTable import HashTable
class HashTableWrapper(HashTable):
def __init__(self, probe, size):
super().__init__(probe, size)
def tableInsert(self, searchKey, newItem):
return self.HashTable.tableInsert(searchKey, newItem)
def tableRetrieve(self, searchKey):
return self.HashTable.tableRetrieve(searchKey)
def tableDelete(self, searchKey):
return self.HashTable.tableDelete(searchKey)
사용 : 나는 다음과 같은 코드를
(I 쉽게 프로그램을 교환 할 필요가 서로 다른 데이터 구조를 가지고) 다음을 사용합니다 :x.tableInsert(4, 6)
다음 오류가 나타납니다. AttributeError : 'HashTabl eWrapper '객체에'HashTable '속성이 없습니다. 파이썬이 모든 HashTable 부분을 강조 표시하므로 반환 부분에 문제가 있다고 생각합니다. 몇 가지 도움을 주시면 감사하겠습니다.
편집 :
class BSTTableWrapper:
def tableInsert(item):
return self.bst.searchtreeInsert(item)
예, 'HashTable' 속성이 없습니다. 래퍼를 만들기 위해 * 작성하지 않은 * 상속받은 *입니다. 예를 들어 'self.tableInsert (searchKey, newItem)'. 그러나 그때 질문이됩니다 : 래퍼의 요점은 무엇입니까? – jonrsharpe
아이템을 삽입하려고 할 때 : [이전 라인은 995 번 이상 반복되었습니다] RecursionError : 최대 재귀 깊이를 초과했습니다. 요점은 내 해시 테이블, binarysearchtree, 2-3-4tree 등 모든 유사한 함수는 래퍼 때문에 같은 이름을 가질 것이라고 생각합니다. 따라서 다른 구조체로 전환하려면 다른 래퍼를 가져올 수 있지만 모든 함수 이름은 그대로 유지됩니다. 아니면 내가 틀렸어? – goml
죄송합니다. 'self'로 호출하면 끝없는 재귀가 발생합니다. 'super(). tableInsert (...)'가 필요하다.'__init__'에서와 같이 메소드의 부모 클래스 버전을 호출 할 수있다. 랩퍼 인터페이스가 상위 인터페이스와 동일한 경우 실제로 이점이 없습니다. – jonrsharpe