zoukankan      html  css  js  c++  java
  • Java 内部类种类及使用解析

    package com.learnjava.innerclass;
    
    class MemberInner
    {
        private int d = 1;
        private int a = 2;
    
        // 定义一个成员内部类
        public class Inner2
        {
            private int a = 8;
    
            public void doSomething()
            {
                // 直接访问外部类对象
                System.out.println(d);
                System.out.println(a);// 直接访问a,则访问的是内部类里的a
    
                // 如何访问到外部类里的a呢?  //InnerClass.this.*也是可以访问方法
                System.out.println(MemberInner.this.a);
            }
    
        }
    
    }
    //

    public class InnerClass {
    public String a;
    public void test(){
    Student student = new Student();
    student.goToSchool();
    }
    public void test1(){
    //方法内部类
    class ClassRoom{
    public void sendMail(){
    System.out.println(a);
    }
    }
    }
    public class Student{
    public void goToSchool(){
    System.out.println("111");
    }
    }
    public static void main(String[] args) {
    InnerClass clazz = new InnerClass();
    clazz.test();
    //成员内部类
    Student student = clazz.new Student();
    student.goToSchool();
    //方法内部类

    }

    }




  • 相关阅读:
    编译使用tinyxml
    GitLab 项目创建后地址由Localhost改为实际IP的方法
    树莓派相机设定
    MongoDB的数据备份与恢复
    Nginx PHP fpm forbidden 原因
    PSR2规范
    docker 日志管理
    Docker 拷贝文件
    Docker MySQL基本操作
    deepin安装php5.6
  • 原文地址:https://www.cnblogs.com/zxf330301/p/6025455.html
Copyright © 2011-2022 走看看