---
title: "内部类形式的继承类"
author: "lylegend13"
date: "2011-08-18T18:26:56Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2011-08-18T18:27:52Z"
id: 1152690
url: "https://lltobe.iteye.com/blog/1152690"
---

```
public class test {

	private final int a;

	public test(int a) {
		this.a = a;
	}

	private int getA() {
		return a;
	}

	public void exec() {
		new test(1) {
			public void print() {
				System.out.println(getA());// 0
			}
		}.print();
	}

	public static void main(String[] args) {
		new test(0).exec();
	}
}
```

 

```
public class test {

	private final int a;

	public test(int a) {
		this.a = a;
	}

	public int getA() {
		return a;
	}

	public void exec() {
		new test(1) {
			public void print() {
				System.out.println(getA());// 1
			}
		}.print();
	}

	public static void main(String[] args) {
		new test(0).exec();
	}
}
```

 

差别在于getA()方法的可见性