您的位置:首页 > 编程语言 > Java开发

自家用的java小总结(2.4):类的知识的查漏补缺(内部类)

2014-08-13 23:43 281 查看
  

这篇文章说了些什么?

这文章是我近来8.6号来在编程思想上打的代码,从0~200页的源码接近到在这里,下文正是总结这0~200页的的知识,涉及到接口,内部类.初始化,数值计算的一些细节.此文章不会一下子写完,可能隔一天可能再补下来.因为代码确实有点多..


注意

1 我的注释不一定正确(不过各小标题和代码一定是正确的,因为是书本上的原话,但是注释不一定正确),如果你确信我的内容的话,你可能会损失很大,因为我只是个菜鸟,我只是来补救一些知识的而已,如果不复制(最好自己打上)代码运行一次,可能会被我的在代码的注释所误解, 欢迎提意见和评论~~~你的意见和评论是我更新的动力(促进我更快滴读完编程思想)


本文适合读者

适合有一定的编程基础的人,因为我的代码可能出错,建议零基础的人找本书先看看,或者一定要复制我的代码运行一下,否则,后果将不堪设想  <( ̄▽ ̄)> 哇哈哈…因为我也只是新手
  


再一次提醒

下面的注释只是我个人的总结.

我的注释不一定正确(不过各小标题和代码一定是正确的,因为是书本上的原话,但是注释不一定正确)

,但是注释的语序凌乱各种天花百草不说了.!!!!绝非书本原话!!!!所以要最好还是把我的代码复制下来,运行一遍,也可以把我的注释删掉,或者提示我说应该怎么说,应该怎么说.


再一次感谢

1 感谢你可以抽出你的时间来进行下面阅读,思考的碰撞是智慧产生的前提.谢谢你的阅读和你愿意复制我的代码并阅读我那千奇百怪无比丑的注释来进行思考的碰撞!~!~


正文:

内部类对于外部类来说,像是定义的关系
外部类对于内部类而言,像是继承的关系


1.内部类的对象的创建

通常都需要依赖外部类对象来使用
1.定义一个外部类对象p
第一个p.new 内部类对


package test.java;

public class Parcel2{
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;
}
}
//又创建两个方法来得到对象,实现多态
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) {
Parcel2  p = new Parcel2();
p.ship("Tasmania");
Parcel2 q = new Parcel2();

//可以两种这样的回归
Parcel2.Contents c = q.contents();
Parcel2.Destination d= q.to("Borneo");
}  
}


3. .this和.new的介绍咯

package test.java;

public class DotThis{
void f(){
System.out.println("DonThis.f()");
}

public class Inner{
public DotThis outer(){
return DotThis.this;  //这个this 是innner的this
}
public void haveATry(){
//			this.f();
DotThis.Inner.this.f();
}
void f(){
System.out.println("Inner This");
}
}
public Inner inner(){
return new Inner();
}

public static void main(String[] args) {
DotThis dt = new DotThis();
//注
//		DotThis.Inner dt = new DotThis().new Inner();//这样才可以
DotThis.Inner dti = dt.inner();	//返回内部类的对象
//给dti的inner
//在这里也可以证明 内部类必须依赖一个外部类的对象来创建 对象
//但这就奇怪了
//如果这样的话,'

//新建一个内部类的对象->
dti.outer().f();		//但是这个通过Inner的返回发现调用的是外部类的F()方法,也就是说,外部类的
dti.haveATry();						//这里证明了DTI是内部类的对象
}
}


DonThis.f()
Inner This


4.在方法里定义内部类

package test.java;
interface Destination{

}
public class Parcel5{
public Destination destination(String s){  //方法里定义内部类
class PDestination implements Destination{   //这个类是方法的一部分,而不是类的一部分
private String label;					//在方法外不能访问 PD类
private PDestination(String whereTo){
label = whereTo;
}
public String readLabel(){
return label;
}
}
return new PDestination(s);  //返回内部类对象
}

public static void main(String[] args) {
Parcel5 p= new Parcel5();
Destination d = p.destination("ABC");   //也能被赋值
}

}


5.if里面的内部类

