获取错误:无法访问类型为****的封闭实例

时间:2015-09-19 04:08:43

标签: java compiler-errors

我正在努力学习我一直在编写的代码。我做了一些研究,但找不到解决方案。

这是我的完整错误:

  

无法访问类型Project2的封闭实例。必须使用Project2类型的封闭实例限定分配(例如x .new A(),其中xProject2的实例。

这是我的完整代码:

import java.io.*;
import java.util.Scanner;

public class Project2 {
    public static void main(String[] args) throws IOException {

        SuperheroWithPowers aquaman = new SuperheroWithPowers();

        Superhero2 catwoman = new Superhero2();

        //Aquaman calls marine life
        aquaman.setTelepathy(true);
        if (aquaman.getTelepathy()) {
            System.out.println("Aquaman calls all marine life around him to help defeat an evil villian!");
        }

        //Aquaman fights in the depths
        aquaman.setAdaptability(true);
        if (aquaman.getAdaptability()) {
            System.out.println("Aquaman is able to hand very high depths of water due to his ability to adapt to the ocean!");
        }

        //Aquaman tries to use his super strength
        aquaman.setStrength(true);
        if (aquaman.getStrength()) {
            System.out.println("Aquaman tries to use his super strength but the villian is stronger!");
        }

        //Aquaman uses the trident
        aquaman.setTrident(true);
        if (aquaman.getTrident()) {
            System.out.println("Aquaman finally defeats his evil foe with his powerful trident!");
        }


        //Catwoman uses her equipment
        catwoman.setEquipment(true);
        if (catwoman.getEquipment()) {
            System.out.println("Catwoman uses her bola to trip up evil");
        }

        //Catwoman uses her gymnastics
        catwoman.setGymnast(true);
        if (catwoman.getGymnast()) {
            System.out.println("Catwoman escapes a near death by being very agile");
        }

        //Catwoman uses judo
        catwoman.setJudo(true);
        if (catwoman.getJudo()) {
            System.out.println("Catwoman has incredible combat skills to take down any foe in her path");
        }

        //Catwoman uses thief
        catwoman.setThief(true);
        if (catwoman.getThief()) {
            System.out.println("Catwoman is easily able to sneak at night due to her master theif skills");
        }
    }


    class SuperheroWithPowers {
        String name;
        String alterEgo;
        boolean telepathy;
        boolean adaptability;
        boolean strength;
        boolean trident;

        //Constructor
        public SuperheroWithPowers() {
            name = "Aquaman";
            alterEgo = "Arthur Curry";
            System.out.println("Aquaman created!");
        }//End Constructor


        //Start setters
        public void setTelepathy(boolean x) {
            telepathy = x;
        }

        public void setAdaptability(boolean x) {
            adaptability = x;
        }

        public void setStrength(boolean x) {
            strength = x;
        }

        public void setTrident(boolean x) {
            trident = x;
        }
        //End Setters

        //Start getters
        public boolean getTelepathy() {
            return telepathy;
        }

        public boolean getAdaptability() {
            return adaptability;
        }

        public boolean getStrength() {
            return strength;
        }

        public boolean getTrident() {
            return trident;
        }
        //End Getters

    }//end SuperheroWithPowers

    class Superhero2 {
        String name;
        String alterEgo;
        boolean equipment;
        boolean gymnast;
        boolean judo;
        boolean thief;

        //Constructor
        public Superhero2() {
            name = "Catwoman";
            alterEgo = "Selina Kyle";
            System.out.println("Catwoman created!");
        }//end constructor

        //Start setters
        public void setEquipment(boolean x) {
            equipment = x;
        }

        public void setGymnast(boolean x) {
            gymnast = x;
        }

        public void setJudo(boolean x) {
            judo = x;
        }

        public void setThief(boolean x) {
            thief = x;
        }
        //End setters

        //Start getters
        public boolean getEquipment() {
            return equipment;
        }

        public boolean getGymnast() {
            return gymnast;
        }

        public boolean getJudo() {
            return judo;
        }

        public boolean getThief() {
            return thief;
        }

    }//End Getters
}//End Superhero2

这个任务的重点是使用构造函数,我认为我错了,但我不能把手指放在它上面。

1 个答案:

答案 0 :(得分:0)

问题是在Project2类中缺少右大括号并在Superhero2类中放置一个右大括号。

在Project2类中添加右大括号:

    //Catwoman uses thief
    catwoman.setThief(true);
    if(catwoman.getThief()){
      System.out.println("Catwoman is easily able to sneak at night due to her master theif skills");
    }
  }
} //Add this right curly brace to close the Project2 class

从Superhero2类中删除最后一个右大括号:

}//End Superhero2 -> Remove this right curly brace

我建议您始终正确缩进代码,这样可以避免此类问题。