在引用字段之前发出访问字段的问题

时间:2019-06-27 22:09:40

标签: java class reference

我正在为类编写一些代码,并且在定义字段之前遇到一些与访问字段有关的问题。

我有一个叫做Webpage的类,它由两个字符串和一个表示项目列表(可以在网页上的项目)组成的接口组成。 Items接口由一个代表项目列表的类和一个代表列表中的第一个项目以及列表的其余部分的类组成。一个项目包括三个类,一个文本类,一个图像类,以及一个引用另一个网页的链接。

这是我的代码:

// represents a web page 
class WebPage {
    String title; 
    String url; 
    ILoItems items; 

    WebPage(String title, String url, ILoItems items) {
        this.title = title; 
        this.url = url; 
        this.items = items; 
    }
}

//represents a list of items 
interface ILoItems {
} 

//represents an empty list of Items 
class MtLoItems implements ILoItems {
    MtLoItems() {}
}

//represents a construct of a list of items
class ConsLoItems implements ILoItems {
    IItem first; 
    ILoItems rest; 

    ConsLoItems(IItem first, ILoItems rest) {
        this.first = first; 
        this.rest = rest; 
    }
}

//represents an item that could go on a web page: one of Text, Image, 
//or a Link to another page  
interface IItem {
}

class Text implements IItem {
    String contents; 

    Text(String contents) {
        this.contents = contents; 
    }
} 

class Image implements IItem {
    String fileName; 
    int size; 
    String fileType; 

    Image(String fileName, int size, String fileType) {
        this.fileName = fileName; 
        this.size = size; 
        this.fileType = fileType; 
    }
}

class Link implements IItem {
    String name; 
    WebPage page; 

    Link(String name, WebPage page) {
        this.name = name; 
        this.page = page; 
    }
}

class ExamplesWebPage {

    // examples of Texts 
    IItem home = new Text("Home sweet Home"); 
    IItem staff = new Text("the staff"); 
    IItem design = new Text("How to Design Programs"); 
    IItem classy = new Text("Stay class, Java"); 

    // examples of Images 
    IItem lab = new Image("wvh-lab", 400, "png"); 
    IItem profs = new Image("profs", 240, "jpeg"); 
    IItem cover = new Image("htdp", 4300, "tiff");

    //examples of links 
    IItem future = new Link("Back to the Future", this.htdp); 
    IItem back = new Link("A Look Back", this.htdp); 
    IItem ahead = new Link("A Look Ahead", this.ood);

    //examples of list of items 
    ILoItems empty = new MtLoItems(); 
    ILoItems htdpItems = new ConsLoItems(this.design, 
            new ConsLoItems(this.cover,this.empty)); 
    ILoItems oodItems = new ConsLoItems(this.classy, 
            new ConsLoItems(this.future, this.empty)); 
    ILoItems fundiesItems = new ConsLoItems(this.home, 
            new ConsLoItems(this.lab, 
                    new ConsLoItems(this.staff, 
                            new ConsLoItems(this.profs, 
                                    new ConsLoItems(this.back, 
                                            new ConsLoItems(this.ahead, this.empty))))));

    //examples of Web Pages 
    WebPage htdp = new WebPage("HtDP", "htdp.org", this.htdpItems);
    WebPage ood = new WebPage("OOD", "ccs.neu.edu/OOD", this.oodItems); 
    WebPage fundiesWP = new WebPage("Fundies II", "ccs.neu.edu/Fundies2", this.fundiesItems);
}

运行此命令时,输出显示创建链接时对网页的引用为空;它会产生一条消息,表明链接的页面字段为空

我尝试切换示例的创建顺序,这只会导致不同的内容显示为空(网页中的列表字段为空,或者列表中的一项为空)。

如何解决此问题?我需要为此运行代码,但是如果它们无法正确实例化,我将无能为力。

0 个答案:

没有答案