简体   繁体   English

Java中这是什么类型的变量声明

[英]What type of variable declaration is this in java

class FreshJuice {
   enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
   FreshJuiceSize size;
}

What type of variable is size ? size是什么类型的变量?

Is FreshJuiceSize size; FreshJuiceSize size; a shorhand way of declaring size as a instance variable ? 一种将size声明为实例变量的快捷方式?

In your example, size is a member variable with type FreshJuiceSize (an enum constrained to the 3 values you defined). 在您的示例中, size是一个类型为FreshJuiceSize的成员变量(一个enum限制为您定义的3个值)。

Perhaps what makes it look unusual is the absence of an access modifier: public , protected or private . 也许使它看起来异常的原因是缺少访问修饰符: publicprotectedprivate For a member with no declared access modifier, it is "package-private", meaning that other classes within the same package may access it, but classes outside the package may not. 对于没有声明的访问修饰符的成员,它是“ package-private”,这意味着同一包内的其他类可以访问它,但包外的类则不能访问。 The Java documentation on Controlling Access to Members of a Class describes this. 有关控制对类成员的访问的Java文档对此进行了描述。

Yes. 是。 The variable type of size is enum FreshJuiceSize . 大小的变量类型是enum FreshJuiceSize

Is FreshJuiceSize size; 是FreshJuiceSize大小; a shorhand way of declaring size as a instance variable ? 一种将大小声明为实例变量的快捷方式?

You are just creating a reference of type FreshJuiceSize which is an enum with a set of predefined constants . 您只是在创建FreshJuiceSize类型的引用,该引用是带有一组预定义常量的枚举。 There is no "short hand" or "long hand". 没有“短手”或“长手”。

Size是FreshJuiceSize类型的变量FreshJuiceSize是一个枚举,因此大小仅采用{SMALL,MEDIUM,LARGE}集之一

FeshJuice is a class - most likely private - not main - judging by the lack of public class classname declaration - FreshJuiceSize is the enum name (similar to the name of an array), and FreshJuiceSize size; FeshJuice是一个类-很可能是私有的-不是主要的-根据缺少公共类的类名声明来判断-FreshJuiceSize是枚举名称(类似于数组的名称),并且FreshJuiceSize的大小; declares the variable size to be a member (I forget the term) of FreshJuiceSize 声明变量size为FreshJuiceSize的成员(我忘记了这个术语)

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

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