如何在程序外部使用内部类

Xeon-Shao 2011-12-23 04:11:03
我写了这样一个程序
涂红部位是一个内部类Node
public class Chain {
public class Node {
private String data;
private Node next;
public Node(String str,Node n)
{
data=str;
next=n;
}

public Node (String str)
{
data=str;
}
}

private Node first=null;
public void addFirst(Node n){
n.next=first;
first=n;
}
public int length(){
int count =0;
Node temp=first;
if(temp==null)
return 0;
else{
while(temp.next!=null)
count++;
return count;
}
}
public void add(Node n){
Node temp=first;
if(first==null)
first=n;
else{
String a=temp.data;
String b=n.data;
while(a.compareTo(b)<0)
temp=temp.next;
temp.next=n;
}
}
public void addIndex(int a,Node n){
Node temp=first;
if(first==null){
first=n;
}
else{
int count=0;
while(count<a){
temp=temp.next;
}
temp.next=n;
}
}
public void deleteIndex(int a){
if(first==null)
System.out.println("链表内内容为空!");
else{
Node temp=first;
Node temp2=first.next.next;
int count=0;
while(count<a-1)
temp=temp.next;
temp.next=temp2;
}
}
public Chain combine(Chain c){
Node temp1=this.first;
Node temp2=c.first;
Chain chain=new Chain();
if(temp1==null)
return c;
if(temp1!=null&&temp2!=null){
int i;
int l1=this.length();
int l2=c.length();
if(l1<=l2)
i=l1;
else
i=l2;
for(int count=1;count<=i;count++){
if(temp1.data.compareTo(temp2.data)<0){
chain.add(temp1);
temp1=temp1.next;
}
else{
chain.add(temp2);
temp2=temp2.next;
}
}
if(l1<l2){
int diff=l2-l1;
for(int n=1;n<=diff;n++){
chain.add(temp2);
temp2=temp2.next;
}
}
else
{
int diff=l1-l2;
for(int n=1;n<=diff;n++){
chain.add(temp1);
temp1=temp1.next;
}
}
return chain;
}
else
return this;
}
}
下面是一个驱动类
public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Chain chain1=new Chain();
chain1.add(new Node("student"));
chain1.add(new Node("teacher"));
}

}
我在用new Node时报错,我应该怎么办呢????
...全文
141 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
amowker 2011-12-24
  • 打赏
  • 举报
回复
恩,内部类不能直接用的
[Quote=引用 1 楼 chenhaijing 的回复:]

Node node = new Chain().new Node("");
[/Quote]




--signature-------------------
http://www.mowker.com/qkdq.htm
Xeon-Shao 2011-12-24
  • 打赏
  • 举报
回复
额已经彻底乱了···························
西部流云 2011-12-24
  • 打赏
  • 举报
回复
可以在外部类的构造中直接new 内部类,如果直接在客户端使用内部类,就破坏了封装了。就如同上面
Node node = new Chain().new Node("");
这样肯定破坏了封装了。





Jobernowl 2011-12-24
  • 打赏
  • 举报
回复
楼上的方法虽然很好 但是通用性不高 如果内部类是private属性的估计那样就不行吧 所以用反射是比较通用的
Hi-Tomorrow 2011-12-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chenhaijing 的回复:]
Node node = new Chain().new Node("");
[/Quote]

正解 如果是静态的话可以 Chain.Node node = new Chain.Node();
龙腾冰 2011-12-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chenhaijing 的回复:]

Node node = new Chain().new Node("");
[/Quote]
就是这样的
ps:贴代码的时候注意格式
liu__shu 2011-12-23
  • 打赏
  • 举报
回复
chain1.add(chain1.new Node("student"));
chain1.add(chain1.new Node("teacher"));
Derek-Chen 2011-12-23
  • 打赏
  • 举报
回复
Node node = new Chain().new Node("");

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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