zoukankan      html  css  js  c++  java
  • public,protected,private

    #include <iostream>
    using namespace std;
    class Animal
    {
    public:
        void eat()
        {
            cout<<"animal eat"<<endl;
        }
    protected:
        void sleep()
        {
            cout<<"animal sleep"<<endl;
        }
    private:
        void breathe()
        {
            cout<<"animal beathe"<<endl;
        }
    };
    class Fish:public Animal
    {
    public:
        void test()
        {
            sleep();//可以调用
        }
        void test1()
        {
            eat();//可以调用
        }
        void test3()
        {
            breathe();//不可调用
        }
    };
    
    class Dog:private Animal
    {
    public:
        void testPublic()
        {
            sleep();
        }
    protected:
        void testProtected()
        {
            sleep();
        }
    };
    class Cat:protected Animal
    {
    
    };
    
    
    void main()
    {
        Animal an;
        an.eat();
        an.sleep();//不可调用
        an.breathe();//不可调用
        Fish fh;
        fh.test();//可以调用
        fh.test1();//可以调用
    
        Dog dg;
        dg.eat();//无法访问
        dg.testPublic();//可以访问
        Cat cat;
        cat.eat();//无法访问
    
    }

    private                  自己可以访问
    
    protected                自己和派生类可以访问
    
    public                   谁都能访问
  • 相关阅读:
    SpringAOP-基于@AspectJ的简单入门
    SpringAOP-切面优先级
    Commons_IO_FileUtils的使用
    java_IO_装饰器
    java_IO_3
    java_IO_2
    java_IO_1
    App Inventor
    java学习_5_24
    java学习_5_23
  • 原文地址:https://www.cnblogs.com/shinecox/p/3029305.html
Copyright © 2011-2022 走看看