java外部类_Java里什么叫内部类什么叫外部类

展开全部

对普通类(没有内部类的类)来说,62616964757a686964616fe78988e69d8331333337396234内部类和外部类都与他无关;对有内部类的类来说,它们就是其内部类的外部类,外部类是个相对的说法,其实就是有内部类的类。

所以,要回答这个问题,只需要讲解内部类是什么:

Java中的内部类共分为四种:

静态内部类static inner class (also called nested class)

成员内部类member inner class

局部内部类local inner class

匿名内部类anonymous inner class

静态内部类Static Inner Class

最简单的内部类形式。

类定义时加上static关键字。

不能和外部类有相同的名字。

被编译成一个完全独立的.class文件,名称为OuterClass$InnerClass.class的形式。

只可以访问外部类的静态成员和静态方法,包括了私有的静态成员和方法。

生成静态内部类对象的方式为:

OuterClass.InnerClass inner = new OuterClass.InnerClass();

示例代码:

package com.learnjava.innerclass;

class StaticInner

{

private static int a = 4;

// 静态内部类

public static class Inner

{

public void test()

{

// 静态内部类可以访问外部类的静态成员

// 并且它只能访问静态的

System.out.println(a);

}

}

}

public class StaticInnerClassTest

{

public static void main(String[] args)

{

StaticInner.Inner inner = new StaticInner.Inner();

inner.test();

}

}

成员内部类Member Inner Class

成员内部类也是定义在另一个类中,但是定义时不用static修饰。

成员内部类和静态内部类可以类比为非静态的成员变量和静态的成员变量。

成员内部类就像一个实例变量。

它可以访问它的外部类的所有成员变量和方法,不管是静态的还是非静态的都可以。

在外部类里面创建成员内部类的实例:

this.new Innerclass();

在外部类之外创建内部类的实例:

(new Outerclass()).new Innerclass();

在内部类里访问外部类的成员:

Outerclass.this.member

示例代码:

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呢?

System.out.println(MemberInner.this.a);

}

}

}

public class MemberInnerClassTest

{

public static void main(String[] args)

{

// 创建成员内部类的对象

// 需要先创建外部类的实例

MemberInner.Inner2 inner = new MemberInner().new Inner2();

inner.doSomething();

}

}

局部内部类Local Inner Class

局部内部类定义在方法中,比方法的范围还小。是内部类中最少用到的一种类型。

像局部变量一样,不能被public, protected, private和static修饰。

只能访问方法中定义的final类型的局部变量。

局部内部类在方法中定义,所以只能在方法中使用,即只能在方法当中生成局部内部类的实例并且调用其方法。

示例代码:

package com.learnjava.innerclass;

class LocalInner

{

int a = 1;

public void doSomething()

{

int b = 2;

final int c = 3;

// 定义一个局部内部类

class Inner3

{

public void test()

{

System.out.println("Hello World");

System.out.println(a);

// 不可以访问非final的局部变量

// error: Cannot refer to a non-final variable b inside an inner

// class defined in a different method

// System.out.println(b);

// 可以访问final变量

System.out.println(c);

}

}

// 创建局部内部类的实例并调用方法

new Inner3().test();

}

}

public class LocalInnerClassTest

{

public static void main(String[] args)

{

// 创建外部类对象

LocalInner inner = new LocalInner();

// 调用外部类的方法

inner.doSomething();

}

}

匿名内部类Anonymous Inner Class

匿名内部类就是没有名字的局部内部类,不使用关键字class, extends, implements, 没有构造方法。

匿名内部类隐式地继承了一个父类或者实现了一个接口。

匿名内部类使用得比较多,通常是作为一个方法参数。

生成的.class文件中,匿名类会生成OuterClass$1.class文件,数字根据是第几个匿名类而类推。

示例代码:

package com.learnjava.innerclass;

import java.util.Date;

public class AnonymouseInnerClass

