简体   繁体   English

(Java) Static 成员通过枚举器的实例引用访问

[英](Java) Static member accessed via instance reference with enumerators

I just started learning Java.我刚开始学习Java。 IntelliJ is giving me a warning "Static member accessed via instance reference" on line 4. Is it bad, should I fix it, somehow, or should I just ignore it? IntelliJ 在第 4 行给我一个警告“通过实例引用访问的静态成员”。这很糟糕,我应该以某种方式修复它,还是应该忽略它?

Here is my code:这是我的代码:

public class MainClass {
    public static void main(String[] args) {
        Dog randomDog = new Dog();
        Dog myDog = new Dog(4, "Charlie", new Dog().breed.Labrador);
        System.out.println("Random dog's name is: " + randomDog.name + ", it's age is: " + randomDog.age + " and it's breed is: " + randomDog.breed);
        System.out.println("My dog's name is: " + myDog.name + ", it's age is: " + myDog.age + " and it's breed is: " + myDog.breed);
    }
}

and the Dog class:和狗 class:

public class Dog {
    int age;
    String name;
    enum breed { Poodle, Shepherd, Labrador }
    breed breed;

    Dog(int age, String name, breed breed) {
        this.age = age;
        this.name = name;
        this.breed = breed;
    }
    Dog() {
        this.age = 0;
        this.name = "Rex";
        this.breed = breed.Labrador;
    }
}

One issue (which causes others) is that you're hiding the type breed by also having a field of the same name in the same scope.一个问题(导致其他问题)是您通过在同一个 scope 中也有一个同名字段来隐藏类型breed

That's a very rare problem to have, because the naming conventions of Java usually prevent this kind of clash: Types (classes, interfaces, enums, annotations) are usually written in CamelCase whereas field names start with a lower case letter ( fieldName ).这是一个非常罕见的问题,因为 Java 的命名约定通常可以防止这种冲突:类型(类、接口、枚举、注释)通常用CamelCase编写,而字段名称以小写字母( fieldName )开头。 While this is not technically a "rule" that the compiler enforces, following this makes your code much more readable to others and also avoids the follow-up problem of hiding the type.虽然这在技术上不是编译器强制执行的“规则”,但遵循此规则会使您的代码对其他人更具可读性,并且还避免了隐藏类型的后续问题。 Also note that constant fields.还要注意常量字段。

I also made two changes that are good ideas but not really related to your issue:我还做了两个好主意但与您的问题无关的更改:

  • constant values (ie most static final fields an enum constants) use ALL_UPPER casing, so I also changed your Breed values常量值(即大多数static final字段一个枚举常量)使用ALL_UPPER大小写,所以我也改变了你的Breed
  • I've moved the nested type definition to the top of your Dog class so as not to hide it within all the instance fields.我已将嵌套类型定义移至Dog class 的顶部,以免将其隐藏在所有实例字段中。 This is just to keep those things that logically belong together close to each other.这只是为了让那些在逻辑上属于一起的东西彼此靠近。
public class Dog {
    enum Breed { POODLE, SHEPHERD, LABRADOR }

    int age;
    String name;
    Breed breed;

    Dog(int age, String name, Breed breed) {
        this.age = age;
        this.name = name;
        this.breed = breed;
    }
    Dog() {
        this.age = 0;
        this.name = "Rex";
        this.breed = Breed.LABRADOR;
    }
}

Changing all these names in the main class and replacing the unneded new Dow().breed with just Dog.Breed produces this working code:更改主 class 中的所有这些名称并仅用Dog.Breed替换未定义的new Dow().breed会产生以下工作代码:

public class MainClass {
    public static void main(String[] args) {
        Dog randomDog = new Dog();
        Dog myDog = new Dog(4, "Charlie", Dog.Breed.LABRADOR);
        System.out.println("Random dog's name is: " + randomDog.name + ", it's age is: " + randomDog.age + " and it's breed is: " + randomDog.breed);
        System.out.println("My dog's name is: " + myDog.name + ", it's age is: " + myDog.age + " and it's breed is: " + myDog.breed);
    }
}

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

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