Java-仅使用一个私有关键字定义私有字段 [英] Java - define private fields using only one private keyword
本文介绍了Java-仅使用一个私有关键字定义私有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是否可以仅使用一个 private
关键字来定义私有字段?我真正要问的是:是否有可以做的事情像这样:
Is there a way for me to define private fields using only one private
keyword?
What I'm really asking is: is there something that I can do that looks like this:
private {
int x;
int y;
Object myObject;
//etcetera
};
和 x
, y
和 myObject
将全部创建为私有字段
and x
, y
, and myObject
will all be created as private fields
推荐答案
至少在Java中,您不能那样做.因为在Java
At least in java you can't do it like that. Because in java
变量有几种:
- 类中的成员变量-这些称为字段.
- 方法或代码块中的变量,这些变量称为局部变量.
- 方法声明中的变量,这些变量称为参数.
每个变量都有自己的访问修饰符,例如:
And each variables has its own Access Modifiers Like:
-
public
修饰符-该字段可从所有类访问. -
private
修饰符-该字段只能在其自己的类中访问.
public
modifier—the field is accessible from all classes.private
modifier—the field is accessible only within its own class.
因此,您必须执行以下操作:
So you have to do like following:
private int x,y;
private Object myObject;
这篇关于Java-仅使用一个私有关键字定义私有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文