《JAVA编程思想》学习备忘(p345 Inner Classes-1)
来源:互联网 发布:淘宝理财产品安全吗 编辑:程序博客网 时间:2024/06/05 17:45
It's possible to place a class definition within another class definition.This is called an inner class.
The inner class is a valuable feature because it allows you to group classes that logically belong together and to control the visibility of one within the other.
creating inner classes
示例:
public class Parcel1 {
}
输出:
Tasmania
More typically,an outer class will have a method that returns a reference to an inner class:
public class Parcel2 {
}
输出:
Tasmania
If you want to make an object of the inner class anywhere except from within a non-static method of the outer class,you must specify the type of that object as OuterClassName.InnerClassName,as seen in main().
The link to the outer class
...In addition,inner classes have access rights to all the elements in the enclosing class:
public interface Selector {
}
public class Sequence {
}
输出:
0 1 2 3 4 5 6 7 8 9
So an inner class has automatic access to the members of the enclosing class.
Using .this and .new
If you need to produce the reference to the outer-class object,you name the outer class followed by a dot and this.
示例:
public class DotThis {
}
输出:
DotThis.f()
Sometimes you want to tell some other object to create an object of one of its inner classes.To do this you must provide a refrence to the other outer-class object in the new expression,using the .new syntax,like this:
public class DotNew {
}
Here,you see the use of .new applied to the "Parcel" example:
public class Parcel3 {
}
Inner classes and upcasting
示例:
public interface Destination {
}
public interface Contents {
}
public class Parcel4 {
}
public class TestParcel {
}
...the private inner class provides a way for the class designer to completely prevent any type-coding dependencies and to completely hide details about implementation.
Inner classes in methods and scopes
使用内置类的两个原因:
1.As shown previously,you're implementing an interface of some kinkd of that you can create and return a reference.
2.You're solving a complicated problem and you want to create a class to aid in your solution,but you don't want it publicly available.
The first example shows the creation of an entire class within the scope of a method(instead od the scope of another class).This is called a local inner class:
public class Parcel5 {
}
The next example shows how you can nest an inner class within any arbitary scope:
public class Parcel6 {
}
Anonymous inner classes
下边的例子看起来有点奇怪:
public class Parcel7 {
}
The contents() method combines the creation of the return value with the definition of the class that represents that return value!In addition,the class is anonymous;it has no name.
What this strange syntax means is "Create an object of an anonymous class that's inherited from Contents."
public class Parcel7b {
}
In the anonymous inner class,Contents is created by using a default constructor.
The following code shows what to do if your base class needs a constructor with an argument:
public class Wrapping {
}
public class Parcel8 {
}
That is,you simply pass the appropriate argument to the base-class constructor,seen here as the x passed in new Wrapping(x).Although it's an ordinary class with an implementation,Wrapping is also being used as a common "interface" to its derived classes:
public class Wrapping {
}
You can also perform initialization when you define fields in an anonymous class:
public class Parcel9 {
}
If you're defining an anonymous inner class and want to use an object that's defined outside the anonymous inner class,the compiler requires that the argument reference be final.
As long as you're simply assigning a field,the approach in this example is fine.But what if you need to perform some constructor-like activity?You can't have a named constructor in an anonymous class(since there's no name!),but with instance initialization,you can,in effect,create a constructor for an anonymous inner class,like this:
abstract class Base {
}
public class AnonymousConstructor {
}
输出:
Base constructor, i = 47
Inside instance initializer
In anonymous f()
Here's the "parcel"theme with instance initialization.Note that the arguments to destination() must be final since they are used within the anonymous class:
public class Parcel10 {
}
输出:
Over budget!
(待续)
- 《JAVA编程思想》学习备忘(p345 Inner Classes-1)
- 《JAVA编程思想》学习备忘(p345 Inner Classes-2)
- 《JAVA编程思想》学习备忘(p345 Inner Classes-3)
- 《JAVA编程思想》学习备忘(第237页:Reusing Classes)-1
- 《JAVA编程思想》学习备忘(第237页:Reusing Classes)-2
- 《JAVA编程思想》学习备忘(第237页:Reusing Classes)-3
- 《JAVA编程思想》学习备忘(第237页:Reusing Classes)-4
- 《JAVA编程思想》学习备忘(第237页:Reusing Classes)-5
- 续《JAVA编程思想》学习备忘(第237页:Reusing Classes)-5
- 《JAVA编程思想》学习备忘(第277页Polymorphism-1)
- 《JAVA编程思想》学习备忘(第311页Interfaces-1)
- 《JAVA编程思想》学习备忘:Holding Your Objects-1
- 《JAVA编程思想》学习备忘(第277页Polymorphism-2)
- 《JAVA编程思想》学习备忘(第277页Polymorphism-3)
- 《JAVA编程思想》学习备忘(第61页:Everything Is an Object--1)
- 《JAVA编程思想》学习备忘(第93页:Operators--1)
- 《JAVA编程思想》学习备忘(第155页:Initialization & Cleanup)--1
- 《JAVA编程思想》学习备忘(第209页:Access Control)-1
- 想动手做一个东西.就当练练手吧
- Trojan.Remover.v6.7.2.READNFO_CRKEXE-FFF
- ARTeam Armag3ddon 1.4 by CondZero
- FoxIt.Reader.Pro.v2.3.3201-DOA
- j2me
- 《JAVA编程思想》学习备忘(p345 Inner Classes-1)
- 《Lucene分析与应用》终于可以和大家见面了
- IE 控件一些高级使用方法
- 关于notifydestroyed() 与 notifypause()
- ntoskrnl.exe implementation
- php+apache+mysql终于实现了第一个动态网页,写一下感受
- 写给C语言初学者的话[转北风]
- Java语言中Timer类的简洁用法
- 菜鸟来了:三层架构从哪里开始写