简体   繁体   English

“静态”关键字有什么作用?

[英]What does the 'Static' keyword do?

I have a program where there are two shapes Player1 and Player2, both inherited by the class Player. 我有一个程序,其中有两个形状Player1和Player2,它们都由类Player继承。

Public class Player{

    public int xPos;
    public int yPos;

    // more code

}

The player1 class: player1类:

Public class Player1 extends Player{

    public Player(){
        xPos = 200;
        yPos = 200;
    }
    // more code
}

The Player2 class: Player2类:

Public class Player2 extends Player{

    public Player2(){

        xPos = 400;
        yPos = 200;
    }
// more code
}

In this case, should I use static for the xPos and yPos in the Player class? 在这种情况下,我应该对Player类中的xPos和yPos使用static吗?

不能。静态变量是类的成员,您希望位置变量成为对象的成员(非静态)。

如果xPos和yPos是静态的,则Player的每个实例将具有相同的位置。

The static keyword in front of a variable signifies that the variable in question belongs to the "class itself" as opposed to individual instances. 变量前面的static关键字表示该变量属于“类本身”,而不是单个实例。 In other words changing it with one instance changes it with all of them. 换句话说,一个实例更改它会同时更改所有实例。 So in this case, you should not do what you are asking. 因此,在这种情况下,您不应该执行自己的要求。

No, 没有,

static as a keyword in Java means that the field is a Class field not a instance field. static作为Java中的关键字表示该字段是Class字段而不是instance字段。 These are supposed to be used for fields that are used between instances of the class and don't have any meaning for a given instance. 这些应该用于在类的实例之间使用的字段,并且对于给定的实例没有任何意义。 In your case the xPos and yPos are for each instance of the class. 在您的情况下, xPosyPos用于该类的每个实例。

Take a look at this static tutorial 看看这个静态教程

The short answer is no . 简短的答案是否定的

A static variable or method means that no instance of this object is required to make use of it (for example, we don't create a new instance of class System when we do System.out.println ). static变量或方法意味着不需要使用此对象的实例(例如,在执行System.out.println时,我们不会创建System类的新实例)。 It also means that the state of the object is consistent every time we call it; 这也意味着每次我们调用它时,对象的状态都是一致的。 that is to say, if we set some value inside of a class with a static field, that value will stay consistent throughout all instances of that class. 也就是说,如果我们在带有静态字段的类中设置某个值,则该值将在该类的所有实例中保持一致。

Every x and y position is unique with respect to each instance of your Player object. 相对于Player对象的每个实例,每个x和y位置都是唯一的。 You don't want to use static . 不想使用static

You use static when you want the values to be same in all its references. 如果希望所有引用中的值都相同,请使用static。 In your case i guess you want the xPos and yPos to be different for different players. 在您的情况下,我想您希望xPosyPos对于不同的玩家来说是不同的。 In other words for each object you create for the class Player you want xPos and yPos to be different. 换句话说,对于您为类Player创建的每个对象,希望xPosyPos不同。 So you don't declare them as static. 因此,您不要将它们声明为静态的。

Additional Info: If you declare some variable as static, the method that uses it should be declared as static too. 其他信息:如果您将某个变量声明为静态,则使用该变量的方法也应声明为静态。

No. Don't use static attributes in this case because xPos and yPos are attributes (properties) of each Player. 不可以。在这种情况下,请不要使用静态属性,因为xPos和yPos是每个Player的属性(属性)。 Each Player that is created (instance) must have its own x and y. 每个创建的玩家(实例)必须具有自己的x和y。

The static keyword is used to create something that belongs to the class and not to the instance of the class. static关键字用于创建属于该类而不属于该类实例的内容。

For example, if xPos was static you could call Player.xPos to get the value of xPos, without the need to have an instance of the class, and all Players would have the same xPos and yPos. 例如,如果xPos是静态的,则可以调用Player.xPos来获取xPos的值,而无需具有该类的实例,并且所有Player都将具有相同的xPos和yPos。

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

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