Skip to content

Commit 98b2b35

Browse files
author
turingfly
committed
Nested classes
1 parent d6e3952 commit 98b2b35

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package advancedClassDesign;
2+
3+
/**
4+
*
5+
* @author chengfeili
6+
* Jun 2, 2017 10:21:31 PM
7+
*
8+
* An anonymous inner class is a local inner class that does not have a
9+
* name. It is declared and instantiated all in on statement using the
10+
* new keyword. Anonymous inner classes are required to extend an
11+
* existing class or implement an existing interface.
12+
*
13+
* You can define them right where they are needed,
14+
* even if that is an argument to another method.
15+
*/
16+
public class AnonymousInnerClass {
17+
// abstract class
18+
abstract class SaleTodayOnly {
19+
abstract int dollarOff();
20+
}
21+
22+
public int admission(int basePrice) {
23+
SaleTodayOnly sale = new SaleTodayOnly() {
24+
25+
@Override
26+
int dollarOff() {
27+
// TODO Auto-generated method stub
28+
return 3;
29+
}
30+
};
31+
return basePrice - sale.dollarOff();
32+
}
33+
34+
// interface
35+
interface SaleTodayOnly2 {
36+
int dollarOff();
37+
}
38+
public int admission2(int basePrice) {
39+
SaleTodayOnly2 sale = new SaleTodayOnly2() {
40+
41+
// interface requires public mehtods
42+
@Override
43+
public int dollarOff() {
44+
// TODO Auto-generated method stub
45+
return 0;
46+
}
47+
};
48+
return basePrice - sale.dollarOff();
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package advancedClassDesign;
2+
3+
/**
4+
*
5+
* @author chengfeili
6+
* Jun 2, 2017 9:54:47 PM
7+
* Local inner class is a nested class defined within a method.
8+
*
9+
* Properties:
10+
* 1. They do not have an access specifier
11+
* 2. They cannot be declared static and cannot declare static fields or methods
12+
* 3. They have access to all fields and methods of the enclosing class
13+
* 4. They do not have access to local variables of a method unless those
14+
* variables are final or effectively final(only set in the line in which it is declared).
15+
*/
16+
public class LocalInnerClass {
17+
private int length = 5;
18+
19+
public void calculate() {
20+
final int width = 20;
21+
class Inner {
22+
public void multily() {
23+
System.out.println(length * width);
24+
}
25+
}
26+
Inner inner = new Inner();
27+
inner.multily();
28+
}
29+
30+
public static void main(String[] args) {
31+
LocalInnerClass outer = new LocalInnerClass();
32+
outer.calculate();
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package advancedClassDesign;
2+
3+
/**
4+
*
5+
* @author chengfeili
6+
* Jun 2, 2017 9:18:54 PM
7+
*
8+
* Member Inner Classes: It is defined at the member level of a
9+
* class(the same level as the methods, instance variables, and
10+
* constructors).
11+
*
12+
* Properties:
13+
* 1. Can be declared public, protected, default and private
14+
* 2. Can extend any class and implement interfaces
15+
* 3. Can be abstract or final
16+
* 4. Cannot declare static fields or methods
17+
* 5. Can access
18+
* members of the outer class including private members
19+
*
20+
*/
21+
public class MemberInnerClass {
22+
private String greeting = "Outer";
23+
24+
protected class Inner {
25+
public int repeat = 3;
26+
private String greeting = "Inner";
27+
28+
public void go() {
29+
for (int i = 0; i < repeat; i++)
30+
System.out.println(greeting);
31+
}
32+
33+
public void sameName() {
34+
System.out.println(greeting); // Inner
35+
System.out.println(this.greeting); // Inner
36+
System.out.println(MemberInnerClass.this.greeting); // Outer
37+
}
38+
}
39+
40+
public void callInner() {
41+
Inner inner = new Inner();
42+
inner.go();
43+
}
44+
45+
public static void main(String[] args) {
46+
MemberInnerClass outer = new MemberInnerClass();
47+
outer.callInner();
48+
/**
49+
* another way to instantiate inner We need an instance of Outer in
50+
* order to create Inner. We can't just call new Inner() because Java
51+
* won't know which instance of Outer it is associated.
52+
*/
53+
MemberInnerClass outer2 = new MemberInnerClass();
54+
Inner inner = outer2.new Inner();
55+
inner.go();
56+
inner.sameName();
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package advancedClassDesign;
2+
3+
/**
4+
*
5+
* @author chengfeili
6+
* Jun 2, 2017 10:50:49 PM
7+
* A static nested class is a static
8+
* class defined at the member level. It can be instantiated without an
9+
* object of the enclosing class, so it can't access the instance
10+
* variables without an explicit object of the enclosing class.
11+
* 1. The nesting creates a name space because the enclosing class name must be
12+
* used to refer to it
13+
* 2. It can be made private or use one of the other
14+
* access modifiers to encapsulate it.
15+
* 3. The enclosing class can refer
16+
* to the fields and methods of the static nested class.
17+
*/
18+
public class StaticNestedClass {
19+
static class Nested {
20+
private int price = 6;
21+
}
22+
23+
public static void main(String[] args) {
24+
Nested nested = new Nested();
25+
System.out.println(nested.price);
26+
}
27+
}

0 commit comments

Comments
 (0)