class Test
{
private int c;
int setc(int i)
{c=i;
return c;
}
/*int getc()
{
return c;
}*/
}
class GetSetEx
{public static void main(String args[])
{Test ob=new Test();//Test class object instantiation
System.out.println(ob.setc(100));//accessing c using setter
}
}
//Output: 100
我无需使用getter方法就可以访问变量c的值通过只在setter方法本身中返回]
是的,您可以这样做,但这是不好的编程习惯。良好的编程惯例是为所有私有成员生成吸气剂/设置剂。您编写的代码很难在具有数百行代码的大规模项目中进行调试。