自家用的java小总结(2.5):类的知识的查漏补缺(静态内部类)
|
|
1
|
发现了,得加上输出结果,怕自己出错,~~ |
这篇文章说了些什么?
这文章是我近来8.6号来在编程思想上打的代码,从0~200页的源码接近到在这里,下文正是总结这0~200页的的知识,涉及到接口,内部类.初始化,数值计算的一些细节.此文章不会一下子写完,可能隔一天可能再补下来.因为代码确实有点多..
注意
1 我的注释不一定正确(不过各小标题和代码一定是正确的,因为是书本上的原话,但是注释不一定正确),如果你确信我的内容的话,你可能会损失很大,因为我只是个菜鸟,我只是来补救一些知识的而已,如果不复制(最好自己打上)代码运行一次,可能会被我的在代码的注释所误解, 欢迎提意见和评论~~~你的意见和评论是我更新的动力(促进我更快滴读完编程思想)
本文适合读者
适合有一定的编程基础的人,因为我的代码可能出错,建议零基础的人找本书先看看,或者一定要复制我的代码运行一下,否则,后果将不堪设想 <( ̄▽ ̄)> 哇哈哈…因为我也只是新手
再一次提醒
下面的注释只是我个人的总结.
我的注释不一定正确(不过各小标题和代码一定是正确的,因为是书本上的原话,但是注释不一定正确)
,但是注释的语序凌乱各种天花百草不说了.!!!!绝非书本原话!!!!所以要最好还是把我的代码复制下来,运行一遍,也可以把我的注释删掉,或者提示我说应该怎么说,应该怎么说.
再一次感谢
1 感谢你可以抽出你的时间来进行下面阅读,思考的碰撞是智慧产生的前提.谢谢你的阅读和你愿意复制我的代码并阅读我那千奇百怪无比丑的注释来进行思考的碰撞!~!~
正文:
1.补充一些嵌套内部类的用法
(补充,内部类不可以有静态方法,但可以有静态final 方法
像数组那样.
int[][] a = new int[3][3];
也可以
long[][] numfive=new long[5][]; //定义一个long类型的不规则数组
numfive[0]=new long[5]; //为第1行分配5列
numfive[1]=new long[6]; //为第2行分配6列
int[][] numeight={{100,200,300,400},{500,600,700,800},{900,1000,1100,1200,1300}};
类也雷同
只要记住嵌套,那就可以了
第一步:定义一个外部类对象
第二步 外部类.内部类= 外部类对象.new 内部类对象
第三步 外部类.内部类.再内部类 = 第二步得到的对象.new 再内部类对象
package test.java;
class MNA
{
private void f(){}
class A
{
private void g(){}
public class B
{
void h()
{
g();
f();
}
}
}
}
class test
{
public static void main(String[] args) {
MNA mna = new MNA();
MNA.A mnaa = mna.new A();
MNA.A.B mnaab = mnaa.new B();
}
}
2.嵌套内部类
static 内部类 有什么属性呢?
●为创建一个static内部类的对象,我们不需要一个外部类对象 ●不能从static内部类的一个对象中访问一个外部类对象 ●由于static成员只能位于一个类的外部级别,所以内部类不可拥有static数据或static内部类
但静态内部类可以有static属性
package test.java;
public class Parcel11{
public static class ParcelContents implements Contents
{
private int i=1;;
public int value() //普通的内部类是不能声明static方法的.我知道啊
{
return i;
}
}
protected static class ParcelDestination implements Destination
{
private String label;
private ParcelDestination(String whereTo)
{
label = whereTo;
}
public String readLabel()
{
return label;
}
public static void f(){}
static int x = 10;
static class AnotherLevel
{
public static void f(){}
static int x =10;
}
}
public static Destination destination (String s )
{
return new ParcelDestination(s);
}
public static Contents contents()
{
return new ParcelContents();
}
public static void main(String[] args) {
Contents c = contents(); // 类里面嵌套static类 ,然后全部都变成static方法是搞那样
Destination d = destination("ABC"); //这样子就完全不依赖内部类对象了, static类的话,只要一个上溯造型,就可以引出家了
}
}
interface Contents
{
}
interface Destination
{
}
5.闭包和回调
package test.java;
interface Incrementable
{
void increment();
}
class Callee1 implements Incrementable
{
private int i=0;
public void increment()
{
i++;
System.out.println(i);
}
}
class MyIncrement
{
public void increment()
{
System.out.println("Other Operation~");
}
static void f(MyIncrement mi) //也是利用上溯造型
{
mi.increment();
}
}
class Callee2 extends MyIncrement
{
private int i=0;
public void increment()
{
super.increment();
i++;
System.out.println(i);
}
private class Closure implements Incrementable
{
public void increment()
{
Callee2.this.increment();
}
}
Incrementable getCallBackReference()
{
return new Closure(); //回调的意思是, 我不能直接调用A------>借助一个东西 --->再调用A
} //闭包呢是一个包含信息的对象, 例如 内部类是面向对象的闭包
} //因为内部类不仅包含外部类对象,还自动拥有指向外围对象的引用
class Caller{
private Incrementable callbackreference;
Caller(Incrementable cbn)
{
callbackreference = cbn;
}
void go()
{
callbackreference.increment();
}
}
public class callBacks{
public static void main(String[] args) {
Callee1 c1 = new Callee1();
Callee2 c2 = new Callee2();
MyIncrement.f(c2); // Other Opeartion
Caller caller1 = new Caller(c1); // caller1 的new back 是c1
Caller caller2 = new Caller(c2.getCallBackReference()); //caller2的调用c2 new Closure();
caller1.go();
caller1.go();
caller2.go();
caller2.go();
}
}
6.内部类

浙公网安备 33010602011771号