-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef810db
commit 6a58dd0
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.betterzhang.learnkotlin.java; | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* Author : Andrew Zhang | ||
* Email : betterzhang.dev@gmail.com | ||
* Time : 2017/06/28 上午 10:42 | ||
* Desc : 静态与非静态内部类 | ||
*/ | ||
public final class Outer { | ||
|
||
private int bar = 100; | ||
|
||
public static class Nested { | ||
public final String foo() { | ||
return "Hello Java"; | ||
} | ||
} | ||
|
||
public final class Inner { | ||
public final int foo() { | ||
return bar; // 可以访问外部类成员 | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println(new Outer.Nested().foo()); | ||
System.out.println(new Nested().foo()); | ||
// System.out.println(new Outer().Inner().foo()); // 不能在其他类中实例化非静态内部类 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.betterzhang.learnkotlin.kotlin | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* Author : Andrew Zhang | ||
* Email : betterzhang.dev@gmail.com | ||
* Time : 2017/06/28 上午 10:50 | ||
* Desc : description | ||
*/ | ||
class Outer { | ||
|
||
private var bar: Int = 100 | ||
|
||
// 嵌套类 | ||
class Nested { | ||
// 不能访问外部类成员 | ||
fun foo() = "Hello Kotlin" | ||
} | ||
|
||
// 内部类 | ||
inner class Inner { | ||
// 可以访问外部类成员 | ||
fun foo() = bar | ||
} | ||
|
||
} | ||
|
||
fun main(args: Array<String>) { | ||
println(Outer.Nested().foo()) | ||
println(Outer().Inner().foo()) | ||
} |