java中的私有变量与C++结构中的私有变量有什么区别?
< java类中的私有变量与C++结构中的私有变量之间有什么区别?
Java代码示例如下:实现ADT表。 C++示例参见:应用“隐藏实现”
我在网上查了一下,找不到任何与这个话题相关的有用信息
Java示例:
class Table{
private int size;
private int num;//numbers of items are stored in the arrray
private int[] array;
public Table(int size, int[] array){
this.size = size;
this.array = new int[size];
}
public insert(int[] array, int newItem){
if(num<size){
//for loop for adding items into the array;
}
else if(num>=size){
//increment the size and copy the original array to the new array
}
}
}
C++实现隐藏的实例:
struct B{
private:
char j;
float f;
public:
int i;
void func();
};
void::func(){
i = 0;
j = '0';
f = 0.0;
};
int main(){
B b;
b.i = i; // legal
b.j = '1'; // illegal
b.f = 1.0; // illegal now
}
在C++中,我们不能改变私有变量,是因为这些BJ=‘1’;b、 f=1.0;main()函数中有两行,这就是为什么?在java中,我们也不能更改main()中的私有变量
谢谢!
# 1 楼答案
私有变量是在类本身之外无法访问的变量
了解有关访问修饰符here的更多信息
<{C++ +{CD1>}在^ {CD2>}中是相似的,它们只能在Stutt的{{CD3}}中访问。