class NestClass{
static class StaticInner{
void fun(){
System.out.println( "static class StaticInner 原始的方法");
}
}
class Inner{
void fun(){
System.out.println( "none static class Inner 原始的方法");
}
}
Inner i;
public static void main(String[] args){
//==============================================================
NestClass.StaticInner n0=new NestClass.StaticInner();
n0.fun();
//==============================================================
NestClass.StaticInner n1=new NestClass.StaticInner(){//匿名類別開始
void fun(){
System.out.println( "static class StaticInner 被覆寫的方法");
}
};//匿名類別結束
n1.fun();
//==============================================================
NestClass n2=new NestClass();
n2.i=n2.new Inner();
n2.i.fun();
//==============================================================
NestClass n3=new NestClass();
n3.i=n3.new Inner(){//匿名類別開始
void fun(){
System.out.println( "none static class Inner 被覆寫的方法");
}
};//匿名類別結束
n3.i.fun();
}
}
//================================================
class A{}
class NestClass2{
public static void main(String[] args){
A a=new A(int I){};//錯誤建立新的建構子
}
}
/*
None static nest class 都要同時使用外部及內部class實體
使用範例:外部實體.內部實體.成員
匿名類別不能新增建構子
匿名類別在Java8.0前只能使用final 外部區域變數
Java8.0(含)後,匿名類別所參照到外部區域變數,會變為隱性final
*/
2008年2月2日 星期六
None static Nest class不能有static member
class Outer4{
class Inner4{
static private String InnerStr="";//錯誤
static public void InnerFun(){}//錯誤
}
Inner4 inner;
public static void main(String[] arg){}
}
/*
none static nest class 一定要依附外部class實體而存在,不能單獨產生
*/
class Inner4{
static private String InnerStr="";//錯誤
static public void InnerFun(){}//錯誤
}
Inner4 inner;
public static void main(String[] arg){}
}
/*
none static nest class 一定要依附外部class實體而存在,不能單獨產生
*/
None static Nest class 建立時使用 外部實體.new方式建立
class Outer3{
class Inner3{
public void InnerFun(){
System.out.println("none static nest class have a none static member =>innerFun" );
}
}
Inner3 inner;
public static void main(String[] arg){
Outer3 outer=new Outer3();
outer.inner=outer.new Inner3();
outer.inner.InnerFun();
}
}
class Inner3{
public void InnerFun(){
System.out.println("none static nest class have a none static member =>innerFun" );
}
}
Inner3 inner;
public static void main(String[] arg){
Outer3 outer=new Outer3();
outer.inner=outer.new Inner3();
outer.inner.InnerFun();
}
}
Static nest Class 擁有None static Member需建立nest class 實體
class Outer2{
static class Inner2{
private String InnerStr="Inner attrib";
public void InnerFun(){
System.out.println( "static nest class have a none" +" static member =>innerFun" );
}
}
public static void main(String[] arg){
Outer2.Inner2 inner=new Outer2.Inner2();
System.out.println(inner.InnerStr);
inner.InnerFun();
//System.out.println(Outer2.Inner2.InnerStr);//錯誤
//Outer2.Inner2.InnerFun();//錯誤
}
}
static class Inner2{
private String InnerStr="Inner attrib";
public void InnerFun(){
System.out.println( "static nest class have a none" +" static member =>innerFun" );
}
}
public static void main(String[] arg){
Outer2.Inner2 inner=new Outer2.Inner2();
System.out.println(inner.InnerStr);
inner.InnerFun();
//System.out.println(Outer2.Inner2.InnerStr);//錯誤
//Outer2.Inner2.InnerFun();//錯誤
}
}
Nest Interface在member attrib視為static Nest type attrib而member method則須經繼承後才可使用
class NestInterface{
interface I{
String STR="nest interface member attrib";
void fun();
}
public static void main(String[] args){
System.out.println(NestInterface.I.STR);
//NestInterface.I.fun();
//^==不能使用因fun()尚未定義
C c=new C();
c.fun();
}
}
class C implements NestInterface.I{
public void fun(){
System.out.println( "nest interface member method");
}
}
//===================================
interface NestInterface2{
interface I{
String STR="nest interface member attrib";
void fun();
}
}
class C implements NestInterface2.I{
public void fun(){
System.out.println( "nest interface member method");
}
public static void main(String[] args){
System.out.println(NestInterface2.I.STR);
C c=new C();
c.fun();
}
}
interface I{
String STR="nest interface member attrib";
void fun();
}
public static void main(String[] args){
System.out.println(NestInterface.I.STR);
//NestInterface.I.fun();
//^==不能使用因fun()尚未定義
C c=new C();
c.fun();
}
}
class C implements NestInterface.I{
public void fun(){
System.out.println( "nest interface member method");
}
}
//===================================
interface NestInterface2{
interface I{
String STR="nest interface member attrib";
void fun();
}
}
class C implements NestInterface2.I{
public void fun(){
System.out.println( "nest interface member method");
}
public static void main(String[] args){
System.out.println(NestInterface2.I.STR);
C c=new C();
c.fun();
}
}
巢狀型別與外部型別存有特殊權限關係
//巢狀型別與外部型別共享一信任關係可互相存取至private權限的成員
class Outer{
static private String outerStr="Outer attrib";
static public void outerFun(){
System.out.println ("outerFun use "+Inner.InnerStr);
}
static class Inner{
static private String InnerStr= "Inner attrib";
static public void InnerFun(){
System.out.println ("innerFun use "+outerStr);
}
}
public static void main(String[] arg)
{
Outer.outerFun();
Outer.Inner.InnerFun();
}
}
class Outer{
static private String outerStr="Outer attrib";
static public void outerFun(){
System.out.println ("outerFun use "+Inner.InnerStr);
}
static class Inner{
static private String InnerStr= "Inner attrib";
static public void InnerFun(){
System.out.println ("innerFun use "+outerStr);
}
}
public static void main(String[] arg)
{
Outer.outerFun();
Outer.Inner.InnerFun();
}
}
巢狀型別分為三類六種型態
第一類:為static NestType
(1)
class{
static class{}
}
(2)
class{
interface{}
}
(3)
interface{
class{}
}
(4)
interface{
interface{}
}
第二類:為none-static NestType
class{
class{}
}
第三類:為local 變數 class
fun (){
class{}
}
interface 之成員屬性,預設為public final static 成員方法預設為public 故內部型別為interface時屬性視為常數,但方法需經繼承後實作才可使用.
Static Nest Type則可直接是為外部型別的成員,方式=> OuterClass.NestType.Member
None-static Nest Type (又稱Inner class)不能直接使用,它與instance有關,需new之後才可藉由參考變數使用
(1)
class{
static class{}
}
(2)
class{
interface{}
}
(3)
interface{
class{}
}
(4)
interface{
interface{}
}
第二類:為none-static NestType
class{
class{}
}
第三類:為local 變數 class
fun (){
class{}
}
interface 之成員屬性,預設為public final static 成員方法預設為public 故內部型別為interface時屬性視為常數,但方法需經繼承後實作才可使用.
Static Nest Type則可直接是為外部型別的成員,方式=> OuterClass.NestType.Member
None-static Nest Type (又稱Inner class)不能直接使用,它與instance有關,需new之後才可藉由參考變數使用
訂閱:
文章 (Atom)