{

@SuppressWarnings("deprecation")

public String getDate(Date date)

{

return date.toLocaleString();

}

public static void main(String[] args)

{

AnonymouseInnerClass test = new AnonymouseInnerClass();

// 打印日期:

String str = test.getDate(new Date());

System.out.println(str);

System.out.println("----------------");

// 使用匿名内部类

String str2 = test.getDate(new Date()

{

});// 使用了花括号,但是不填入内容,执行结果和上面的完全一致

// 生成了一个继承了Date类的子类的对象

System.out.println(str2);

System.out.println("----------------");

// 使用匿名内部类,并且重写父类中的方法

String str3 = test.getDate(new Date()

{

// 重写父类中的方法

@Override

@Deprecated

public String toLocaleString()

{

return "Hello: " + super.toLocaleString();

}

});

System.out.println(str3);

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541465.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《精通Matlab数字图像处理与识别》一6.2 傅立叶变换基础知识

本节书摘来自异步社区《精通Matlab数字图像处理与识别》一书中的第6章&#xff0c;第6.2节&#xff0c;作者 张铮 , 倪红霞 , 苑春苗 , 杨立红&#xff0c;更多章节内容可以访问云栖社区“异步社区”公众号查看 6.2 傅立叶变换基础知识 精通Matlab数字图像处理与识别要理解傅立…

多线程循环输出abcc++_C ++循环| 查找输出程序| 套装5

多线程循环输出abccProgram 1: 程序1&#xff1a; #include <iostream>using namespace std;int main(){int num 15673;int R1 0, R2 0;do {R1 num % 10;R2 R2 * 10 R1;num num / 10;} while (num > 0);cout << R2 << " ";return 0;}Ou…

java oql_深入理解java虚拟机(八):java内存分析工具-MAT和OQL

以下内容翻译自MAT帮助文档。一、Class HistogramClass Histogram shows the classes found in the snapshot, the number of objects for each class, the heap memory consumption of these objects, and the minimum retained size of the objects二、Dominator treeDomina…

《Python数据分析与挖掘实战》一1.2 从餐饮服务到数据挖掘

本节书摘来自华章出版社《Python数据分析与挖掘实战》一书中的第1章&#xff0c;第1.2节&#xff0c;作者 张良均 王路 谭立云 苏剑林&#xff0c;更多章节内容可以访问云栖社区“华章计算机”公众号查看 1.2 从餐饮服务到数据挖掘 企业经营最大的目的就是盈利&#xff0c;而餐…

obj[]与obj._Ruby中带有示例的Array.include?(obj)方法

obj[]与obj.Ruby Array.include&#xff1f;(obj)方法 (Ruby Array.include?(obj) Method) In the previous articles, we have seen how we can check whether two Array instances are identical or not with the help of <> operator, operator, and .eql? method?…

java javah_Java开发网 - 一个javah的问题

Posted by:jerry_xuPosted on:2006-03-13 15:39我在环境变量中已经设置了path为D:\Program Files\Java\jdk1.5.0_06&#xff0c;ClassPath设置为.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;class的路径为&#xff1a;D:\JNItest\bin\jni\Hello.class &#xff0c;但是…

《Python面向对象编程指南》——2.7 __del__()方法

本节书摘来自异步社区《Python面向对象编程指南》一书中的第2章&#xff0c;第2.7节&#xff0c;作者&#xff3b;美&#xff3d;Steven F. Lott&#xff0c; 张心韬 兰亮 译&#xff0c;更多章节内容可以访问云栖社区“异步社区”公众号查看。 2.7 __del__()方法 __del__()方…

NullReferenceException C#中的异常

什么是NullReferenceException&#xff1f; (What is NullReferenceException?) NullReferenceException is an exception and it throws when the code is trying to access a reference that is not referencing to any object. If a reference variable/object is not refe…

java map key 大写转小写_Spring JdbcTemplate 查询出的Map,是如何产生大小写忽略的Key的?(转)...

Java 是区分大小写的&#xff0c;普通的Map例如HashMap如果其中的key"ABC" value"XXX"那么map.get("Abc") 或 map.get("abc")是获取不到值得。但Spring中产生了一个忽略大小写的map使我产生了好奇例如 jdbcTemplate.queryForList(sql)…

《iOS 6核心开发手册(第4版)》——2.11节秘诀:构建星星滑块

本节书摘来自异步社区《iOS 6核心开发手册&#xff08;第4版&#xff09;》一书中的第2章&#xff0c;第2.11节秘诀&#xff1a;构建星星滑块&#xff0c;作者 【美】Erica Sadun&#xff0c;更多章节内容可以访问云栖社区“异步社区”公众号查看 2.11 秘诀&#xff1a;构建星星…

css框架和js框架_优雅设计的顶级CSS框架

css框架和js框架Brief discussion: 简要讨论&#xff1a; Well, who doesnt want their website or web page to look attractive, stylish and be responsive? 那么&#xff0c;谁不希望自己的网站或网页看起来有吸引力&#xff0c;时尚并且ReactSwift&#xff1f; We put …

软考下午题具体解释---数据流图设计

在历年的软考下午题其中&#xff0c;有五道大题。各自是数据流图的设计&#xff0c;数据库设计&#xff0c;uml图&#xff0c;算法和设计模式&#xff0c;从今天这篇博文開始&#xff0c;小编就跟大家来一起学习软考下午题的相关内容。包含理论上的知识以及典型例题的解说&…

基本程序 打印Scala的Hello World

Scala中的基本程序 (Basic program in Scala) As your first Scala program, we will see a basic output program that just prints "Hello World" or any other similar type of string. With this example, we will see what are the part of the code that is im…

java treemap lastkey_Java TreeMap lastKey()用法及代码示例

java.util.TreeMap.lastKey()用于检索Map中存在的最后一个或最高键。用法:tree_map.lastKey()参数&#xff1a;该方法不带任何参数。返回值&#xff1a;该方法返回映射中存在的最后一个键。异常&#xff1a;如果映射为空&#xff0c;则该方法将引发NoSuchElementException。以下…

mysql属于数据库三级模式_数据库系统的三级模式指的是什么

数据库系统的三级模式指的是什么发布时间&#xff1a;2020-10-26 10:11:21来源&#xff1a;亿速云阅读&#xff1a;52作者&#xff1a;小新小编给大家分享一下数据库系统的三级模式指的是什么&#xff0c;希望大家阅读完这篇文章后大所收获&#xff0c;下面让我们一起去探讨吧&…

《自顶向下网络设计(第3版)》——导读

目录 第1部分 辨明客户的需求和目标 第1章 分析商业目标和制约 1.1 采用自顶向下的网络设计方法 1.2 分析商业目标 1.3 分析商业制约 1.4 商业目标检查表 1.5 小结 1.6 复习题 1.7 设计环境 第2章 分析技术目标与折衷措施 2.1 可扩展性 2.2 可用性 2.3 网络性能 2.4 安全性 2…

python矩阵变化_用numpy改变矩阵的形状

我的问题有两个方面。我有下面的代码来处理一些矩阵。在import numpytupleList [(0, 122), (1, 246), (2, 157), (3, 166), (4, 315), (5, 108), (6, 172), (7, 20), (8, 173), (9, 38), (10, 28), (11, 72), (12, 102), (13, 277), (14, 318), (15, 316), (16, 283), (17, 31…

最小硬币问题_进行更改的最小硬币数量

最小硬币问题Description: 描述&#xff1a; This is classic dynamic programming problem to find minimum number of coins to make a change. This problem has been featured in interview rounds of Amazon, Morgan Stanley, Paytm, Samsung etc. 这是经典的动态编程问题…

java 生成xml乱码_jdom解决中文乱码问题 JAVA生成xml文件帮了我很大的忙

决解了数据库读取出来 再保存到xml 产生的乱码问题import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import org.jdom.Attribute;import org.jdom.Document;import org.jdom.Element;import org.jdom.output.Format;import org.…

给定重量上限,背包问题_满足给定重量的袋子的最低成本

给定重量上限,背包问题Problem statement: 问题陈述&#xff1a; You are given a bag of size W kg and you are provided costs of packets different weights of oranges in array cost[] where cost[i] is basically cost of i kg packet of oranges. cost[i] -1 means t…