Java中內部類的騷操作
- 2019 年 10 月 3 日
- 筆記
10.1 ???????
???10.1-1 ??
public class Parcel1 {
public class Contents{
private int value = 0;
?
public int getValue(){
return value;
}
}
}
???????????????,???????????????????????Contents??????????
???????10.1-1?????????????Contents??????
???10.1-2 ??
public class Parcel1 {
?
public class Contents{
private int value = 0;
?
public int getValue(){
return value;
}
}
?
public Contents contents(){
return new Contents();
}
?
public static void main(String[] args) {
Parcel1 p1 = new Parcel1();
Parcel1.Contents pc1 = p1.contents();
System.out.println(pc1.getValue());
}
}
????? 0
???????????????????????Contents??????????Contents??????????.?????????????????????????Parcel1.Contents pc1 = p1.contents();??????pc1 ???????????Contents??????
?????????????10.1-2 ??contents?????????pc1???????
????????????????????????????
10.2 ?????????
??????????????????????????????????????????????????????????????????????????????????????????????????????10.2-1
public class Parcel2 {
?
private static int i = 11;
?
public class Parcel2Inner {
?
public Parcel2Inner(){
i++;
}
?
public int getValue(){
return i;
}
?
}
?
public Parcel2Inner parcel2Inner(){
return new Parcel2Inner();
}
?
public static void main(String[] args) {
Parcel2 p2 = new Parcel2();
for(int i = 0;i < 5;i++){
p2.parcel2Inner();
}
System.out.println("p2.i = " + p2.i);
}
}
????? 16
??????????????????????????????????????????????Parcel2Inner ??????Parcel2??i????????????????
????????????????????????????????????????????????10.2-2
public class Parcel3 {
?
public class Contents {
?
public Parcel3 dotThis(){
return Parcel3.this;
}
?
public String toString(){
return "Contents";
}
}
?
public Parcel3 contents(){
return new Contents().dotThis();
}
?
public String toString(){
return "Parcel3";
}
?
public static void main(String[] args) {
Parcel3 pc3 = new Parcel3();
Contents c = pc3.new Contents();
Parcel3 parcel3 = pc3.contents();
System.out.println(pc3);
System.out.println(c);
System.out.println(parcel3);
}
}
??: Parcel3 Contents Parcel3
????????Parcel3?????????Contents,???????????dotThis(),???????????????,????????contents()???????????????????
10.3 ????????
?????????????????????????????????????????????????????????????????????? ??10.3-1
public interface Animal {
?
void eat();
}
?
public class Parcel4 {
?
private class Dog implements Animal {
?
public void eat() {
System.out.println("???");
}
}
?
public Animal getDog(){
return new Dog();
}
?
public static void main(String[] args) {
Parcel4 p4 = new Parcel4();
//Animal dog = p4.new Dog();
Animal dog = p4.getDog();
dog.eat();
}
}
??? ???
?????????????Dog??private?????????????????????????????????? ??????????Parcel4 ?public???Parcel4???????????????Animal??????Parcel4???????Dog????Dog?????Animal?????getDog()???????Animal?????????????????????????
10.4 ????????????????
????????????????????????????????????????????????????????? ???????????????
1.???????????????????????????????????
2.??????????????????????????????????????????
3.???…
??????????????
•??????????(?????)•???????????????????????(?????)•???????????(?????)•??????????????????•???????????????•??????????????????•??????????????????
public class Parcel5 {
private Destination destination(String s){
?
class PDestination implements Destination{
?
String label;
?
public PDestination(String whereTo){
label = whereTo;
}
?
public String readLabel() {
return label;
}
}
return new PDestination(s);
}
?
public static void main(String[] args) {
Parcel5 p5 = new Parcel5();
Destination destination = p5.destination("China");
System.out.println(destination.readLabel());
}
}
?? ? China
???????????????????????????????????????????????????????????????,?????????????Debugger??????????p5.destination(“China”)????????return new PDestination(s)??????PDestination?????????????????????????????????????????????????? ?: ????????????????
•???????????????????????
public class Parcel6 {
// ??????
private void eatCoconut(boolean flag){
// ?????????
if(flag){
class Coconut {
private String pipe;
?
public Coconut(String pipe){
this.pipe = pipe;
}
?
// ???????
String drinkCoconutJuice(){
System.out.println("????");
return pipe;
}
}
// ?????????????
Coconut coconut = new Coconut("????");
coconut.drinkCoconutJuice();
}
?
/**
* ?????????????????????
* ??????????????????????????
*/
// Coconut coconut = new Coconut("????");
// coconut.drinkCoconutJuice();
}
?
public static void main(String[] args) {
Parcel6 p6 = new Parcel6();
p6.eatCoconut(true);
}
}
??? ????
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
•???????????
???????????????????????return ??????????????????????????????????????????????
public interface Contents {
?
int getValue();
}
?
public class Parcel7 {
?
private Contents contents(){
return new Contents() {
?
private int value = 11;
?
@Override
public int getValue() {
return value;
}
};
}
?
public static void main(String[] args) {
Parcel7 p7 = new Parcel7();
System.out.println(p7.contents().getValue());
}
}
?? : 11
??????????????????? {}????????? ????????????????????????????????????????????????new Contents()??????Contents???????????????????????????????????????
public class Parcel7b {
?
private class MyContents implements Contents {
?
private int value = 11;
?
@Override
public int getValue() {
return 11;
}
}
?
public Contents contents(){
return new MyContents();
}
?
public static void main(String[] args) {
Parcel7b parcel7b = new Parcel7b();
System.out.println(parcel7b.contents().getValue());
}
}
?????????????? ??????????? 10.3 ?????????????
•??????????????????
????????????????(???????)????????
public class WithArgsConstructor {
?
private int sum;
?
public WithArgsConstructor(int sum){
this.sum = sum;
}
?
public int sumAll(){
return sum;
}
}
?
public class Parcel8 {
?
private WithArgsConstructor withArgsConstructor(int x){
?
// ??WithArgsConstructor???????????????
return new WithArgsConstructor(x){
?
// ??sumAll????????????
@Override
public int sumAll(){
return super.sumAll() * 2;
}
};
}
?
public static void main(String[] args) {
Parcel8 p8 = new Parcel8();
System.out.println(p8.withArgsConstructor(10).sumAll());
}
}
??WithArgsConstructor ????????????sum?????????????sumAll????sum???Parcel8??withArgsConstructor??????x???????????????????????????????????????sumAll?????????????? Java????198??? ????“;”?????????????????????????????????????????
•????????????????
???????????????????????????????????????????????????????? ????????
public class Parcel9 {
?
private Destination destination(String dest){
return new Destination() {
?
// ???????
private String label = dest;
?
@Override
public String readLabel() {
return label;
}
};
}
?
public static void main(String[] args) {
Parcel9 p9 = new Parcel9();
System.out.println(p9.destination("pen").readLabel());
}
}
????????????????????final??????final????????????????????????final?????????? ???????private???????private ??public,????????
???????????????????????Java????????????????????????????????????????????????
•??????????????????
public abstract class Base {
?
public Base(int i){
System.out.println("Base Constructor = " + i);
}
?
abstract void f();
}
?
public class AnonymousConstructor {
?
private static Base getBase(int i){
?
return new Base(i){
{
System.out.println("Base Initialization" + i);
}
?
public void f(){
System.out.println("AnonymousConstructor.f()??????");
}
};
}
?
public static void main(String[] args) {
Base base = getBase(57);
base.f();
}
}
??? Base Constructor = 57 Base Initialization 57 AnonymousConstructor.f()??????
????? “??????????????????” ?????????????????????????
10.5 ???
10.4 ?????6???????????????????10.1 ?????????contents()?????????????????
??????????????????????????????static??????????????????????????????????????????????????????????????????????????????? ????????? ?1?????????????????????? ?2??????????????????????
???? 10.5-1
public class Parcel10 {
?
private int value = 11;
?
static int bValue = 12;
?
// ?????
private static class PContents implements Contents {
?
// ??????????PContents????value???
@Override
public int getValue() {
return value;
}
?
// ???????????PContents????????bValue
public int f(){
return bValue;
}
}
?
// ?????
private class PDestination implements Destination {
?
@Override
public String readLabel() {
return "label";
}
}
?
// ?????????????????????
public static Contents contents(){
return new PContents();
}
?
// ?????????????????????
public Contents contents2(){
Parcel10 p10 = new Parcel10();
return p10.new PContents();
}
?
// ????????????????????
public static Destination destination(){
Parcel10 p10 = new Parcel10();
return p10.new PDestination();
}
?
// ?????????????????????
public Destination destination2(){
return new PDestination();
}
}
??????????10.1???????? ???????????????????????????????????????
??????
??????????????????????????????? ????????????????????????????????????????????????public?static??????static????????????????????????????????????????????????????????????
public interface InnerInterface {
?
void f();
?
class InnerClass implements InnerInterface {
?
@Override
public void f() {
System.out.println("????????");
}
?
public static void main(String[] args) {
new InnerClass().f();
}
}
?
// ????????main??????????????????
// public static void main(String[] args) {}
}
??? ????????
?????????
?Java???????????????????????????????????????????????????????????????????????????????”????”??????extends???????????????????????????
??chenssy??? http://www.cnblogs.com/chenssy/p/3389027.html ?????????
public class Food {
?
private class InnerFruit implements Fruit{
void meakFruit(){
System.out.println("?????");
}
}
?
private class InnerMeat implements Meat{
void makeMeat(){
System.out.println("????");
}
}
?
public Fruit fruit(){
return new InnerFruit();
}
?
public Meat meat(){
return new InnerMeat();
}
?
public static void main(String[] args) {
Food food = new Food();
InnerFruit innerFruit = (InnerFruit)food.fruit();
innerFruit.meakFruit();
InnerMeat innerMeat = (InnerMeat) food.meat();
innerMeat.makeMeat();
}
}
??? ????? ????
10.6 ??????
?????????????????????????????????
public class BaseClass {
?
class BaseInnerClass {
?
public void f(){
System.out.println("BaseInnerClass.f()");
}
}
?
private void g(){
System.out.println("BaseClass.g()");
}
}
/**
* ?????InheritInner????????BaseInnerClass???????
* ????????????????
* ??????enclosingClassReference.super()??????
* ??????????????????????
*
*/
public class InheritInner extends BaseClass.BaseInnerClass{
?
// ????
// public InheritInner(){}
?
public InheritInner(BaseClass bc){
bc.super();
}
?
@Override
public void f() {
System.out.println("InheritInner.f()");
}
?
/*
* ??@Override ??????BaseInnerClass ???g()??
* ?????????????Override?????????????
* ??????????????????g()?????????
@Override
public void g(){
System.out.println("InheritInner.g()");
}*/
?
public static void main(String[] args) {
BaseClass baseClass = new BaseClass();
InheritInner inheritInner = new InheritInner(baseClass);
inheritInner.f();
}
}
???InheritInner.f()
10.7 ??????
????????????????
public class Man {
?
private ManWithKnowledge man;
?
protected class ManWithKnowledge {
?
public void haveKnowledge(){
System.out.println("??????????");
}
}
?
// ??????????haveKnowledge()??
public Man(){
System.out.println("??????????????????????????????");
new ManWithKnowledge().haveKnowledge();
}
}
?
// ??
public class InternetCelebrity extends Man {
?
protected class ManWithKnowledge {
?
public void haveKnowledge(){
System.out.println("????????????");
}
}
?
public static void main(String[] args) {
new InternetCelebrity();
}
}
????????????????????????????????? ??????????
??????????????????????? InternetCelebrity.haveKnowledge() ,???????????????ManWithKnowledge.haveKnowledge()??? ????????????????????????????????????????????????????????
10.8 ???????????
???????????.class ?????????????????? ?????????????.class ?? ?????: OneClass$OneInnerClass
??: ?????Java????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????get???????????????????????
10.9 ??????
1????????????????????????????????????
2?????????????????????????????????????????
3???????????????????
Java ?????????????? Java ?????????????????????? Java ????????????????????????????????????????????
??????????????????“???”??????? Java ????/???????