前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >面向对象中的内部类

面向对象中的内部类

作者头像
星哥玩云
发布2022-09-08 13:33:36
9750
发布2022-09-08 13:33:36
举报
文章被收录于专栏:开源部署

1、成员内部类

将一个类A定义在另一个类B里面,里面的那个类A就称为内部类,B则称为外部类。

1.1、定义格式

代码语言:javascript
复制
<span class="hljs-class"><span class="hljs-keyword">class</span> 外部类</span>{
    <span class="hljs-class"><span class="hljs-keyword">class</span> 内部类</span>{
    }
}

在描述事物时,若一个事物内部还包含其他事物,就可以使用内部类这种结构。比如,电脑Computer类中包含鼠标 Mouse类 ,这时, Computer 就可以使用内部类来描述,定义在成员位置。 如:

代码语言:javascript
复制
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Computer</span></span>{<span class="hljs-comment">//外部类</span>
  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Mouse</span></span>{<span class="hljs-comment">//内部类</span>
    
  }
}

1.2、访问

1.2.1、创建内部类对象

外部类名.内部类名 对象名 = new 外部类型().new 内部类型();

外部类名.内部类名 对象名 = 外部类对象.new 内部类型();

1.2.2、访问特点

内部类可以直接访问外部类的成员,包括私有成员。

外部类要访问内部类的成员,必须要建立内部类的对象。

代码语言:javascript
复制
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Computer</span></span>{<span class="hljs-comment">//外部类</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> kj=<span class="hljs-keyword">false</span>;
    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Mouse</span></span>{<span class="hljs-comment">//内部类</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">start</span><span class="hljs-params">()</span></span>{
            <span class="hljs-keyword">if</span>(kj){
                System.out.println(<span class="hljs-string">"鼠标可移动"</span>);
            }<span class="hljs-keyword">else</span>{
                System.out.println(<span class="hljs-string">"鼠标没有反应"</span>);
            }
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">isKj</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> kj;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setKj</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> kj)</span> </span>{
        <span class="hljs-keyword">this</span>.kj = kj;
    }
}
代码语言:javascript
复制
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ComputerDemo</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-comment">//外部类对象</span>
        Computer c=<span class="hljs-keyword">new</span> Computer();
        <span class="hljs-comment">//内部类对象</span>
        Computer.Mouse m=c.new Mouse();
        <span class="hljs-comment">//调用内部类方法</span>
        m.start();
        <span class="hljs-comment">//修改外部类方法</span>
        c.setKj(<span class="hljs-keyword">true</span>);
        <span class="hljs-comment">//调用内部类方法</span>
        m.start();
    }
}

运行结果:

2、局部内部类

局部内部类是定义在一个方法或者一个作用域里面的类,它和成员内部类的区别在于局部内部类的访问仅限于方法内或者该作用域内。

2.1、定义格式

代码语言:javascript
复制
<span class="hljs-class"><span class="hljs-keyword">class</span> 外部类</span>{
   方法(){
     <span class="hljs-class"><span class="hljs-keyword">class</span> 内部类</span>{}
   }
}

2.2、应用

代码语言:javascript
复制
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">People</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">People</span><span class="hljs-params">()</span> </span>{
         
    }
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Man</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Man</span><span class="hljs-params">()</span></span>{
         
    }
    <span class="hljs-function"><span class="hljs-keyword">public</span> People <span class="hljs-title">getWoman</span><span class="hljs-params">()</span></span>{
        <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Woman</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">People</span></span>{   <span class="hljs-comment">//局部内部类</span>
            <span class="hljs-keyword">int</span> age =<span class="hljs-number">0</span>;
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Woman();<span class="hljs-comment">//返回局部内部类对象</span>
    }
}

局部内部类就像是方法里面的一个局部变量一样,是不能有public、protected、private以及static修饰符的。

3、静态内部类

静态内部类也是定义在另一个类里面的类,只不过在类的前面多了一个关键字static。静态内部类是不需要依赖于外部类的,这点和类的静态成员属性有点类似,并且它不能使用外部类的非static成员变量或者方法,这点很好理解,因为在没有外部类的对象的情况下,可以创建静态内部类的对象,如果允许访问外部类的非static成员就会产生矛盾,因为外部类的非static成员必须依附于具体的对象。

3.1、定义格式

代码语言:javascript
复制
<span class="hljs-class"><span class="hljs-keyword">class</span> 外部类</span>{
  <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> 内部类</span>{
  
  }
}

3.2、应用

3.2.1、创建内部类对象

外部类名.内部类名 对象名 = new 外部类名.内部类名();

3.2.2、案例
代码语言:javascript
复制
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span>  </span>{
       <span class="hljs-comment">//产生静态内部类对象</span>
        Outter.Inner inner = <span class="hljs-keyword">new</span> Outter.Inner();
    }
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Outter</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Outter</span><span class="hljs-params">()</span> </span>{
         
    }
    <span class="hljs-comment">//静态内部类</span>
    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Inner</span> </span>{
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Inner</span><span class="hljs-params">()</span> </span>{
             
        }
    }
}

