由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 请教: class private data member
相关主题
这几年Java跟其他语言的差距拉大了。Apply lock on a class.
怎样才能使一个算法用于不同的数据结构?问一个关于access object instance的问题
Why Java needs clone interface?Is there a way to pass by value in Java?
copy constructor都什么时候be called啊git clones only one branch?
A question about inheritancethe best way to transfer data?
发现 synchronized 的一个问题其实有很多概念都是误导性的
array 在java里 是一定放在heap 吗?Dependency Injection
Re: Java Vs. C++ on performance!?一道初级JAVA题求帮助
相关话题的讨论汇总
话题: private话题: test话题: class话题: const话题: rhs
进入Java版参与讨论
1 (共1页)
g******d
发帖数: 48
1
有一个疑问,比如下面的代码:
class A{
public:
A();
A(const A& rhs){ ia = rhs.a; }//rhs is reference
private:
int a;
};
问题是,既然a 是私有的成员,但是为什么在定义copy constructor时,rhs能够直接引
用呢(即rhs.a)?
C********g
发帖数: 1548
2
问错地方了?java不用const,用final
g******d
发帖数: 48
3
不好意思,是C++的问题。希望高人指点一下。

【在 C********g 的大作中提到】
: 问错地方了?java不用const,用final
O**e
发帖数: 130
4
道理是一样的。private这样的访问控制符是就类而言的,跟哪个对象无关。

