zoukankan      html  css  js  c++  java
  • 面向对象(内部类访问规则)

    /**
    * Created by rabbit on 2014-07-30.刘朋程.博客园
    * 内部类:将一个类定义在另一个类的里面,对里面的那个类
    * 就称为内部类(内置类,嵌套类)
    *
    *内部类的访问规则:
    * 1、内部类可以直接访问外部类中的成员,包括私有。
    * 子所以可以直接访问外部类中的成员,是因为内部类中持有了
    * 一个外部类的引用。outer.this.x
    * 2、外部类要访问内部类,必须建立内部类对象。
    *
    */
    //Created by rabbit on 2014-07-30.刘朋程.博客园
    class outer
    {
        private int x = 3;
        class inner //内部类
        {
            int x = 4;
            void function()
            {
                int x = 5;
                System.out.println("inner "+ x + "内部类局部变量"); //结果为5 内部类局部变量
                System.out.println("inner "+ this.x + "内部类变量"); //结果是4,内部类变量
                System.out.println("inner "+ outer.this.x + "外部类变量"); //结果为3 外部类变量
            }

        }
        void method()
        {
            inner in = new inner();
            in.function();
        }
    }
    //Created by rabbit on 2014-07-30.刘朋程.博客园
    public class innerclassDemo {
        public static void main(String [] args)
        {
            //1、第一种访问方式
            outer out = new outer();
            out.method();

            //2、直接访问内部类的成员
            outer.inner in = new outer().new inner();
            in.function();
        }

    }
    //Created by rabbit on 2014-07-30.刘朋程.博客园

  • 相关阅读:
    4.结构化机器学习项目
    九、LaTex中的浮动体
    第二章 决策树
    菜鸟教程C++(一)
    12月月计划与周计划
    目标检测常见错误
    Tensorflow object detection API(1)---环境搭建与测试
    爬取今日头条街拍美图
    BS4库详解
    requests库详解
  • 原文地址:https://www.cnblogs.com/liupengcheng/p/3889482.html
Copyright © 2011-2022 走看看