面试题,前辈们帮下忙

zhen_yan2009 2010-01-16 01:01:29
凭记忆写的,应该没错

public class TestOne{

private String name;
private TestOne test;

public void setName(String name)
{
this.name = name;
}

public String getName()
{
return name;
}

public void setTest(TestOne test)
{
this.test = test;
}

public TestOne getTest()
{
return test;
}
}

public class TestTwo extends TestOne{

private ArrayList list;

public void setList(TestOne test)
{
list.add(test);
}

public ArrayList getList()
{
return list;
}
}

问题:取所有子节点的name属性;
求达人解答
...全文
70 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
mucrea 2010-01-18
  • 打赏
  • 举报
回复
上面有些问题!做了如下修改
public String getName(TestTwo testTwo) {

String strName = "";
for(int i=0; i <testTwo.getList.size(); i++) {
TestOne testOne = testTwo.getList().get(i);
if(null != testOne.getName())
{
strName += testOne.getName();
}
while(testOne.getTest() != null) {

if(null != testOne.getTest().getName())
{
strName += testOne.getTest().getName();
}

testOne = testOne.getTest();
}
}
}
mucrea 2010-01-18
  • 打赏
  • 举报
回复
public String getName(TestTwo testTwo) {

String strName = "";
for(int i=0; i<testTwo.getList.size(); i++) {

if(null != testOne.getName())
{
strName += testOne.getName();
}
while(testOne.getTest() != null) {

strName += testOne.getTest().getName();
testOne = testOne.getTest().getTest();
}
}
}

51,396

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