如何解决如何按照OOP原则使这些功能起作用?
我在一个类中有几种联系在一起的函数/方法。我正在建立一个模仿终端命令和链接的类。但是,有人告诉我这不是适当的OOP。如何将这些方法分开以独立工作。方法不应调用其他方法。正确吗?
class directory:
#FILES,LINKS AND DIRECTORIES
current_path = []
hold_files = [
'test1.txt','test2.py',{'/desktop': ['computer.txt','tfile.doc',{'/peace':{
'/pictures': [
'hello.gif','smile.gif','run.gif'
]},'/work':[
'file1.txt','file2.txt'
]
}]
}
]
#recursively delete folder (if dot in)
def delete(itself):
#if dictionary,call self,else delete
del itself
return
## HELPER METHODS
# Join list together to produce new link,basically return the added folder to the link
def concatenate(self):
new_link ="".join(current_path)
return new_link
#strip slashes and place in list
def adjust_link(self,paths):
new_string = ""
# shorten link,if someone uses cd ..,basically go back to previous folder
if paths == "cd ..":
current_path.pop()
#extend link,if someone is cding into another folder,remove /'s and append to separate list
elif "cd " in paths:
paths = paths[3:]
for slash in paths:
if slash == "/":
current.append(new_string)
new_string = ""
else:
new_string+=slash
# This shouldn't be here as OOP must be separated but this calls the other function to concatenate a new link
stripped = concatenate()
return stripped
#returns link
def link(self,paths):
address_location = adjust_link(paths)
return address_location
directory.link("cd desktop/peace")
directory.link("cd pictures")
directory.link("cd ..")
directory.delete()
谢谢。
*此外,这不是重构问题。我已经问过堆栈交换代码审查,他们告诉我来这里。代码不起作用。
编辑2:为什么“ directory.link()”不起作用?
解决方法
“我这里有一个程序,我想将其转换为OOP”通常不是这样完成的:“我有一个我想使用OOP解决的问题”通常是这种方法。看起来您正在创建将遍历内部目录结构的内容。因此骨骼可能看起来像:
class DirectoryTraverser:
def __init__(self,directory_tree):
self.hold_files = directory_tree
self.current_path = []
def... # all your other functions
# then to use it might look like:
# create a directory traversal object with directory tree
dt = DirectoryTraverser(hold_files)
dt.link("cd desktop/peace")
dt.link("cd pictures")
dt.link("cd ..")
dt.delete()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。