java内部类 2016年12月13号
1、在外部类的任意位置创建内部类对象的方法:
1)从外部类的非静态方法之外的任意位置创建某个内部类的对象,必须指明这个对象所在的外部类和内部类:OuterClassName.InnerClassName。如:
package cn.qdu.parcel;
/**
* 内部类的第一个例子
* @author cena 2016/12/13 at BoZhi
*
*/
public class Parcel {//外部类
class Contents{//内部类
private int i=11;
public int value(){
return i;
}
} class Destination{//内部类
private String label;
Destination(String whereTo){
label=whereTo;
}
String readLabel(){return label;}
}
//下面是外部类中定义的3个方法
public Destination to(String s){
return new Destination(s);
} public Contents contents(){
return new Contents();
} public void ship(String dest){
Contents c=contents();
Destination d=to(dest);
System.out.println(d.readLabel());
} public static void main(String[] args) {
Parcel p=new Parcel();
p.ship("Tasmaina");
Parcel q=new Parcel();
//在外部类的非静态方法之外的任意位置创建某个内部类的对象,必须具体地指明这个对象的类型:OuterClassName.InnerClassName。
Parcel.Contents c=q.contents();//定义内部类对象的方法
Parcel.Destination d=q.to("Borneo");
}
}
2)外部类的static方法建立内部类问题。如:
class Outer{//外部类
class Inner{//内部类
}
public static method(){
Outer out=new Outer();//要先创建外部类的对象
Inner in=out.new Inner();
}
}
method()方法属于外部类的静态方法,内部类Inner相当于外部类的一个非静态方法Inner(){},创建内部类对象时需要先创建外部类对象out,用外部类Outer的out对象之后才能去定义Inner对象,这个道理就如同在静态方法中不能直接调用非静态方法一样。
java内部类 2016年12月13号的更多相关文章
- 2016年12月13日 星期二 --出埃及记 Exodus 21:8
2016年12月13日 星期二 --出埃及记 Exodus 21:8 If she does not please the master who has selected her for himsel ...
- WinForm 进程、线程、TreeView递归加载、发送邮件--2016年12月13日
进程:一个程序就是一个进程,但是也有一个程序需要多个进程来支持的情况 进程要使用的类是:Process它在命名空间:System.Diagnostics; 静态方法Start(); Process.S ...
- 2016年12月18日 星期日 --出埃及记 Exodus 21:13
2016年12月18日 星期日 --出埃及记 Exodus 21:13 However, if he does not do it intentionally, but God lets it hap ...
- 北京Uber优步司机奖励政策(12月13日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 2019第一期《python测试开发》课程,10月13号开学
2019第一期<python测试开发>课程,10月13号开学! 主讲老师:上海-悠悠 上课方式:QQ群视频在线教学,方便交流 本期上课时间:10月13号-12月8号,每周六.周日晚上20: ...
- 2016年12月31日 星期六 --出埃及记 Exodus 21:26
2016年12月31日 星期六 --出埃及记 Exodus 21:26 "If a man hits a manservant or maidservant in the eye and d ...
- 2016年12月30日 星期五 --出埃及记 Exodus 21:25
2016年12月30日 星期五 --出埃及记 Exodus 21:25 burn for burn, wound for wound, bruise for bruise.以烙还烙,以伤还伤,以打还打 ...
- 2016年12月29日 星期四 --出埃及记 Exodus 21:24
2016年12月29日 星期四 --出埃及记 Exodus 21:24 eye for eye, tooth for tooth, hand for hand, foot for foot,以眼还眼, ...
- 2016年12月28日 星期三 --出埃及记 Exodus 21:23
2016年12月28日 星期三 --出埃及记 Exodus 21:23 But if there is serious injury, you are to take life for life,若有 ...
随机推荐
- Reveal使用步骤和 破解Revealapp的试用时间限制
下载地址:http://pan.baidu.com/s/1eQstR2M 一.Reveal使用步骤 1.启动Reveal --> Help --> Show Reveal Library ...
- UIWebView用法详解及代码分享
今天我们来详细UIWebView用法.UIWebView是iOS内置的浏览器控件,可以浏览网页.打开文档等 能够加载html/htm.pdf.docx.txt等格式的文件. 用UIWebView我们就 ...
- Java 网络编程----基本概念
网络现在是一个非常普遍的概念. 以下是维基百科上的解释: 网络一词有多种意义,可解作: 网络流也简称为网络(network).一般用于管道系统.交通系统.通讯系统建模. 有时特指计算机网络. 或特指其 ...
- rails关于utf8问题-------------------utf8申明必须置顶
utf-8必须置顶,如果放在其他位置,会导致后面如果遇到中文无法解析,然后报其他乱七八糟的错误,比如不能连接数据库,比如语法错误......这种错误不好找,切记!!! 出错代码: #!/bin/env ...
- Effective Java 14 In public classes, use accessor methods, not public fields
Principle To offer the benefits of encapsulation you should always expose private field with public ...
- cmd常用命令 和 sql server相关基础
在Java开发中 ms sql server 接触算是比较少的,本文记录一些ms sql server的基础知识. 1. 为表字段增加索引:create index user_openid on us ...
- npm报错Error: ENOENT, stat 'D:\NodeLearn\node-global'
最近想试下当前的当红炸子鸡 Nodejs,在安装配置时,发生了下面的错误: C:\nodejs\npmjs\bin>cd .. C:\nodejs\npmjs>cd .. C:\nodej ...
- Servlet生命周期+工作原理
Servlet生命周期+工作原理 1.Servlet的生命周期: Servlet加载,加载,服务,销毁. 2.典型函数解释: Init():这个函数是用来初始化Servlet对象的.在 ...
- 无法将匿名方法转换为System.Delegate
在WinForm中,不允许非UI线程访问UI,如果非UI线程需要跨线程调用UI控件,通常的解决办法是使用Control类中的Invoke方法,传递给该方法一个委托和委托调用的参数列表(params [ ...
- 【原】基于 HAproxy 1.6.3 Keeplived 在 Centos 7 中实现mysql mariadb galera cluster 集群分发读写 —— 上篇
前言 有一段时间没有写blogs,乘着周末开始整理下haproxy + keeplived 实现 mysql mariadb galera cluster 集群访问环境的搭建工作. 本文集中讲hapr ...