4、匿名内部类

是内部类的简化写法。它的本质是一个带具体实现的父类或者父接口的匿名的子类对象。是最常用的内部类方式。

4.1、定义格式

匿名内部类必须继承一个父类或者实现一个父接口。

匿名内部类有两个步骤:

a.临时定义一个类型的子类

b.定义后即刻创建刚刚定义的这个类的对象

代码语言:javascript
复制
<span class="hljs-keyword">new</span> 父类名或者接口名(){ 
<span class="hljs-comment">// 方法重写 </span>
<span class="hljs-meta">@Override</span> 
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">method</span><span class="hljs-params">()</span> </span>{
<span class="hljs-comment">// 执行语句 </span>
} 
};

4.2、应用

4.2.1、使用方式
代码语言:javascript
复制
<span class="hljs-comment">//已经存在的父类:</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span></span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">eat</span><span class="hljs-params">()</span></span>;
}
<span class="hljs-comment">//定义并创建该父类的子类对象,并用多态的方式赋值给父类引用变量</span>
Person  p = <span class="hljs-keyword">new</span> Person(){
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">eat</span><span class="hljs-params">()</span> </span>{
      System.out.println(<span class="hljs-string">"我吃了"</span>);
    }
};
<span class="hljs-comment">//调用eat方法</span>
p.eat();

<span class="hljs-comment">//将定义子类与创建子类对象两个步骤由一个格式一次完成</span>
<span class="hljs-keyword">new</span> Person(){
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">eat</span><span class="hljs-params">()</span> </span>{
      System.out.println(<span class="hljs-string">"我吃了"</span>);
    }
}.eat();
4.2.2、案例
代码语言:javascript
复制
<span class="hljs-comment">//接口</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">Fly</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">open</span><span class="hljs-params">()</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">fly</span><span class="hljs-params">()</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">void</span> <span class="hljs-title">close</span><span class="hljs-params">()</span></span>;
}
<span class="hljs-comment">//实现接口</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">YanZi</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Fly</span></span>{
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">open</span><span class="hljs-params">()</span> </span>{
        System.out.println(<span class="hljs-string">"张开小翅膀"</span>);
    }
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">fly</span><span class="hljs-params">()</span> </span>{
        System.out.println(<span class="hljs-string">"能飞3000米高空"</span>);
    }
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">close</span><span class="hljs-params">()</span> </span>{
        System.out.println(<span class="hljs-string">"关闭小翅膀,安全着陆"</span>);
    }
}

匿名内部类完成

代码语言:javascript
复制
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FlyDemo</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        Fly yz = <span class="hljs-keyword">new</span> YanZi();
        yz.open();
        yz.fly();
        yz.close();
        <span class="hljs-keyword">new</span> YanZi().open();
        System.out.println(<span class="hljs-string">"-------------------"</span>);
        <span class="hljs-comment">//实现类对象赋值给父接口</span>
        Fly fj = <span class="hljs-keyword">new</span> Fly(){
            <span class="hljs-meta">@Override</span>
            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">open</span><span class="hljs-params">()</span> </span>{
                System.out.println(<span class="hljs-string">"不需要张开翅膀,一直都是张开状态"</span>);
            }
            <span class="hljs-meta">@Override</span>
            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">fly</span><span class="hljs-params">()</span> </span>{
                System.out.println(<span class="hljs-string">"喷气式助力飞行!"</span>);
            }
        <span class="hljs-meta">@Override</span>
            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">close</span><span class="hljs-params">()</span> </span>{
                System.out.println(<span class="hljs-string">"不需要关闭翅膀,得哪撞哪"</span>);
            }
        };
        fj.open();
        fj.fly();
        fj.close();
        System.out.println(<span class="hljs-string">"-----------------------------"</span>);
        <span class="hljs-comment">//实现类对象直接以匿名对象的方式调用方法</span>
        <span class="hljs-keyword">new</span> Fly() {
            <span class="hljs-meta">@Override</span>
            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">open</span><span class="hljs-params">()</span> </span>{
                System.out.println(<span class="hljs-string">"小翅膀"</span>);

            }
            <span class="hljs-meta">@Override</span>
            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">fly</span><span class="hljs-params">()</span> </span>{
                System.out.println(<span class="hljs-string">"乱飞"</span>);
            }
            <span class="hljs-meta">@Override</span>
            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">close</span><span class="hljs-params">()</span> </span>{
                System.out.println(<span class="hljs-string">"各种撞树"</span>);
            }
        }.fly();
    }
}

运行结果:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、成员内部类
    • 1.1、定义格式
      • 1.2、访问
        • 1.2.1、创建内部类对象
        • 1.2.2、访问特点
    • 2、局部内部类
      • 2.1、定义格式
        • 2.2、应用
        • 3、静态内部类
          • 3.1、定义格式
            • 3.2、应用
              • 3.2.1、创建内部类对象
              • 3.2.2、案例
          • 4、匿名内部类
            • 4.1、定义格式
              • 4.2、应用
                • 4.2.1、使用方式
                • 4.2.2、案例
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档