public(C# 參考): https://msdn.microsoft.com/zh-cn/library/yzh058ae.aspx


protected(C# 參考):https://msdn.microsoft.com/zh-cn/library/bcd5672a.aspx




class PointTest
{
public int x;
public int y;
} class MainClass4
{
static void Main()
{
PointTest p = new PointTest();
// Direct access to public members:
p.x = 10;
p.y = 15;
Console.WriteLine("x = {0}, y = {1}", p.x, p.y);
}
}
// Output: x = 10, y = 15


   class Point
{
protected int x;
protected int y;
} class DerivedPoint: Point
{
static void Main()
{
DerivedPoint dpoint = new DerivedPoint(); // Direct access to protected members:
dpoint.x = 10;
dpoint.y = 15;
Console.WriteLine("x = {0}, y = {1}", dpoint.x, dpoint.y);
}
}
// Output: x = 10, y = 15
class Employee2
{
private string name = "FirstName, LastName";
private double salary = 100.0; public string GetName()
{
return name;
} public double Salary
{
get { return salary; }
}
} class PrivateTest
{
static void Main()
{
Employee2 e = new Employee2(); // The data members are inaccessible (private), so
// they can't be accessed like this:
// string n = e.name;
// double s = e.salary; // 'name' is indirectly accessed via method:
string n = e.GetName(); // 'salary' is indirectly accessed via property
double s = e.Salary;
}
}

class Employee2
{
private string name = "FirstName, LastName";
private double salary = 100.0; public string GetName()
{
return name;
} public double Salary
{
get { return salary; }
}
} class PrivateTest
{
static void Main()
{
Employee2 e = new Employee2(); // The data members are inaccessible (private), so
// they can't be accessed like this:
// string n = e.name;
// double s = e.salary; // 'name' is indirectly accessed via method:
string n = e.GetName(); // 'salary' is indirectly accessed via property
double s = e.Salary;
}
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

C++于public、protected和private说明(From MSDN)的更多相关文章

  1. public,protected,friendly,private的访问权限

    请说出作用域public,private,protected,以及不写时的区别 这四个作用域的可见范围如下表所示. 说明:如果在修饰的元素上面没有写任何访问修饰符,则表示friendly. 作用域   ...

  2. java 修饰符的作用一(public protected default private 组)

    1.public protected default private 组 public 权限最大,同类,同包,不同包,同包子类父类之间,不同包子类父类之间都可以访问. java 默认的权限是defau ...

  3. java四种权限修饰符(public > protected > (default) > private)

    权限修饰符在哪里可以访问 (default) : 表示什么权限修饰符都不写 位置 public protected (default) private 同一个类 yes yes yes yes 同一个 ...

  4. Java访问权限修饰符public protected friendly private用法总结(转载好文Mark)

    首先声明:Java中,friendly这个修饰符并没有显式的声明,在成员变量和方法前什么修饰符也不用,默认的就是friendly.为了条理清晰,分三种不同情况来总结. 一 访问权限修饰符修饰成员变量和 ...

  5. java中的 public protected friendly private

    1.public:public表明该数据成员.成员函数是对所有用户开放的,所有用户都可以直接进行调用 2.private:private表示私有,私有的意思就是除了class自己之外,任何人都不可以直 ...

  6. Java学习笔记(4)----Public,Protected,Package,Private修饰符可见性

    Java修饰符类型(public,protected,private,friendly) public的类.类属变量及方法,包内及包外的任何类均可以访问:protected的类.类属变量及方法,包内的 ...

  7. 【java基础】(1)Java的权限修饰符(public,protected,default,private)

    访问权限修饰符权限从高到低排列是public  ,protected  ,default, private. 一.根据“是否是同包”.“是否是子类”分为4中情况+本类 5种情况 二.把 同包中的子类 ...

  8. 详解Java中的访问控制修饰符(public, protected, default, private)

    Java中的访问控制修饰符已经困惑笔者多时,其中较复杂的情况一直不能理解透彻.今天下定决心,系统.全面地研究Java中的访问控制修饰符的所有方面,并整理成这篇文章,希望有同样疑惑的读者读完后能有所收获 ...

  9. java中public protected friendly private作用域

    1.public:public表明该数据成员.成员函数是对所有用户开放的,所有用户都可以直接进行调用 2.private:private表示私有,私有的意思就是除了class自己之外,任何人都不可以直 ...

  10. public, protected and private inheritance in C++

    Get from Stackoverflow. The details can easily understand from the below example. class A { public: ...

随机推荐

  1. SQL日期时间函数

    一.Sql Server中的日期与时间函数 1. 当前系统日期.时间  select getdate() 2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值  例如 ...

  2. arcengine,深入理解游标Cursors,实现数据的快速查找,插入,删除,更新

    风过无痕 原文  arcengine,深入理解游标Cursors,实现数据的快速查找,插入,删除,更新 深入理解游标Cursors,实现数据的快速查找,插入,删除,更新 1.查找数据Search Cu ...

  3. [Angular] Bind async requests in your Angular template with the async pipe and the "as" keyword

    Angular allows us to conveniently use the async pipe to automatically register to RxJS observables a ...

  4. JVM调优基础 分类: B1_JAVA 2015-03-14 09:33 250人阅读 评论(0) 收藏

    一.JVM调优基本流程 1.划分应用程序的系统需求优先级 2.选择JVM部署模式:单JVM.多JVM 3.选择JVM运行模式 4.调优应用程序内存使用 5.调优应用程序延迟 6.调优应用程序吞吐量 二 ...

  5. crx 【 集合 】

    Vimium dbepggeogbaibhgnhhndojpepiihcmeb-1.64-Crx4Chrome.com.crx https://www.crx4chrome.com/down/731/ ...

  6. JS生成一个种子随机数(伪随机数)

    原文链接:https://geniuspeng.github.io/2016/09/12/js-random/ 最近有一个需求,需要生成一个随机数,但是又不能完全随机,就是说需要一个种子seed,se ...

  7. 手机浏览器 input 输入框 数字

    其实很简单了啦 type="tel"就行了呢 如果是type="number"其实不好用

  8. 【34.88%】【codeforces 569C】Primes or Palindromes?

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. HDOJ 2043 password

    刚開始看到这个题目的时候,就直接理解成仅仅要是长度符合要求而且字符符合要求,就是一个安全的password了,并没有考虑到至少要3种字符的组合.然后就直接写程序了(先暂且觉得题目就是那个意思),在測试 ...

  10. 结构体(struct)与类(class)

    在 C++ 的范畴里,除了 struct 结构体不支持访问权限修饰符(private.protected.public)外,已不再区分二者的其他方面语法上的差异. 因此,在 C++ 编程中,如果一个类 ...