简体   繁体   English

Java 没有可访问的封闭类型实例

[英]Java No enclosing instance of type is accessible

there I'm pretty new to Java and have german class and method titles.在那里,我对 Java 很陌生,并且拥有德语类和方法标题。 This Code is meant to give a string output for every class extending "Musiker".此代码旨在为每个扩展“Musiker”的类提供字符串输出。 I have already looked on SO but my problem is that changing it to static gives an error on the class itself.我已经看过 SO 但我的问题是将其更改为静态会在类本身上产生错误。 The main reason why I open a new Question is, that every other class is working as planned.我打开一个新问题的主要原因是,其他所有课程都按计划进行。 And please don't wonder why the Strings look weird, the Book I copied this from is meant to be humoristic.请不要想知道为什么弦乐看起来很奇怪,我从中复制的书是幽默的。

public class Proberaum {
public static void main(String[] args) {

    try {

    Musiker saenger = new Saenger();
    Musiker gitarrist = new Gitarrist();
    Musiker bassist = new Bassist();
    Musiker trompeter = new Trompeter();
    Musiker backgroundSaengerin = new BackgroundSaengerin();
    machtMusik(saenger, gitarrist, bassist, trompeter, backgroundSaengerin);

    } catch(Exception e) {
        new Exception().printStackTrace();
    }
}

public static void machtMusik(Musiker... gruppe) {
    for(Musiker musiker : gruppe) {
      musiker.musizieren();
    }
  }


public class Musiker {

    private String name;
    private int alter;
    private Band band;

    public void musizieren() {
        System.out.println("OO Mmmmmmmmh, OO Mmmmmmmmh");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAlter() {
        return alter;
    }

    public void setAlter(int alter) {
        this.alter = alter;
    }

    public Band getBand() {
        return band;
    }

    public void setBand(Band band) {
        this.band = band;
    }
}

public class Band {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
public class Saenger extends Musiker {

    @Override
    public void musizieren() {
        this.singen();
    }

    public void singen() {
        System.out.println("Oh, bäbi, juuuu a mei sannnnscheiiiiin");
    }
}
public class BackgroundSaengerin extends Saenger {
}

public class Bassist extends Musiker {
}

public class Gitarrist extends Musiker {

    public void musizieren() {
        System.out.println("Tschiiiiiingzäääängggggg");
    }   
}
public class Trompeter extends Musiker {

}

} }

Your Saenger class is actually a non-static member of the Proberaum class.您的Saenger类实际上是Proberaum类的非静态成员。 Because it's non-static, you actually need to create an instance of Proberaum before you can use any of these classes:因为它是非静态的,所以您实际上需要先创建Proberaum的实例,然后才能使用这些类中的任何一个:

Proberaum proberaumObject = new Proberaum();
Musiker saenger = new proberaumObject.Saenger();

In your case, classes inside classes is probably not what you want to do.在您的情况下,类中的类可能不是您想要做的。 If you extract each of your classes into its own file, you should find your problem going away.如果您将每个类提取到自己的文件中,您应该会发现问题消失了。 (If that's not possible for whatever reason, declaring your subclasses as static should work too.) (如果由于某种原因无法做到这一点,将您的子类声明为static应该有效。)

Like Joe C also mentioned in his answer: the core of the problem is that your classes Saenger , Musiker , etc etc. are all nested classes (nested inside Proberaum ), but they are defined as non-static.像乔C也以他的答复中提到:这个问题的核心是你的类SaengerMusiker ,等等等等,都是嵌套类(嵌套在Proberaum ),但它们定义为非静态的。

In Java, non-static nested classes are called "inner classes".在 Java 中,非静态嵌套类称为“内部类”。 Inner classes have implicit access to their enclosing class members (even private ones), but of course the flipside of this is that there first needs to be an object of that enclosing class for the inner class to reference.内部类可以隐式访问它们的封闭类成员(甚至是私有成员),但当然另一方面,首先需要一个封闭类的对象供内部类引用。 That is why the compiler is complaining in your example: you're trying to create an object of class Saenger , which is an inner class of Proberaum , so to create that object it needs to have a reference to an object of type Proberaum .这就是编译器在您的示例中抱怨的原因:您正在尝试创建类Saenger的对象,它是Proberaum的内部类,因此要创建该对象,它需要引用Proberaum类型的对象。 Since you're doing the object creation in the (static) main method, no such object exists.由于您在(静态) main方法中创建对象,因此不存在此类对象。

So, to fix, you have to change your inner classes.所以,要解决这个问题,你必须改变你的内部类。 Easiest is to declare them all static .最简单的方法是将它们全部声明为static Note that you can do this is in addition to be making them public :请注意,除了将它们设为public之外,您还可以这样做:

 public static class Seanger extends Musiker { ... 

As also remarked elsewhere however, you really should not put every class in the same file.然而,正如其他地方所说,你真的不应该把每个类都放在同一个文件中。 Learn to work with one class per file, it's the Java Way™.学习为每个文件使用一个类,这就是 Java Way™。

Instead of declaring the nested classes as static, one can alternatively create objects of nested classes like mentioned below.除了将嵌套类声明为静态,还可以创建如下所述的嵌套类的对象。

Proberaum proberaumObject = new Proberaum();
Musiker saenger = proberaumObject.new Saenger();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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