What is a “Mutable class” in OOP?

2019-05-07 13:10发布

The standard class...is it mutable or not?

标签: oop
5条回答
等我变得足够好
2楼-- · 2019-05-07 13:46

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.

查看更多
疯言疯语
3楼-- · 2019-05-07 13:46

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楼-- · 2019-05-07 13:53

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.

查看更多
Juvenile、少年°
5楼-- · 2019-05-07 13:59

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.

查看更多
地球回转人心会变
6楼-- · 2019-05-07 14:08

"Usually" (as in usual languages) it is mutable.

查看更多
登录 后发表回答