由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 大家写java class的时候是完全封装的么?
相关主题
[转载] 有用Eclipse的嘛?重新拣起C++....
Ant and Netbeans helpdeserializer probelm.Ignore upper. Thx
请教:Junit fails as an Ant taskHelp
新手问一个:如何找.jar文件里都有什么class?Simple question
传递一个object reference,如何防止更改object?spring frame work question
类里的字符串向量赋值,总报错:问一个Collection Update的问题
如何读懂Java程序请教关于使用map和fields
java为啥这么多frameworkRe: [转载] Re: .jar文件是怎么产生的?
相关话题的讨论汇总
话题: object话题: copy话题: what话题: 赋值话题: getb
进入Java版参与讨论
1 (共1页)
T*****e
发帖数: 361
1
比方说,如果class A中有一个object B,
在set/get方法中,是直接赋值呢,还是先拷贝?
直接赋值:
B getB(){ return b; }
void setB( B b ){ this.b = b ; }
拷贝:
B getB(){ return new B(b); }
void setB( B b ){ this.b = new B(b) ; }
当年用c++编程的时候,除了container class之外,
都是用拷贝,这样可以防止引用对象/数据在别处被误删。
java基本上不存在这个问题。
当然了,如果直接赋值,数据在别处被修改就没法避免。
本来想都用拷贝方法来完全封装的,后来觉得还是直接赋值
比较快(写代码、执行)。不过直接赋值可能会导致调试
比较复杂,而且容易被abuse。
大家都是怎么处理这个问题的?
呵呵,可能我需要读一本Thinking in java之类的书。
by the way, 哪儿有电子版么?或者别的推荐?
多谢了先。
d****s
发帖数: 30
2

Because you don't need to aware "delete" an object,
you'd better use what you said "直接赋值".
The system has an automatic gabarge colletion mechanism.
You can't delete an object. If an object is still referenced,
it won't be deleted by the system as well.
Sometimes you have to use Copy. But most time just use reference.

【在 T*****e 的大作中提到】
: 比方说,如果class A中有一个object B,
: 在set/get方法中,是直接赋值呢,还是先拷贝?
: 直接赋值:
: B getB(){ return b; }
: void setB( B b ){ this.b = b ; }
: 拷贝:
: B getB(){ return new B(b); }
: void setB( B b ){ this.b = new B(b) ; }
: 当年用c++编程的时候,除了container class之外,
: 都是用拷贝,这样可以防止引用对象/数据在别处被误删。

m******t
发帖数: 2416
3

I would simply have the setter take over the reference, rather than
making a copy, unless it is *real* important to ensure that the object
is completely shielded, in which case there are lots of complications -
you'd have to make sure to make a deep copy of the object, and the object(s)
need to properly implement equals() and hashCode() to deal with the
"split identity" issue, and then there is also the issue on the way out -
do you make another deep copy in the getter before passing it out to

【在 T*****e 的大作中提到】
: 比方说,如果class A中有一个object B,
: 在set/get方法中,是直接赋值呢,还是先拷贝?
: 直接赋值:
: B getB(){ return b; }
: void setB( B b ){ this.b = b ; }
: 拷贝:
: B getB(){ return new B(b); }
: void setB( B b ){ this.b = new B(b) ; }
: 当年用c++编程的时候,除了container class之外,
: 都是用拷贝,这样可以防止引用对象/数据在别处被误删。

m******t
发帖数: 2416
4

Oops, I forgot to rephrase how garbage collection works in my post,
I guess that puts me in the "have no idea what gc is" group, too. LOL
I am glad we agree on this, for once, though.

【在 d****s 的大作中提到】
:
: Because you don't need to aware "delete" an object,
: you'd better use what you said "直接赋值".
: The system has an automatic gabarge colletion mechanism.
: You can't delete an object. If an object is still referenced,
: it won't be deleted by the system as well.
: Sometimes you have to use Copy. But most time just use reference.

m******t
发帖数: 2416
5

On a side note,
I'm not even sure it's a good idea to make it a general rule in C++,
either, because of complications similar to what I mentioned.
What I used to do in C++ is to define clearly in the API contract
who owns whom at which point. E.g., the setter would clearly document
that it would not make a copy of the object passed in, and would
take over the lifecycle management of the object, and the caller
shall forfeit all rights to the object thereafter and shall not
hold the setter liable

【在 T*****e 的大作中提到】
: 比方说,如果class A中有一个object B,
: 在set/get方法中,是直接赋值呢,还是先拷贝?
: 直接赋值:
: B getB(){ return b; }
: void setB( B b ){ this.b = b ; }
: 拷贝:
: B getB(){ return new B(b); }
: void setB( B b ){ this.b = new B(b) ; }
: 当年用c++编程的时候,除了container class之外,
: 都是用拷贝,这样可以防止引用对象/数据在别处被误删。

d****s
发帖数: 30
6

Don't spend time on arguing with me. Spend time on learning ANT JAR
task! Next time somebody asks "What doesn manifest mean in Export or create
a jar", don't answer "sorry, I don't know. I always use ANT to create jars"...

