Home > Database > Mysql Tutorial > 为何要通过公有接口来访问私有成员(2)

为何要通过公有接口来访问私有成员(2)

WBOY
Release: 2016-06-07 15:15:00
Original
1255 people have browsed it

一般私有成员,对象不能直接访问,只能通过类中设置的接口函数来访问,那么,为什么要有这种设置呢?非常的简单,为了安全,看下面代码就明白了 #include iostreamusing namespace std;class Human{public: void set(int w); int print();private: int weigh

一般私有成员,对象不能直接访问,只能通过类中设置的接口函数来访问,那么,为什么要有这种设置呢?非常的简单,为了安全,看下面代码就明白了

#include <iostream>
using namespace std;
class Human
{
public:
    void set(int w);
    int print();
private:
    int weight;
};

int main()
{
    Human Tom;
    Tom.set(111);
    cout0&&w
<p>代码很清晰啊,这样通过接口访问的话,会安全,比如设置数据的时候,也可咋print接口中设置一下取的条件,这样程序的安全性会非常的好.</p>


</iostream>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template