简体   繁体   中英

Eclipse doesn't recognize anonymous inner classes

For some reason, Eclipse does not seem to recognize when anonymous inner classes are used and prints an error message that the anonymous class in question does not exist. After trying to run the following example, which is copied directly from the book Thinking in Java, I get Contents cannot be resolved to a type

public class Parcel7 {
    public Contents contents() {
        return new Contents() { // Insert a class definition
            private int i = 11;

            public int value() {
                return i;
            }
        }; // Semicolon required in this case
    }

    public static void main(String[] args) {
        Parcel7 p = new Parcel7();
        Contents c = p.contents();
    }
}

Am I missing something obvious?

必须有一个名为Contents的接口才能起作用,您确定它已创建或导入吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM