package com.third;
public class LinkTest {
public static void main(String[] args) {
CreatNote cn=new CreatNote();
cn.setLen(10);
cn.CreatNotes();
cn.show();
}
}
class Note{
int data;
Note nextNote=null;
public Note(int data){
this.data=data;
}
public Note(){
}
}
class CreatNote{
private int len=0;
public Note firstNote=null;
public Note tempNote=null;
protected void setLen(int len){
this.len=len;
}
//此处若建立在构造方法中则会发生错误(firstNote为空)
public void CreatNotes(){
for (int i=1;i<=len;i++){
if(1==i){
Note note =new Note(i);
firstNote =note;
tempNote =note;
}
else
{
if (i!=len){
Note note=new Note(i);
tempNote.nextNote=note;
tempNote=note;
}
else{
Note note=new Note(i);
tempNote.nextNote=note;
tempNote=note;
tempNote.nextNote=firstNote;
}
}
}
}
protected void show(){
tempNote=firstNote;
do{
System.out.println(tempNote.data);
tempNote=tempNote.nextNote;
}while(tempNote!=firstNote);
}
}
构造方法不能乱用
最新推荐文章于 2022-11-15 14:27:46 发布