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
Lines changed: 19 additions & 0 deletions
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+
}
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.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+
}

0 commit comments

Comments
 (0)