-5
object a = new object(); 
method(object a);

如果我在这个方法中改变了 a 的值,那么这个方法之外的 a 的值也应该改变。但

enum b = enum.something; 
method(enum b);

如果我在这个方法中改变b的值,在这个方法之外改变b的值,我发现它并没有改变。我不知道为什么?

4

3 回答 3

1

enum不是类型,enum是声明性关键字。此外,参数类型不在方法调用中声明,而是在方法声明中声明。这会更正确:

public class Main {

    public enum Suit { CLUBS, SPADES, HEARTS, DIAMONDS }

    public static void main(String[] args) {
        Suit suit = Suit.CLUBS;
        print(suit);
    }

    public static void print(Suit suit) {
        System.out.println(suit);
    }
}
于 2013-07-31T23:32:49.383 回答
0

不确定您在做什么或在问什么,但我知道这似乎与您的问题相关:

MyObject aOutside = new MyObject(); // 
method(MyObject a) {
  a.setSomeValue("value"); // Change the object referenced by aOutside
  a = new MyObject(); // Does *NOT* change aOutside
}

enum MyEnum { AAA, BBB; }
MyEnum  bOutside = MyEnum.something; 
method(MyEnum b) {
  b = MyEnum.AAA; // Does *NOT* change bOutside
}
于 2013-08-01T00:32:14.013 回答
0
Object a = new Object(); 
method(a);
public void method(Object a){
        // do operation on Object a 
}

由于您正在传递对象引用,因此更改将反映在您的实际对象上。

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Because they are constants, the names of an enum type's fields are in uppercase letters.

枚举是常量,旨在保持其状态。就像public static final常量一样。

于 2013-07-31T23:43:19.257 回答