Java学习笔记(十二)面向对象---内部类,
分享于 点击 42437 次 点评:57
Java学习笔记(十二)面向对象---内部类,
内部类的访问规则
- 内部类可以直接访问外部类中的成员,包括私有成员。
因为内部类中持有了一个外部类的引用,格式为:外部类名.this
- 外部类要访问内部类,必须要建立内部对象。
class Outer { //外部类
private int x = 1;
class Inner{ //内部类
int x = 2;
void function() {
int x = 3;
System.out.println(x);
System.out.println(this.x);
System.out.println(Outer.this.x);
}
}
void method() {
Inner in = new Inner();
in.function();
}
}
public class InnerClassDemo {
public static void main(String[] args) {
Outer out = new Outer();
out.method();
Outer.Inner in = new Outer().new Inner();
in.function();
}
}
因为内部类中持有了一个外部类的引用,格式为:外部类名.this
class Outer { //外部类
private int x = 1;
class Inner{ //内部类
int x = 2;
void function() {
int x = 3;
System.out.println(x);
System.out.println(this.x);
System.out.println(Outer.this.x);
}
}
void method() {
Inner in = new Inner();
in.function();
}
}
public class InnerClassDemo {
public static void main(String[] args) {
Outer out = new Outer();
out.method();
Outer.Inner in = new Outer().new Inner();
in.function();
}
}
运行结果:
3
2
1
3
2
1
访问
静态内部类
- 在外部其他类中,如何直接访问静态内部类的非静态成员
class Outer {
private static int x = 1;
static class Inner {
void function() {
System.out.println(x);
}
}
}
public class InnerDemo2 {
public static void main(String[] args) {
new Outer.Inner().function();
}
}
- 在外部其他类中,如何直接访问静态内部类的静态成员
class Outer {
private static int x = 1;
static class Inner {
static void function() {
System.out.println(x);
}
}
}
public class InnerDemo3 {
public static void main(String[] args) {
Outer.Inner.function();
}
}
- 当内部类中定义了静态成员,该内部类必须也是静态的
以下代码编译会导致编译错误
class Outer {
private static int x = 1;
class Inner { //此内部类中定义了静态成员但是此内部类不为静态
static void function() {
System.out.println(x);
}
}
}
public class InnerDemo3 {
public static void main(String[] args) {
Outer.Inner.function();
}
}
- 当外部类中的静态方法访问内部类时,内部类也必须是静态的。
内部类的定义原则
class Outer {
private static int x = 1;
static class Inner {
void function() {
System.out.println(x);
}
}
}
public class InnerDemo2 {
public static void main(String[] args) {
new Outer.Inner().function();
}
}
class Outer {
private static int x = 1;
static class Inner {
static void function() {
System.out.println(x);
}
}
}
public class InnerDemo3 {
public static void main(String[] args) {
Outer.Inner.function();
}
}
以下代码编译会导致编译错误
class Outer {
private static int x = 1;
class Inner { //此内部类中定义了静态成员但是此内部类不为静态
static void function() {
System.out.println(x);
}
}
}
public class InnerDemo3 {
public static void main(String[] args) {
Outer.Inner.function();
}
}
当描述一个事物时,这个事物的内部有事物,此时这个内部的事物需要使用内部类来描述。
局部内部类
- 内部类定义在局部时:
class Outer {
void method(final int x) {
class Inner {
void function() {
System.out.println(x);
}
}
new Inner().function();
}
}
public class InnerDemo4 {
public static void main(String[] args) {
Outer out = new Outer();
out.method(7);
out.method(8);//因为局部变量在栈内存中,每次一调用入栈,使用结束出栈。所以虽然是被final修饰,但是仍然可以重复调用。
}
}
class Outer {
void method(final int x) {
class Inner {
void function() {
System.out.println(x);
}
}
new Inner().function();
}
}
public class InnerDemo4 {
public static void main(String[] args) {
Outer out = new Outer();
out.method(7);
out.method(8);//因为局部变量在栈内存中,每次一调用入栈,使用结束出栈。所以虽然是被final修饰,但是仍然可以重复调用。
}
}
运行结果:
7
8
匿名内部类
abstract class Abs {
abstract void method();
}
class Outer {
int x = 3;
/*class Inner extends Abs {
@Override
void mthod() {
System.out.println(x);
}
}*/
public void function() {
//new Inner().mthod();
new Abs() {
@Override
void method() {
System.out.println(x);
}
}.method();
}
}
public class InnerDemo5 {
public static void main(String[] args) {
new Outer().function();
}
}
abstract class Abs {
abstract void method();
}
class Outer {
int x = 3;
/*class Inner extends Abs {
@Override
void mthod() {
System.out.println(x);
}
}*/
public void function() {
//new Inner().mthod();
new Abs() {
@Override
void method() {
System.out.println(x);
}
}.method();
}
}
public class InnerDemo5 {
public static void main(String[] args) {
new Outer().function();
}
}
运行结果
3
以上代码将注释部分,用匿名内部类的形式体现了出来。
public class InnerClassTest {
public static void main(String[] args) {
new Object() { //如果左边有Object类型引用(Object ob = ),将无法调用method方法
public void method() {
System.out.println("method run");
}
}.method();
}
}
练习
interface Inter {
void method();
}
class Test {
//补足代码,通过匿名内部类
}
public class InnerClassTest {
public static void main(String[] args) {
Test.function().method();
}
}
分析 Test.function().method(); 这行代码可以发现function()应该是Test类中一个静态方法,function()方法后面还有一个method()的调用,说明调用function()方法应是返回了一个对象而且是Inner类型的对象,从而才能继续调用method()方法。
可以得到以下代码:
interface Inter {
void method();
}
class Test {
//补足代码,通过匿名内部类
static Inter function() { //这里返回值类型为Inner
return new Inter(){
@Override
public void method() {
System.out.println("Hello World");
}
};
}
}
public class InnerClassTest {
public static void main(String[] args) {
Test.function().method();
}
}
相关文章
- 暂无相关文章
用户点评