【在 g******d 的大作中提到】
: 不好意思,是C++的问题。希望高人指点一下。
q*********u
发帖数: 280
5
我来一个具体的好了,看了一下programming和这里的,发现自己懂和表达清楚完全两
码事起。其中有一个老兄凶神恶煞的,看了都一怔。
#include
using namespace std;
class T
{
public:
T(){a = 11;}
int Get() const{return a;}
private:
int a;
};
class Test
{
public:
Test(int a):m_a(a){}
Test(const Test &t){m_a = t.m_a;} //拷贝构造函数,通过同类型的引用参数访问
私有变量
Test& operator=(const Test &t) //赋值操作函数,通过同类型的引用参数访问私
有变量
{
m_a = t.m_a;
}
//Test(const T &t){m_a = t.a;} //此语句出错,不能访问T类型的私有成员
Test(const T &t){m_a = t.Get();} //这个实现可以
void Print() const
{
cout<<"m_a:

【在 O**e 的大作中提到】
: 道理是一样的。private这样的访问控制符是就类而言的,跟哪个对象无关。
g******d
发帖数: 48
6
谢谢mahu! 读了好几遍,总算弄懂了!
原来我老是觉得,类外如果要get access to private member,就要有一个接口,比如用T.get_value()什么的,不能直接T.a。
十分感谢!!

【在 q*********u 的大作中提到】
: 我来一个具体的好了,看了一下programming和这里的,发现自己懂和表达清楚完全两
: 码事起。其中有一个老兄凶神恶煞的,看了都一怔。
: #include
: using namespace std;
: class T
: {
: public:
: T(){a = 11;}
: int Get() const{return a;}
: private:

O**e
发帖数: 130
7
这个不是“方便”,而是必须。如果访问同类型其它对象的私有成员也只能通过公有权
限的方法,那就意味着你必须写一个公有权限的方法,而本来需要私有化藏起来的成员
就被这个公有权限的办法给暴露了。

【在 q*********u 的大作中提到】
: 我来一个具体的好了,看了一下programming和这里的,发现自己懂和表达清楚完全两
: 码事起。其中有一个老兄凶神恶煞的,看了都一怔。
: #include
: using namespace std;
: class T
: {
: public:
: T(){a = 11;}
: int Get() const{return a;}
: private:

q*********u
发帖数: 280
8
对的

这个不是“方便”,而是必须。如果访问同类型其它对象的私有成员也只能通过公有权
限的方法,那就意味着你必须写一个公有权限的方法,而本来需要私有化藏起来的成员
就被这个公有权限的办法给暴露了。

【在 O**e 的大作中提到】
: 这个不是“方便”,而是必须。如果访问同类型其它对象的私有成员也只能通过公有权
: 限的方法,那就意味着你必须写一个公有权限的方法,而本来需要私有化藏起来的成员
: 就被这个公有权限的办法给暴露了。

l*********s
发帖数: 5409
9
If it has to be exposed at some time point, maybe it shall not classified as
private.

【在 O**e 的大作中提到】
: 这个不是“方便”,而是必须。如果访问同类型其它对象的私有成员也只能通过公有权
: 限的方法,那就意味着你必须写一个公有权限的方法,而本来需要私有化藏起来的成员
: 就被这个公有权限的办法给暴露了。

O**e
发帖数: 130
10
man, did you ever read the original post...

as

【在 l*********s 的大作中提到】
: If it has to be exposed at some time point, maybe it shall not classified as
: private.

相关主题
发现 synchronized 的一个问题Apply lock on a class.
array 在java里 是一定放在heap 吗?问一个关于access object instance的问题
Re: Java Vs. C++ on performance!?Is there a way to pass by value in Java?
进入Java版参与讨论
l*********s
发帖数: 5409
11
I did; but you sounded like this point is foolproof which I beg to differ.
For analogy, you and I are both human beings, but I don't and
shall not have reading access to your under-ware. Lz's original intuition is
understandable.

【在 O**e 的大作中提到】
: man, did you ever read the original post...
:
: as

h*****0
发帖数: 4889
12
you analogy is not right. the relation between class and instances, are
mostly different from between human and individuals. basically, instances
are all cloned from the same template, and they all have the same behavior.

is

【在 l*********s 的大作中提到】
: I did; but you sounded like this point is foolproof which I beg to differ.
: For analogy, you and I are both human beings, but I don't and
: shall not have reading access to your under-ware. Lz's original intuition is
: understandable.

s******n
发帖数: 876
13
while the programmer is the god, he decides what is or is not visible from
one human to another. if he doesn't want "this" reading another's underwear,
he simply doesn't code such reads in the methods. he can prevent other gods
from seeing his people's underwears by declaring "private".

is

【在 l*********s 的大作中提到】
: I did; but you sounded like this point is foolproof which I beg to differ.
: For analogy, you and I are both human beings, but I don't and
: shall not have reading access to your under-ware. Lz's original intuition is
: understandable.

l*********s
发帖数: 5409
14
well,it is OK with me that access control is implemented on the classes,
instead on the instances.
Nonetheless the latter is a desirable goal for the sake of encapsulation.
Say there is a Connection class to represent internet traffic, would you
want my instance of Connection to have full access to the private data in your
instance?

【在 h*****0 的大作中提到】
: you analogy is not right. the relation between class and instances, are
: mostly different from between human and individuals. basically, instances
: are all cloned from the same template, and they all have the same behavior.
:
: is

h*****0
发帖数: 4889
15
sounds like you would let a internet connection to have full access to all
your public method...

your

【在 l*********s 的大作中提到】
: well,it is OK with me that access control is implemented on the classes,
: instead on the instances.
: Nonetheless the latter is a desirable goal for the sake of encapsulation.
: Say there is a Connection class to represent internet traffic, would you
: want my instance of Connection to have full access to the private data in your
: instance?

1 (共1页)
进入Java版参与讨论
相关主题
一道初级JAVA题求帮助A question about inheritance
这道题该走什么路发现 synchronized 的一个问题
Talk a little more about How to lock a filearray 在java里 是一定放在heap 吗?
Re: Entity EJB: anyone with real experienceRe: Java Vs. C++ on performance!?
这几年Java跟其他语言的差距拉大了。Apply lock on a class.
怎样才能使一个算法用于不同的数据结构?问一个关于access object instance的问题
Why Java needs clone interface?Is there a way to pass by value in Java?
copy constructor都什么时候be called啊git clones only one branch?
相关话题的讨论汇总
话题: private话题: test话题: class话题: const话题: rhs