(Java) 通过带有枚举器的实例引用访问的静态成员
(Java) Static member accessed via instance reference with enumerators
我刚开始学习Java。 IntelliJ 在第 4 行向我发出警告“通过实例引用访问静态成员”。这是不是很糟糕,我应该以某种方式修复它,还是应该忽略它?
这是我的代码:
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);
}
}
和狗 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;
}
}
一个问题(导致其他问题)是您隐藏了 type breed
也有一个 field在同一范围内具有相同的名称。
这是一个非常罕见的问题,因为 Java 的命名约定通常可以防止这种冲突:类型(classes、接口、枚举、注释)通常写在 CamelCase
而字段名称以小写字母开头 (fieldName
)。虽然这在技术上不是编译器强制执行的“规则”,但遵循此规则会使您的代码对其他人更具可读性,并且还避免了隐藏类型的后续问题。还要注意常量字段。
我还进行了两项更改,它们是不错的想法,但与您的问题并没有真正相关:
- 常量值(即大多数
static final
字段和枚举常量)使用 ALL_UPPER
大小写,所以我也更改了您的 Breed
值
- 我已将嵌套类型定义移至
Dog
class 的顶部,以免将其隐藏在所有实例字段中。这只是为了让那些逻辑上属于一起的东西彼此靠近。
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;
}
}
更改主要 class 中的所有这些名称并将不需要的 new Dow().breed
替换为 Dog.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);
}
}
我刚开始学习Java。 IntelliJ 在第 4 行向我发出警告“通过实例引用访问静态成员”。这是不是很糟糕,我应该以某种方式修复它,还是应该忽略它?
这是我的代码:
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);
}
}
和狗 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;
}
}
一个问题(导致其他问题)是您隐藏了 type breed
也有一个 field在同一范围内具有相同的名称。
这是一个非常罕见的问题,因为 Java 的命名约定通常可以防止这种冲突:类型(classes、接口、枚举、注释)通常写在 CamelCase
而字段名称以小写字母开头 (fieldName
)。虽然这在技术上不是编译器强制执行的“规则”,但遵循此规则会使您的代码对其他人更具可读性,并且还避免了隐藏类型的后续问题。还要注意常量字段。
我还进行了两项更改,它们是不错的想法,但与您的问题并没有真正相关:
- 常量值(即大多数
static final
字段和枚举常量)使用ALL_UPPER
大小写,所以我也更改了您的Breed
值 - 我已将嵌套类型定义移至
Dog
class 的顶部,以免将其隐藏在所有实例字段中。这只是为了让那些逻辑上属于一起的东西彼此靠近。
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;
}
}
更改主要 class 中的所有这些名称并将不需要的 new Dow().breed
替换为 Dog.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);
}
}