Skip to content

Commit 55f7cfb

Browse files
committed
java/se: add inner class sample
1 parent 6d42b14 commit 55f7cfb

File tree

17 files changed

+313
-0
lines changed

17 files changed

+313
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.edu.ntu.javase.syntax.iclass.anonymous;
2+
3+
import java.util.function.Consumer;
4+
5+
/**
6+
* type3: anonymous inner class <br>
7+
* 1. anonymous in known as implements interface as parameter. <br>
8+
* 2. anonymous inner class have all access to outer class. <br>
9+
* 3. outer class have a no access to anonymous inner class: <br>
10+
* - anonymous inner class can define property,and can only used in local, <br>
11+
* - and cannot used in outer class due to no class name.<br>
12+
* 4. even cannot create or get anonymous instance.<br>
13+
* 5. anonymous just define implements and it will not execute besides call interface method.
14+
* <br>
15+
* 6. create anonymous for each interface method call.<br>
16+
*/
17+
interface AnonymousInterface {
18+
void accept(String tag, Consumer consumer);
19+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.edu.ntu.javase.syntax.iclass.anonymous;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
import java.util.function.Consumer;
6+
7+
@Slf4j
8+
public class Test {
9+
10+
int a;
11+
12+
public void anonymousClassTest() {
13+
// this just define implements and it will not execute besides call interface method.
14+
AnonymousInterface anonymousInterface =
15+
new AnonymousInterface() {
16+
// can define property in anonymous class,and can only used in local.
17+
int field = a;
18+
19+
@Override
20+
public void accept(String tag, Consumer consumer) {
21+
consumer.accept(tag);
22+
}
23+
};
24+
25+
// create anonymous for each interface method call.
26+
anonymousInterface.accept("obj1", (Object a) -> log.info(a.toString()));
27+
anonymousInterface.accept("obj3", a -> log.info(a.toString()));
28+
}
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cn.edu.ntu.javase.syntax.iclass.func.callback;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
/**
6+
* @author Zack Zhang
7+
*/
8+
@Slf4j
9+
public class Callee extends MyIncrement {
10+
private int i = 0;
11+
12+
private void incr() {
13+
i++;
14+
log.info("{}", i);
15+
}
16+
17+
private class Closure implements Incrementable {
18+
public void increment() {
19+
incr();
20+
}
21+
}
22+
23+
public Incrementable getCallbackReference() {
24+
return new Closure();
25+
}
26+
27+
public static void main(String[] args) {
28+
Callee callee = new Callee();
29+
callee.increment();
30+
Incrementable callbackReference = callee.getCallbackReference();
31+
callbackReference.increment();
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package cn.edu.ntu.javase.syntax.iclass.func.callback;
2+
3+
public interface Incrementable {
4+
void increment();
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cn.edu.ntu.javase.syntax.iclass.func.callback;
2+
3+
public class MyIncrement {
4+
public void increment() {
5+
System.out.println("Other increment()");
6+
}
7+
8+
static void f(MyIncrement f) {
9+
f.increment();
10+
}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package cn.edu.ntu.javase.syntax.iclass.func.inherit;
2+
3+
public class Example1 {
4+
public String name() {
5+
return "longjiazuo";
6+
}
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package cn.edu.ntu.javase.syntax.iclass.func.inherit;
2+
3+
public class Example2 {
4+
public String address() {
5+
return "longjiazuo";
6+
}
7+
8+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cn.edu.ntu.javase.syntax.iclass.func.inherit;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
@Slf4j
6+
@Deprecated
7+
public class MoreInherit {
8+
private class test1 extends Example1 {
9+
public String name() {
10+
return super.name();
11+
}
12+
}
13+
14+
private class test2 extends Example2 {
15+
public String address() {
16+
return super.address();
17+
}
18+
}
19+
20+
public String name() {
21+
return new test1().name();
22+
}
23+
24+
public String age() {
25+
return new test2().address();
26+
}
27+
28+
public static void main(String args[]) {
29+
MoreInherit mi = new MoreInherit();
30+
log.info("姓名:" + mi.name());
31+
log.info("地址:" + mi.age());
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.edu.ntu.javase.syntax.iclass.func.inherit;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
@Slf4j
6+
public class MoreInheritV2 {
7+
8+
private Example1 e1 = new Example1();
9+
private Example2 e2 = new Example2();
10+
11+
public String name() {
12+
return e1.name();
13+
}
14+
15+
public String age() {
16+
return e2.address();
17+
}
18+
19+
public static void main(String args[]) {
20+
MoreInheritV2 mi = new MoreInheritV2();
21+
log.info("姓名:" + mi.name());
22+
log.info("地址:" + mi.age());
23+
}
24+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cn.edu.ntu.javase.syntax.iclass.func.oop;
2+
3+
public class Example {
4+
private class InsideClass implements Interface {
5+
public void test() {
6+
System.out.println("这是一个测试");
7+
}
8+
}
9+
10+
public Interface getIn() {
11+
return new InsideClass();
12+
}
13+
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package cn.edu.ntu.javase.syntax.iclass.func.oop;
2+
3+
public interface Interface {
4+
void test();
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cn.edu.ntu.javase.syntax.iclass.func.oop;
2+
3+
public class TestFunc {
4+
public static void main(String args[]) {
5+
Example a = new Example();
6+
Interface a1 = a.getIn();
7+
a1.test();
8+
}
9+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cn.edu.ntu.javase.syntax.iclass.innerc;
2+
3+
import lombok.Data;
4+
import lombok.extern.slf4j.Slf4j;
5+
6+
@Slf4j
7+
@Data
8+
public class OuterClass {
9+
10+
int a = 1;
11+
private int op;
12+
private static int sop;
13+
private InnerClass innerClass = new InnerClass();
14+
15+
16+
public class InnerClass {
17+
// inner class cannot contains static properties.
18+
// private static int p;
19+
int a = 2;
20+
private int ip;
21+
22+
public void accessOp() {
23+
log.info("out class properties: {}-{}", op, sop);
24+
}
25+
26+
public void method() {
27+
int a = 3;
28+
// 访问方法内部变量
29+
log.info("{}", a);
30+
// 访问内部类的成员变量
31+
log.info("{}", this.a);
32+
// 访问外部内的成员变量
33+
log.info("{}", OuterClass.this.a);
34+
}
35+
}
36+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cn.edu.ntu.javase.syntax.iclass.innerc;
2+
3+
public class Test {
4+
5+
public static void main(String[] args) {
6+
7+
OuterClass.InnerClass inner = new OuterClass().new InnerClass();
8+
inner.accessOp();
9+
}
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cn.edu.ntu.javase.syntax.iclass.localc;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
5+
@Slf4j
6+
public class OuterClass {
7+
public int op;
8+
9+
public void getXx() {
10+
int mp = 1;
11+
12+
final class LocalInner {
13+
public int ip;
14+
15+
public void getX() {
16+
log.info("{}-{}-{}", op, mp, ip);
17+
}
18+
}
19+
}
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cn.edu.ntu.javase.syntax.iclass.staticc;
2+
3+
import lombok.Data;
4+
import lombok.extern.slf4j.Slf4j;
5+
6+
@Slf4j
7+
public class OuterClass {
8+
int a = 1;
9+
private int op;
10+
private static int sop;
11+
private InnerClass innerClass = new InnerClass();
12+
13+
@Data
14+
public static class InnerClass {
15+
int a = 2;
16+
17+
static int sp;
18+
19+
private int ip;
20+
21+
public void accessOp() {
22+
log.info("out class properties: {}", sop);
23+
}
24+
25+
public void method() {
26+
27+
int a = 3;
28+
// 访问方法内部变量
29+
log.info("{}", a);
30+
// 访问内部类的成员变量
31+
log.info("{}", this.a);
32+
// 访问外部内的成员变量: error
33+
// log.info("{}", OuterClass.this.a);
34+
}
35+
}
36+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.edu.ntu.javase.syntax.iclass.staticc;
2+
3+
4+
import lombok.val;
5+
6+
public class Test {
7+
public static void main(String[] args) {
8+
val a = OuterClass.InnerClass.sp;
9+
OuterClass.InnerClass inner = new OuterClass.InnerClass();
10+
inner.accessOp();
11+
int ip = inner.getIp();
12+
}
13+
}

0 commit comments

Comments
 (0)