在java中解除复制的实例变量的链接
我不熟悉在java中解除复制的实例变量的链接,java,oop,Java,Oop,我不熟悉java,我希望能够更改一个实例变量,该变量的值是从其他实例复制来的,我会得到一些奇怪的结果。下面是一些简单的代码 DummyBean类{ private Integer[] instant = new Integer[9]; private Integer state; Integer[] getInstant() { return instant; } void setInstant(Integer[] arr) { for(int index =0;index
java
,我希望能够更改一个实例变量,该变量的值是从其他实例复制来的,我会得到一些奇怪的结果。下面是一些简单的代码
DummyBean类{
private Integer[] instant = new Integer[9];
private Integer state;
Integer[] getInstant() {
return instant;
}
void setInstant(Integer[] arr) {
for(int index =0;index<9;index++)
this.instant[index] = arr[index];
}
Integer getState() {
return state;
}
void setState(Integer state) {
this.state = state;
}
public DummyBean(){
}
String getDummy() {
return dummy;
}
void setDummy(String dummy) {
this.dummy = dummy;
}
private String dummy;
public DummyBean(DummyBean another) {
this.dummy = another.getDummy();
this.instant = another.getInstant();
this.state = another.getState();
}
public static void main(String[] args) throws CloneNotSupportedException {
DummyBean dum = new DummyBean();
Integer[] test = new Integer[9];
for(int index =0;index<9;index++)
test[index] = 1;
dum.setInstant(test);
dum.setState(10);
System.out.println(dum.getInstant()[0]);
System.out.println(dum.getState());
DummyBean dumtwo = new DummyBean(dum);
System.out.println(dumtwo.getInstant()[0]);
System.out.println(dumtwo.getState());
dum.getInstant()[0] = 2;
dum.setState(20);
System.out.println(dum.getInstant()[0]);
System.out.println(dum.getState());
System.out.println(dumtwo.getInstant()[0]);
System.out.println(dumtwo.getState());
}
在最后两个值中,我知道dumtwo
state实例没有改变,但是为什么dumtwoinstant
变量会相应地改变dum
改变呢
顺便说一句,要获得复制的
dumtou
而不是继承自dum
更改?您正在为DummyBean的两个实例引用相同的整数数组。如果您希望每个实例都有自己的整数数组,则需要复制它。您正在为DummyBean的两个实例引用相同的整数数组。如果您希望如果每个数组都有自己的整数数组,则需要对其进行复制。因为在复制构造函数中,dum
和dumtou
引用了相同的数组,所以您没有在构造函数中复制数组:
public DummyBean(DummyBean another) {
this.dummy = another.getDummy();
this.instant = another.getInstant();
this.state = another.getState();
}
public DummyBean(DummyBean another) {
this.dummy = another.getDummy();
setInstant(another.getInstant();
this.state = another.getState();
}
因此,任何对象实例中的任何更改都会影响其他对象
最后,您需要创建实例数组的新副本,以便可以使用:
System.arraycopy();
因为在复制构造函数中,
dum
和dumtoo
引用了相同的数组,所以您没有在构造函数中复制数组:
public DummyBean(DummyBean another) {
this.dummy = another.getDummy();
this.instant = another.getInstant();
this.state = another.getState();
}
public DummyBean(DummyBean another) {
this.dummy = another.getDummy();
setInstant(another.getInstant();
this.state = another.getState();
}
因此,任何对象实例中的任何更改都会影响其他对象
最后,您需要创建实例数组的新副本,以便可以使用:
System.arraycopy();
在构造函数中复制数组引用
this.instant=other.getInstant();
。因为数组是一个状态为的对象
,所以可以对其进行修改
Integer
和String
都是不可变的,所以复制引用是可以的
您需要进行setInstant
final
:
final void setInstant(Integer[] arr) {
for(int index =0;index<9;index++)
this.instant[index] = arr[index];
}
或使用
另外,请根据Java约定对代码进行格式化和组织:
public class DummyBean {
private Integer[] instant = new Integer[9];
private Integer state;
private String dummy;
public DummyBean() {
}
public DummyBean(DummyBean another) {
this.dummy = another.getDummy();
setInstant(another.getInstant());
this.state = another.getState();
}
Integer[] getInstant() {
return instant;
}
final void setInstant(Integer[] arr) {
for (int index = 0; index < 9; index++) {
this.instant[index] = arr[index];
}
}
Integer getState() {
return state;
}
void setState(Integer state) {
this.state = state;
}
String getDummy() {
return dummy;
}
void setDummy(String dummy) {
this.dummy = dummy;
}
}
公共类DummyBean{
私有整数[]即时=新整数[9];
私有整数状态;
私有字符串虚拟;
公共DummyBean(){
}
公共DummyBean(另一个DummyBean){
this.dummy=另一个.getDummy();
setInstant(另一个.getInstant());
this.state=另一个.getState();
}
整数[]getInstant(){
返回瞬间;
}
最终void setInstant(整数[]arr){
对于(int-index=0;index<9;index++){
this.instant[index]=arr[index];
}
}
整型getState(){
返回状态;
}
无效设置状态(整数状态){
this.state=状态;
}
字符串getDummy(){
返回假人;
}
void setDummy(字符串伪){
这个.dummy=dummy;
}
}
i、 e.首先是变量声明。然后是构造函数。最后是方法。在构造函数中复制数组引用
this.instant=other.getInstant();
。由于数组是具有状态的对象,因此可以对其进行修改
Integer
和String
都是不可变的,所以复制引用是可以的
您需要进行setInstant
final
:
final void setInstant(Integer[] arr) {
for(int index =0;index<9;index++)
this.instant[index] = arr[index];
}
或使用
另外,请根据Java约定对代码进行格式化和组织:
public class DummyBean {
private Integer[] instant = new Integer[9];
private Integer state;
private String dummy;
public DummyBean() {
}
public DummyBean(DummyBean another) {
this.dummy = another.getDummy();
setInstant(another.getInstant());
this.state = another.getState();
}
Integer[] getInstant() {
return instant;
}
final void setInstant(Integer[] arr) {
for (int index = 0; index < 9; index++) {
this.instant[index] = arr[index];
}
}
Integer getState() {
return state;
}
void setState(Integer state) {
this.state = state;
}
String getDummy() {
return dummy;
}
void setDummy(String dummy) {
this.dummy = dummy;
}
}
公共类DummyBean{
私有整数[]即时=新整数[9];
私有整数状态;
私有字符串虚拟;
公共DummyBean(){
}
公共DummyBean(另一个DummyBean){
this.dummy=另一个.getDummy();
setInstant(另一个.getInstant());
this.state=另一个.getState();
}
整数[]getInstant(){
返回瞬间;
}
最终void setInstant(整数[]arr){
对于(int-index=0;index<9;index++){
this.instant[index]=arr[index];
}
}
整型getState(){
返回状态;
}
无效设置状态(整数状态){
this.state=状态;
}
字符串getDummy(){
返回假人;
}
void setDummy(字符串伪){
这个.dummy=dummy;
}
}
i、 首先是变量声明,然后是构造函数,最后是方法。试试这个,你会得到答案:
System.out.println(dum.instant.toString());
System.out.println(dumtwo.instant.toString());
试试这个,你会得到你的答案:
System.out.println(dum.instant.toString());
System.out.println(dumtwo.instant.toString());
因为您正在为dum
和dumtwo
引用同一个对象instant
public DummyBean(DummyBean another) {
this.dummy = another.getDummy();
this.instant = setArrayCopy(another);
this.state = another.getState();
}
private Integer[] setArrayCopy(DummyBean another){
Integer[] arr = new Integer[9];
for(int index =0;index<9;index++)
arr[index] = another.getInstant()[index];
return arr;
}
公共DummyBean(另一个DummyBean){
this.dummy=另一个.getDummy();
this.instant=setArrayCopy(另一个);
this.state=另一个.getState();
}
私有整数[]setArrayCopy(DummyBean另一个){
整数[]arr=新整数[9];
对于(int index=0;index,因为您正在为dum
和dumtoo
引用同一对象instant
。在构造函数中可以做的是
public DummyBean(DummyBean another) {
this.dummy = another.getDummy();
this.instant = setArrayCopy(another);
this.state = another.getState();
}
private Integer[] setArrayCopy(DummyBean another){
Integer[] arr = new Integer[9];
for(int index =0;index<9;index++)
arr[index] = another.getInstant()[index];
return arr;
}
公共DummyBean(另一个DummyBean){
this.dummy=另一个.getDummy();
this.instant=setArrayCopy(另一个);
this.state=另一个.getState();
}
私有整数[]setArrayCopy(DummyBean另一个){
整数[]arr=新整数[9];
对于(int index=0;index