C++中public、protected及private用法(From MSDN),protectedmsdn

來源:互聯網
上載者:User

C++中public、protected及private用法(From MSDN),protectedmsdn
public(C# 參考): https://msdn.microsoft.com/zh-cn/library/yzh058ae.aspx
protected(C# 參考):https://msdn.microsoft.com/zh-cn/library/bcd5672a.aspx
private(C# 參考):https://msdn.microsoft.com/zh-cn/library/st6sy9xe.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;    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.