The standard class...is it mutable or not?
问题:
回答1:
It depends strongly on the language. Some of them do not even allow mutable objects.
Many mainstream languages default to being highly mutable, depending on what members you expose on your class's public interface. In at least a couple mainstream languages (particularly dynamic languages) it is really hard to make immutable objects.
See a definition of (im)mutable for more information:
In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created.
回答2:
A mutable class is one that can change its internal state after it is created.
Generally speaking, a class is mutable unless special effort is made to make it immutable.
回答3:
A mutable class is a class that has a changeable state.
for example, if you have a class representing a number, Number
, then it is mutable if you can do something like
Number num(4);
num.set(5);
i.e., change the internal state.
from Wikipedia:
In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created. An object can be either entirely immutable or some attributes in the object may be declared immutable; for example, using the const member data attribute in the C++ programming language.
回答4:
"Usually" (as in usual languages) it is mutable.
回答5:
Classes are typically not mutable (though some languages deviate from this). The objects that you create from classes, on the other hand, are often mutable if they include state and you do not take special care to prevent anyone from changing that state after object creation.