package test;
interface Destination{

}
public class
Parcel6{
private void internalTracking(boolean b){
if ( b)
{
class TrackingSlip
{
private String id;

TrackingSlip(String s){
id =s;
}

String getSlip(){
return id;
}
}
TrackingSlip ts = new TrackingSlip("Slip");
String s = ts.getSlip();
System.out.println(s);
//在if里面定义  和使用内部类完全 大丈夫~~~  表明 内部类 也只是一个变量类型(特殊的)
}
//		TrackingSlip ts = new TrackingSlip("x");在 if 外 Tracking类型就无法解析了
}

public void track(){
internalTracking(true);
}

public static void main(String[] args) {
Parcel6 p = new Parcel6();
p.track();
}
}


Slip


6.匿名内部类

new 父类接口()
{
//注意在这里会覆盖父类的方法.
}//分号不能丢
;


package test;
public class Parcel8{
public Wrapping warpping (int x){		//这里设置一个参数,传过来当构造器的从参数
return new Wrapping(x)		//如果高早起里有参数,就这样来
{
public int value()
{
return super.value()*47;  //匿名内部类只能写同名方法,来进行局部覆盖,匿名内部类方法的调用
//													必须依赖一个类,也必须依赖一个  返回该类的对象的方法
}
};
}
public static void main(String[] args) {
Parcel8 p  = new Parcel8();
Wrapping w = p.warpping(10);		//这里返回了明显值得是匿名内部类的对象

System.out.println(w.value());
}
}
class Wrapping{
private int i;
public Wrapping (int x){
i  =x;
}
public int value(){
return i;
}
}


470


7.内部类里面的赋值的数字必须final

package test.java;

public class Parcel9{
//	public Destination destination (final String dest){
public Destination destination ( String dest){
return new Destination()
{
private String label = dest;		//在内部类赋值的的时候,一定要将赋值的参数变成终态
public String readLabel()
{
return label;
}
};
}
public static void main(String[] args) {
Parcel9 p = new Parcel9();
Destination d = p.destination("ABC");
}
}

class Destination{
private String label;
Destination ()
{

}
}


8.在内部类里面使用方法

new 父类()
{
//注意这里像是一个新类那样,不能调用方法.
而调用方法就
{
//初始化域.
}
};


package test.java;

abstract class  Base{
public Base(int i){
System.out.println("Base constroct , i =" +i);

}
public abstract void f();
}
public class AnnoymousConstructor{
public static Base getBase(int i)
{
return
new Base(i)
{
//首先会new一下构造函数
{
System.out.println("inside instatnce initializer");	//在main {} static{}里头才能调用方法- -~~~晕菜了
}
public void f()
{
System.out.println("In anoymous f()");
}
//没有实现f()方法会报错, 因为是抽象的方法

};
}

public static void main(String[] args) {
Base base = getBase(47);
base.f();
}
}


8.匿名内部类实现工厂方法

package test.java;

interface Service{
void method1();
void method2();
}

interface ServiceFactory{
Service getService();
}
class Implementation1 implements Service{		//声明为abstract class的时候,就不会产生奇葩行为
//	privat/e Implemention1{}
//	public void method1();
//	public vo
private Implementation1(){}
public void method1()
{
System.out.println("Implements method");
}
public void method2()
{
System.out.println("Implements method2");
}
//static方法
public static  ServiceFactory factory =
new ServiceFactory()
{
public Service getService()
{
return new Implementation1();
}
};
}

class Implementation2 implements Service{
private  Implementation2(){}
public void method1()
{
System.out.println("Implementation2 method1");
}
public void method2()
{
System.out.println("Implementation2 method2");
}

//static  方法
public static  ServiceFactory factory =
new ServiceFactory()
{
public Service getService()
{
return new Implementation2();
}
};
}

public class Factories{
public static void serviceConsumer(ServiceFactory fact)
{
Service s = fact.getService();
s.method1();
s.method2();
}

public static void main(String[] args) {
//为了让别人不轻易滴制造类的对象,  运用 static来返回对象 ,匿名内部类,不用再创建类的对象的调用
serviceConsumer(Implementation1.factory);
serviceConsumer(Implementation2.factory);

}
}


9.在接口中可以定义类的里面.

package test.java;
public interface ClassInIterface
{
void howdy();
class Test implements ClassInIterface
{
public void howdy()
{
System.out.println("howdy!");
}
public static void main(String[] args) {
new Test().howdy();   //因为接口都是自动提升为 public 和static的,  变量添加为final ,
//这样的话证明了接口里面应该是可以定义类,和实现类,但是不可以写明方法体
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: