简体   繁体   English

在 java 中无法访问类型保存的封闭实例

[英]No enclosing instance of type savings is accessible in java

im just getting message like "No enclosing instance of type savings is accessible."我只是收到类似“无法访问类型保存的封闭实例”之类的消息。 starting in line 17. I don't know what kind of instance that the IDE means.从第 17 行开始。我不知道 IDE 表示哪种实例。 I'm learning java for 2 weeks now.我现在正在学习 java 2 周。

public class savings {
class MyInfo{
    String name;
    int balance;
}
class MyParent{
    String name;
    int balance;
}
class Mysister{
    String name;
    int balance;
}
public class Mainsimulationsavings {
public static void main(String[] args) {
    
    MyInfo me = new MyInfo();
    me.name ="gaga";
    me.balance =1000000;
    
    MyParent parent = new MyParent();
    parent.name ="Abdul";
    parent.balance =20000000;
    
    Mysister sis = new Mysister();
    sis.name ="Ifdah";
    sis.balance =500000 ;
    
    System.out.printf("|%-5s|%-10s|%-10s|%-10s|\n", "No", "No.ID", "balance", "name");
    System.out.printf("|%-5s|%-10s|%-10s|%-10s|\n", "--", "----", "-----", "----");
    System.out.printf("|%-5s|%-10s|%-10s|%-10s|\n", "1", "12345", "Rp."+me.balance,me.name);
    System.out.printf("|%-5s|%-10s|%-10s|%-10s|\n", "2", "54321", "Rp."+parent.balance,parent.name);
    System.out.printf("|%-5s|%-10s|%-10s|%-10s|\n", "3", "67890", "Rp."+sis.balance,sis.name);
}
}

} }

Sticking classes in classes makes 'instance classes' which you don't want, usually.通常,在类中粘贴类会产生您不想要的“实例类”。 They have an extra invisible field of their outer type: You can't make new instances of eg Mysister here without also having an instance of savings available, which you don't.它们的外部类型有一个额外的不可见字段:如果没有可用的savings实例,您不能在这里创建例如Mysister的新实例,而您没有。 This is all very confusing so you never want to do this until you're much more advanced in java (and even then, I generally avoid it, saving a few characters at the cost of confusing code is rarely a good tradeoff).这一切都非常令人困惑,所以你永远不想这样做,直到你在 java 中更高级(即使那样,我通常会避免它,以混淆代码为代价保存几个字符很少是一个好的权衡)。

Either don't stick classes in classes, or remember to always mark classes-in-classes as static .要么不要在课堂上坚持课堂,要么记住始终将课堂上的课堂标记为static Such as static class MyParent { .static class MyParent { .

The Answer by rzwitserloot is correct. rzwitserloot 的回答是正确的。

The likely solution is to move those related classes to outside the Savings class.可能的解决方案是将那些相关类移到Savings class之外

Also, given your naming, you seem to be confusing the idea of classes with instances.此外,鉴于您的命名,您似乎混淆了类与实例的概念。 There is no need for MyInfo and MyParent and MySister .不需要MyInfoMyParent以及MySister The state and behavior of those are all the same. state 和它们的行为都是一样的。 The only difference is their relationship to you.唯一的区别是他们与你的关系。 Instead, using a single class Person .相反,使用单个 class Person And, either add a relationship member field to the class, or imply the relationship through the naming of instance variables.并且,要么给class增加一个relationship成员字段,要么通过实例变量的命名来暗示关系。 See code below.请参阅下面的代码。

Tip: Define your classes more briefly as a record if their main purpose is to communicate data transparently and immutably.提示:如果您的类的主要目的是透明且不变地传递数据,则将您的类更简短地定义为记录。

By the way, watch your naming.顺便说一句,注意你的命名。 Java naming conventions start a class name with an uppercase letter. Java 命名约定以大写字母开头 class 名称。 So Savings , not savings .所以Savings ,不是savings

record Person ( String name , int balance ) {}

Another tip: A record can be declared locally, within a method.另一个提示:记录可以在方法中本地声明。 Or declare by nesting in a class. Or declare separately, in its own .java file.或者通过嵌套在 class 中声明。或者单独声明,在它自己的.java文件中。

Yet another tip: You can use a LOW LINE (underscore) character to arbitrarily group the digits of a numeric literal.另一个提示:您可以使用 LOW LINE(下划线)字符对数字文字的数字进行任意分组。

In your main method, or other such code, instantiate.在您的main方法或其他此类代码中,实例化。

Person me = new Person( "Gaga" , 1_000_000 ) ;
Person parent = new Person( "Abdul" , 20_000_000 ) ;
Person sister = new Person( "Ifdah" , 500_000 ) ;

Make a list and loop, to report.做一个列表和循环,报告。

List<Person> persons = List.of( me , parent , sister ) ;
for( Person person : persons ) 
{
    System.out.println( "Name: " + person.name() … ) ;
}

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

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