class Node:
data = None
next = None
def __init__(self, data):
self.data = data
def add(self, data):
if self.next is None:
self.next = Node(data)
else:
self.next.add(data)
from com.python.demo1.loki.entity.Node import Node
from com.python.demo1.loki.entity.Student import Student
class NodeUtil:
root = None
size = 0
@classmethod
def isEmpty(cls):
if cls.size == 0 or cls.root is None:
return True
else:
return False
@classmethod
def add(cls, data):
if cls.isEmpty():
cls.root = Node(data)
else:
cls.root.add(data)
cls.size += 1
@classmethod
def getSize(cls):
return cls.size
@classmethod
def getRoot(cls):
return cls.root
@classmethod
def toString(cls):
if cls.isEmpty():
return ""
else:
while cls.root is not None:
if isinstance(cls.root.data, Student):
print(cls.root.data.toString())
cls.root = cls.root.next
from com.python.demo1.loki.entity.Student import Student
from com.python.demo1.loki.entity.Node import Node
from com.python.demo1.loki.util.NodeUtil import NodeUtil
if __name__ == '__main__':
for i in range(0, 4):
stu = Student('loki' + str(i), 22 + i, 80, 174)
node = Node(stu)
NodeUtil.add(node)
print(NodeUtil.getSize())
NodeUtil.toString()
老师,请问下,当我的Node类中的data是Object类型,我设置了Student的对象进去,为什么在NodeUtil中的toString方法里,用isinstance(cls.root.data, Student)判断,居然是false?我是否还要做什么转换吗?