private什么意思

如题所述

第1个回答  2022-10-23
分类: 电脑/网络 >> 程序设计 >> 其他编程语言
问题描述:

private什么意思

解析:

private 是C++的关键字,表示私有成员。

private 是类中的一个属性 用它定义的feild和method只能在类中被调用.如果定义了private,就不能被外部类所访问了

比如说你在一个类中有这样的定义:

class test{

private int n;

private int m;

}

然后你想在另一个地方调用n和m.

class text{

public static void main(String [] arg){

test x=new test();

int s=x.n;

}

}

就会产生错误.

你可以在定义test类的时候加上一定的方法来返回n和m的值.

public int getn(){return n;}

public int getm(){return m;}

然后在调用的时候

s=x.getn();

就可以了!
相似回答