【在 m******t 的大作中提到】
:
: On a side note,
: I'm not even sure it's a good idea to make it a general rule in C++,
: either, because of complications similar to what I mentioned.
: What I used to do in C++ is to define clearly in the API contract
: who owns whom at which point. E.g., the setter would clearly document
: that it would not make a copy of the object passed in, and would
: take over the lifecycle management of the object, and the caller
: shall forfeit all rights to the object thereafter and shall not
: hold the setter liable

m******t
发帖数: 2416
7

jars"...
Go back and read the OP, see if you rephrased exactly what was said by
both parties.
There is a difference between what *you think* other people are
saying and what other people are really saying, you know.
I only asked "what 'export'?", because honestly I have never used that
feature in Eclipse. In fact, I didn't even know it was there until
the OP relied.
But then, you feel free to jump to whatever conclusion you wish, my friend.

【在 d****s 的大作中提到】
:
: Don't spend time on arguing with me. Spend time on learning ANT JAR
: task! Next time somebody asks "What doesn manifest mean in Export or create
: a jar", don't answer "sorry, I don't know. I always use ANT to create jars"...

T*****e
发帖数: 361
8
同志们,别太较真,大家都友好一点。
争论归争论,观点不同也没有什么哪。
有时候知道别人有不同想法也不错啊。
呵呵,我从你们的争论中学了不少东西。
好些时候都是一边看你们的帖子,一边google一些文档看看。

create

【在 m******t 的大作中提到】
:
: jars"...
: Go back and read the OP, see if you rephrased exactly what was said by
: both parties.
: There is a difference between what *you think* other people are
: saying and what other people are really saying, you know.
: I only asked "what 'export'?", because honestly I have never used that
: feature in Eclipse. In fact, I didn't even know it was there until
: the OP relied.
: But then, you feel free to jump to whatever conclusion you wish, my friend.

T*****e
发帖数: 361
9
Yes, you guys are right. I am going for direct assignment.

【在 m******t 的大作中提到】
:
: jars"...
: Go back and read the OP, see if you rephrased exactly what was said by
: both parties.
: There is a difference between what *you think* other people are
: saying and what other people are really saying, you know.
: I only asked "what 'export'?", because honestly I have never used that
: feature in Eclipse. In fact, I didn't even know it was there until
: the OP relied.
: But then, you feel free to jump to whatever conclusion you wish, my friend.

T*****e
发帖数: 361
10
当年用c++写的是一个比较复杂的系统,用完全封装的办法可以保证数据的一致性。
当时只要的问题是,如果一个对象被修改,那么与其相关联的对象或者属性也需要
做出调整。比方说,一条线段上的任何一个坐标发生变化,都可能导致整个图层
的拓扑重构。
当然了,如果api contract非常清晰而且每个程序员都非常清楚所有的tricks,
直接引用当然好,至少速度上能够快一些。当时没有这么做,主要是风险问题。

【在 m******t 的大作中提到】
:
: jars"...
: Go back and read the OP, see if you rephrased exactly what was said by
: both parties.
: There is a difference between what *you think* other people are
: saying and what other people are really saying, you know.
: I only asked "what 'export'?", because honestly I have never used that
: feature in Eclipse. In fact, I didn't even know it was there until
: the OP relied.
: But then, you feel free to jump to whatever conclusion you wish, my friend.

g*****g
发帖数: 34805
11
Deep copy doesn't make you any safer. How do you ensure
consistency of different copies?

【在 T*****e 的大作中提到】
: 当年用c++写的是一个比较复杂的系统,用完全封装的办法可以保证数据的一致性。
: 当时只要的问题是,如果一个对象被修改,那么与其相关联的对象或者属性也需要
: 做出调整。比方说,一条线段上的任何一个坐标发生变化,都可能导致整个图层
: 的拓扑重构。
: 当然了,如果api contract非常清晰而且每个程序员都非常清楚所有的tricks,
: 直接引用当然好,至少速度上能够快一些。当时没有这么做,主要是风险问题。

T*****e
发帖数: 361
12
If every object takes good care of its own stuff, then
there is no consistency problem at all. For example:
class A {
B b;
public void setB(B b){
this.b = new B(b);
//do something to ensure self-consistency:
...
}
public B getB(){ return new B(b);}
}
When you call a.getB(), what you get is a copy of a.b.
You can do whatever you want to do to the copy, it won't
bring any trouble to a.
If you want to update the original copy, say a.b, you
call a.setB( b_copy ) and object a will

【在 g*****g 的大作中提到】
: Deep copy doesn't make you any safer. How do you ensure
: consistency of different copies?

1 (共1页)
进入Java版参与讨论
相关主题
Re: [转载] Re: .jar文件是怎么产生的?传递一个object reference,如何防止更改object?
Jar question类里的字符串向量赋值,总报错:
Open Jar如何读懂Java程序
应该做成哪类包?java为啥这么多framework
[转载] 有用Eclipse的嘛?重新拣起C++....
Ant and Netbeans helpdeserializer probelm.Ignore upper. Thx
请教:Junit fails as an Ant taskHelp
新手问一个:如何找.jar文件里都有什么class?Simple question
相关话题的讨论汇总
话题: object话题: copy话题: what话题: 赋值话题: getb