定义:
可以将一个类的定义放在另一个类的内部 这就是内部类。--摘自java编程思想
一般实现方式:
public class SimpleInnerClass { class Content { private int i; public int getVlaue() { return i; } } class Description { private String lable; Description(String lab) { this.lable = lab; } public String readLable() { return lable; } } public void ship(String lable) { Content content=new Content(); Description description=new Description(lable); System.out.println(description.readLable()); } public static void main(String[] args) { Parcle2 parcle2 = new Parcle2(); parcle2.ship("hi"); } }
上面的事例是最普通不过的内部类表达的例子,通过创建内部类对象的引用访问内部类方法
但一般有更通用的做法,通常是在外围类创建一个方法关联内部类的引用;通过这种方式来建立彼此之间的联系。以代码为例:
public class InnerClass { class Content { private int i; public int getVlaue() { return i; } } class Description { private String lable; Description(String lab) { this.lable = lab; } public String readLable() { return lable; } } public Content getContentInstance() { return new Content(); } public Description getDescriptionIntance(String lable) { return new Description(lable); } public void ship(String lable) { Content content = getContentInstance(); Description description = getDescriptionIntance(lable); System.out.println(description.readLable()); System.out.println(description.readLable()); } public static void main(String[] args) { InnerClass parcle2 = new InnerClass(); parcle2.ship("hi"); InnerClass.Content c = parcle2.getContentInstance();// 如果想在外部类的非静态方法之外的任意位置访问某个内部类的对象,那么必须通过OutClass.xx InnerClass.Description d = parcle2.getDescriptionIntance("hello"); } }
有了联系之后内部类就可以访问外围类的所有元素,无论是private类型也可以。
以下代码即是佐证:
public interface Selector { boolean end(); Object current(); void next(); } public class Sequence { private Object[] items; private int next = 0; public Sequence(int size) { items = new Object[size]; } public void add(Object object) { if (next < items.length) { items[next++] = object; } } class SequenceSelector implements Selector { private int i = 0; @Override public boolean end() { // TODO Auto-generated method stub return i == items.length; } @Override public Object current() { // TODO Auto-generated method stub return items[i]; } @Override public void next() { if (i < items.length) { i++; } } } /** * 内部类对象只有与外围类对象相关联时才能被创建<p> * 构建内部类对象时,需要指向一个其外围类对象的引用;如果编译器访问不到这个引用就会报错 * @return */ public Selector selector() { return new SequenceSelector(); } public static void main(String[] args) { Sequence sequence = new Sequence(5); for (int i = 0; i < 5; i++) { sequence.add(i); } // Sequence.SequenceSelector c=sequence.selector(); Selector selector = sequence.selector(); while (!selector.end()) { System.out.println(selector.current()); selector.next(); } } }
在此,比较初始化的内部类细节介绍完毕,下一章笔者在继续介绍更深一层次的内部类细节
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; color: #931a68 }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Menlo; min-height: 14.0px }
span.s1 { color: #931a68 }
span.s2 { color: #000000 }
span.s3 { color: #0326cc }
span.s4 { color: #7e504f }
span.s5 { text-decoration: underline; color: #7e504f }
span.s6 { color: #3933ff }
span.Apple-tab-span { white-space: pre }
原文地址:https://www.cnblogs.com/zhangfengshi/p/9385808.html
时间: 2024-10-31 17:29:08