Java学习笔记——内部类及其调用方法
一、static内部类的static方法
public class Test0719_Inner_Test {
public static void main(String[] args) {
//static内部类的static方法。。。
Outter.InnerStatic.InnerNameee();
System.out.println(Outter.InnerStatic.jjj);
System.out.println();
}
}
class Outter {
String i = "非static";
static String j = "static";
static class InnerStatic{
static String jjj = "内部类的static";
public static void InnerNameee() {
System.out.println("static内部类的static方法。。。");
//static内部类访问外部类的static成员
System.out.println(j);
//static内部类访问外部类的非static成员
System.out.println(new Outter().i);
}
}
}
运行结果:
static
非static
内部类的static
二、static内部类的非static方法
public class Test0719_Inner_Test {
public static void main(String[] args) {
//static内部类的非static方法
Outter.InnerStatic inner = new Outter.InnerStatic();
inner.InnerName();
System.out.println(inner.iii);
System.out.println();
}
}
class Outter {
String i = "非static";
static String j = "static";
static class InnerStatic{
String iii = "内部类的非static";
public void InnerName() {
System.out.println("static内部类的非static方法");
//static内部类访问外部类的static成员
System.out.println(j);
//static内部类访问外部类的非static成员
System.out.println(new Outter().i);
}
}
}
运行结果:
static
非static
内部类的非static
三、非static内部类的非static方法
public class Test0719_Inner_Test {
public static void main(String[] args) {
//非static内部类的非static方法…………
Outter.InnerNoStatic innerNoStatic = new Outter().new InnerNoStatic();
innerNoStatic.InnerNoName();
System.out.println(innerNoStatic.iiiNoStatic);
}
}
class Outter {
String i = "非static";
static String j = "static";
class InnerNoStatic{
int iiiNoStatic = 99776;
public void InnerNoName() {
System.out.println("非static内部类的非static方法…………");
//非static内部类访问外部类的static成员
System.out.println(Outter.j);
//非static内部类访问外部类的非static成员
System.out.println(i);
}
}
}
运行结果:
static
非static
99776
四、综合练习
package com.acc;
public class Test0719_Inner2 {
String str = "孙洋洋";
class Text2 {
int i = 1;
void m2() {
System.out.println(new Test0719_Inner2().str);
}
class Text3 {
int k = 3;
public void m1() {
System.out.println("m1的"+ new Test0719_Inner2().str);
}
}
}
public static void main(String[] args) {
Test0719_Inner2.Text2 text2 = new Test0719_Inner2().new Text2();
text2.m2();
System.out.println();
Test0719_Inner2.Text2.Text3 text3 = new Test0719_Inner2().new Text2().new Text3();
text3.m1();
}
}
运行结果:
m1的孙洋洋
五、汇总
package com.test;
public class Test0719_Inner_Test {
public static void main(String[] args) {
//static内部类的static方法。。。
Outter.InnerStatic.InnerNameee();
System.out.println(Outter.InnerStatic.jjj);
System.out.println();
//static内部类的非static方法
Outter.InnerStatic inner = new Outter.InnerStatic();
inner.InnerName();
System.out.println(inner.iii);
System.out.println();
//非static内部类的非static方法…………
Outter.InnerNoStatic innerNoStatic = new Outter().new InnerNoStatic();
innerNoStatic.InnerNoName();
System.out.println(innerNoStatic.iiiNoStatic);
}
}
class Outter {
String i = "非static";
static String j = "static";
static class InnerStatic{
String iii = "内部类的非static";
static String jjj = "内部类的static";
public void InnerName() {
System.out.println("static内部类的非static方法");
//static内部类访问外部类的static成员
System.out.println(j);
//static内部类访问外部类的非static成员
System.out.println(new Outter().i);
}
public static void InnerNameee() {
System.out.println("static内部类的static方法。。。");
//static内部类访问外部类的static成员
System.out.println(j);
//static内部类访问外部类的非static成员
System.out.println(new Outter().i);
}
}
class InnerNoStatic{
int iiiNoStatic = 99776;
public void InnerNoName() {
System.out.println("非static内部类的非static方法…………");
//非static内部类访问外部类的static成员
System.out.println(Outter.j);
//非static内部类访问外部类的非static成员
System.out.println(i);
}
}
}
运行结果:
static
非static
内部类的static
static内部类的非static方法
static
非static
内部类的非static
非static内部类的非static方法…………
static
非static
99776
Java学习笔记——内部类及其调用方法的更多相关文章
- Java学习笔记(六)——方法
一.方法定义 1.语法: 其中: (1) 访问修饰符:方法允许被访问的权限范围, 可以是 public.protected.private 甚至可以省略 ,其中 public 表示该方法可以被其他任何 ...
- 疯狂java学习笔记之面向对象(三) - 方法所属性和值传递
方法的所属性: 从语法的角度来看:方法必须定义在类中 方法要么属于类本身(static修饰),要么属于实例 -- 到底是属于类还是属于对象? 有无static修饰 调用方法时:必须有主调对象(主语,调 ...
- Java学习笔记——设计模式之五.工厂方法
水边一只青蛙在笑 --石头和水 工厂方法模式(Factory Method),定义了一个用于创建对象的接口,让实现类决定实例化哪一个类.工厂方法使一个类的实例化延迟到其子类. 这里在简单和工厂的基础上 ...
- 0040 Java学习笔记-多线程-线程run()方法中的异常
run()与异常 不管是Threade还是Runnable的run()方法都没有定义抛出异常,也就是说一条线程内部发生的checked异常,必须也只能在内部用try-catch处理掉,不能往外抛,因为 ...
- Java学习笔记-内部类
内部类在Android中有着大量的运用 内部类 内部类提供了更好的封装:内部类可以直接访问外部类的私有数据:匿名内部类适合那些只需要使用一次的类.非静态内部类不能拥有静态成员.内部类比外部类可以多使用 ...
- 1.8(java学习笔记)继承与方法的重写
继承 在java中可以通过继承提高代码的复用率. 例如A继承了B,就可以是 例如,首先有一个类似Person,这个类中有有一些属性和方法,我们再新建一个Student类,其中有一部分属性和方法与Per ...
- java学习笔记之String.Split方法
hello 大家好,好久不见,今天 我们要讨论的是java的split方法,或许你很早就知道了,但你真的知道吗? 我们来看看吧. 首先我们来看看我们最常用的split()方法也就是单个参数的方法 pu ...
- Java 学习笔记之 线程interrupted方法
线程interrupted方法: interrupted()是Thread类的方法,用来测试当前线程是否已经中断. public class InterruptThread extends Threa ...
- Java 学习笔记之 线程interrupt方法
线程interrupt方法: interrupt方法是用来停止线程的,但是他的使用效果并不像for+break那样,马上就停止循环. 调用interrupt()其实仅仅是在当前线程中打了一个停止标记, ...
随机推荐
- [翻译][Trident] Trident state原理
原文地址:https://github.com/nathanmarz/storm/wiki/Trident-state ----------------------------- Trident在读写 ...
- 图片转换成base64_encode的链接代码示例
<?php $file = "example.jpg"; $type = getimagesize( $file ); //取得图片的大小,类型等 $file_content ...
- 解决sqlserver使用IP无法连接的问题,用localhost或者‘“.”可以连接
今天装了一个mssql发现用ip无法连接但是用localhost和“.”却可以连接,纠结了一天终于找到了问题的解决办法: 打开mssql配置管理器(我点电脑---->右键选择管理--->服 ...
- android获取手机的所有通讯录的号码和sim卡号码
============personer================================================ package com.qgc.cantent.entity; ...
- 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证]
二.使用Azure AD进行身份验证 之所以将Azure AD 作为开始,是应为基本上我们所有应用都需要进行安全管理.Azure Active Directory (Azure AD) 通过以下方式简 ...
- Javascript里,想把一个整数转换成字符串,字符串长度为2
Javascript里,想把一个整数转换成字符串,字符串长度为2. 想把一个整数转换成字符串,字符串长度为2,怎么弄?比如 1 => "01"11 => " ...
- 获取select当前选择的值和文本
<html> <head> <script type="text/javascript"> function EE(obj) { alert(& ...
- CloudStack4.2 更新全局参数API
测试更新全局参数API http://192.168.153.34:8080/client/api?command=updateConfiguration&response=json& ...
- codis3.1集群搭建
Codis31搭建 codis 3.1 安装搭建 一.基本信息 1. 服务器基本信息 ip地址 安装服务 172.16.200.71 zk1.codis-dashboard.codis-fe.codi ...
- k近邻法的C++实现:kd树
1.k近邻算法的思想 给定一个训练集,对于新的输入实例,在训练集中找到与该实例最近的k个实例,这k个实例中的多数属于某个类,就把该输入实例分为这个类. 因为要找到最近的k个实例,所以计算输入实例与训练 ...