/** 多層嵌套內部類, 調用時要層層往下調用 格式: 外部類.內部類1.內部類2 對象名 = new 外部類().new 內部類1().new 內部類2(); 對象名.屬性/方法名(); */ class Outer { public void oSay() { System.out.println("這是 outer 方法"); } public class Inter1 { public void iSay() { System.out.println("這是 inter1 方法"); } public class Inter2 { public void say() { System.out.println("這是 inter2 的 say method"); } } } } public class Hi { public static void main(String[] args) { Outer.Inter1.Inter2 oii = new Outer().new Inter1().new Inter2(); oii.say(); } } // ============= /** 實例內部類 Inter與外部類 Outer有着同名的成員變量i,則在內部類 Inter中, i,this.i和 Inter.this.i都表示爲 Inter類的成員i;而 Outer.this.i才表示外部類 Outer 的成員 */ class Outer { int i = 10; public class Inter { int i = 20; public void getOuterI() { System.out.println("i表示爲 Inter中的 i 值: "+i); // 20 System.out.println("this.i表示爲 Inter中的 i 值;"+this.i); // 20 System.out.println("Inter.this.i表示爲 Inter 中 i 的值:"+Inter.this.i); // 20 System.out.println("Outer.this.i表示爲 Outer中的 i 值:"+Outer.this.i); // 10 } } } public class Hi { public static void main(String[] args) { Outer.Inter oi = new Outer().new Inter(); oi.getOuterI(); } } // ============= /** 靜態內部類建立對象實例 外部類.靜態內部類 靜態內部類對象 = new 外部類.靜態內部類(); */ class Outer { private int a = 4; // 定義實例屬性 a private static int b = 5; // 定義靜態屬性 b public static class Inter // 定義靜態內部類 { private int x = 100; // 定義實例屬性 x private static int y = 200; // 定義靜態屬性 y public void add() // 定義實例方法 { // 經過外部類的實例對象調用外部類的非靜態成員 int temp = new Outer().a; System.out.println(temp+"+"+x+"="+(x+temp)); } public static void mul() // 定義靜態方法 { // 直接調用外部類的靜態成員 System.out.println(b+"+"+y+"="+(b+y)); } } public void getInfo() { // 經過內部類對象訪問靜態內部類的非靜態方法 new Inter().add(); // 直接經過"靜態內部類.靜態方法"形式訪問靜態內部類的靜態方法 Inter.mul(); } } public class Hi { public static void main(String[] args) { Outer out = new Outer(); // 建立外部類實例 out.getInfo(); Outer.Inter in = new Outer.Inter(); // 建立內部類實例 in.add(); in.mul(); } } /* 4+100=104 5+200=205 4+100=104 5+200=205 */ // ============= /* 局部內部類: 是指定義在方法體內的內部類,局部內部類僅在該方法裏有效,所以, 局部內部類不能被外部類和外部類之外的其它類訪問,因此局部內部類 不須要訪問控制符和 static 修飾符的 */ class Outer { public void fun() { class Inter { public int i = 90; } Inter in = new Inter(); System.out.println(in.i); } } public class Hi { public static void main(String[] args) { Outer out = new Outer(); out.fun(); } } // ============= /* 局部內部類: 是指定義在方法體內的內部類,局部內部類僅在該方法裏有效,所以, 局部內部類不能被外部類和外部類之外的其它類訪問,因此局部內部類 不須要訪問控制符和 static 修飾符的 */ class Outer { public void fun() { class Inter // 定義局部內部類 { public int i = 90; } Inter in = new Inter(); // 建立局部內部類的實例 System.out.println(in.i); } } public class Hi { public static void main(String[] args) { Outer out = new Outer(); // 建立外部類實例 out.fun(); } } // ============ /* 局部內部類能夠訪問外部類的全部成員,包括私有成員。 若局部內部類定義在靜態方法體內時,局部內部類不能直接訪問外部類的非靜態成員, 可是能夠訪問外部類的靜態成員。若須要調用外部類的非靜態成員時, 能夠經過外部類的實例。 */ class Outer { private float f = 1.0F; //實例屬性 private static int x = 90; // 靜態屬性 public void fun1() // 實例方法 { class Inter1 // 定義局部內部類 { private float interF = 4.0F; public Inter1() // 構造方法 { System.out.println(f+"+"+interF+"="+(f+interF)); } } new Inter1(); //匿名對象 } public static void fun2() // 靜態方法 { class Inter2 { private int i = 10; public Inter2() { System.out.println(i+"+"+x+"="+(i+x)); float temp = new Outer().f; System.out.println(i+"+"+temp+"="+(i+temp)); } } new Inter2(); } } public class Hi { public static void main(String[] args) { Outer out = new Outer(); out.fun1(); out.fun2(); } } // ========== /* 匿名內部類是指在定義時沒有名稱的內部類,必須在聲明時候時使用 new 主義聲明類. 匿名內部類是一種特殊的內部類,除了具備普通內部類的特色之外,還有本身的一些特色。 匿名內部類通常只使用一次 格式: new <類/接口>([參數列表]){ ...... } 雖然匿名內部類沒有類名,匿名內部類必須擴展一個基類或實現一個接口,但不要顯式地 使用 extends 或 implements 關鍵字。若是匿名內部類繼承抽象類或實現接口時,還要實現 父類及接口中全部的抽象方法。 */ // 如下的匿名內部類繼承了抽象類 abstract class AbstractClass // 定義抽象類 { public abstract void getInfo(); // 聲明抽象方法 } class Inter { public void print() { show(new AbstractClass(){ // 匿名內部類 public void getInfo() { System.out.println("Hi,lin3615"); } }); } public void show(AbstractClass a) // 傳入抽象類實例 { a.getInfo(); // 調用抽象類方法 } } public class Hi { public static void main(String[] args) { new Inter().print(); // 建立 Inter類的實例並調用 print方法 } } // ============ //定義類 訪問控制符 [修飾符] class 類名 { 訪問控制符 [修飾符] 數據類型 屬性名; .....; 訪問控制符 [修飾符] 數據類型 方法名(參數列表) { } } 訪問控制符:主要有 默認控制符(無關鍵詞),public 修飾符: static final abstract ... // 建立對象 // 格式1 類名 對象名=null; 對象名 = new 類名(); // 格式2 類名 對象名 = new 類名(); class Person { String name; int age; public void say() { System.out.println("my name is "+name+",age is "+age); } } public class Hi { public static void main(String[] args) { Person p1 = null; p1 = new Person(); Person p2 = new Person(); Person p3 = p2; p1.name="小二"; p1.age = 26; p1.say(); // 輸出 } } // ================= // ------------- class Person { private String name; private int age; public void setName(String nn) { this.name = nn; } public void setAge(int aa) { this.age = aa; } public void say() { System.out.println("my name is "+name+",age is "+age); } } public class Hi { public static void main(String[] args) { Person p1 = null; p1 = new Person(); p1.setName("lin3615"); p1.setAge(26); p1.say(); } } // ==================== 匿名對象:指沒有棧空間的對象,即沒有沒有明顯給出名字的對象。 匿名對象使用的是堆內存,是經過關鍵詞new進行開闢的,只能使用一次 class Person { private String name; private int age; public Person(String s, int a) { this.name =s; this.age = a; } public void setName(String nn) { this.name = nn; } public void setAge(int aa) { this.age = aa; } public void say() { System.out.println("my name is "+name+",age is "+age); } } public class Hi { public static void main(String[] args) { Person p1 = new Person("lin", 26); p1.say(); } } // 構造函數,也可重載,若是沒有顯示聲明,則調用系統默認的,(是一個的無參方法) 構造方法與類名一致 不能有任何返回值的類型聲明,包括 void 不能使用return 不能被static,final,abstract,native和synchronized 修飾 // 建立構造方法: 格式1: 訪問控制符 類名() { // } 格式2: 訪問控制符 類名(參數列表) { // } // static 靜態,java不沒有傳統意義上的全局,因此可用此表示全局 public class Hi { public static int ticket = 10; //定義靜態變量 public void sell() // 定義sell()方法 { ticket--; System.out.println("剩下 "+ticket); } public static void left() // 定義靜態方法 left() { int i = ticket-2; System.out.println("還剩下:"+i); } public static void main(String[] args) { int x = ticket--; // 直接訪問 ticket System.out.println("x = "+x+", ticket = "+ticket); int y = Hi.ticket--; // 經過類名訪問 System.out.println("y = "+y+" ticket = "+ticket); Hi str = new Hi(); str.left(); // 經過非靜態訪問 int k = str.ticket--; // 經過實例訪問 System.out.println("k = "+k+", ticket = "+ticket); System.out.println(Hi.ticket); // 經過類訪問 left(); // 經過靜態訪問 Hi.left(); // 經過非靜態訪問 } } /* x = 10, ticket = 9 y = 9 ticket = 8 還剩下:6 k = 8, ticket = 7 7 還剩下:5 還剩下:5 */ // ======================== public class Hi { { // begin 構造塊 System.out.println("構造塊1"); } // end 構造塊 { // begin 構造塊 System.out.println("構造塊2"); } // end 構造塊 public Hi() { System.out.println("構造方法"); } public static void main(String[] args) { new Hi(); new Hi(); new Hi(); } } /* 構造塊1 構造塊2 構造方法 構造塊1 構造塊2 構造方法 構造塊1 構造塊2 構造方法 */ //==================== // 靜態代碼塊只會執行一次 class Code { { System.out.println("code的構造塊"); // 構造代碼塊 4 } static { System.out.println("code 的靜態代碼塊"); // 靜態代碼塊 3 } public Code() { System.out.println("code 的構造方法普通代碼塊"); // 構造方法 } } public class Hi { { System.out.println("Hi 的構造塊"); // 構造代碼塊 } static { System.out.println("Hi 的靜態代碼塊"); // 靜態代碼塊 1 } public Hi() { System.out.println("hi 的構造方法普通塊"); // 構造方法 } public static void main(String[] args) { System.out.println("Hi 的主體方法"); //2 new Code(); new Code(); new Hi(); new Hi(); } } /** 靜態代碼塊只會執行一次,不管在一次中,實例多少都是一次 Hi 的靜態代碼塊 Hi 的主體方法 code 的靜態代碼塊 code的構造塊 code 的構造方法普通代碼塊 code的構造塊 code 的構造方法普通代碼塊 Hi 的構造塊 hi 的構造方法普通塊 Hi 的構造塊 hi 的構造方法普通塊 */ // ============ // 使用 package 定義包 // 定義包就是將聲明的類放入包中,爲包指定包名,格式以下: // package 包名1[.子包名1.子包名2....]; package org.com.pack; public class Hi { public static void main(String[] args) { System.out.println("定義包"); } } // package 語句必須是程序中可執行的第一行代碼 // package 語句只能有一句 // 在前面的包名是 後面的包名 的父目錄 // 沒有 package 語句,則默認無包名 // 用javac 編譯,編譯以下: // javac -d . Hi.java // -d 表示生成目錄. . 表示目錄的位置,此表示生成爲當前目錄,最終會生成 org/com/pack/Hi.class // 執行時用 java 包路徑.類名: java org.com.pack.Hi // 當定義包後,同一個包中的類是默認導入的,若是一個類訪問來自另外一個包中的類時, // 前者必須經過 import 語句把 後者導入才能使用。 // import 語法格式 // 格式1:import 包名[.子包名...].類名; 只會導入當前的類 // 格式2: import 包名[.子包名 ...].*; 會導入包中全部的類,但不會導入其子包中的類,要用到時還得手動導入 // =================== // 創建 Student.java, Student.class 位於 org.com.pack.stu中 package org.com.pack.stu; public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { setName(name); setAge(age); } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public String getName() { return this.name; } public int getAge() { return this.age; } public void show() { System.out.println("my name is "+this.name+" and age is "+this.age); } } // 創建 Hi.java, 生成的類位於包 org.com.pack.pack中 package org.com.pack.pack; import org.com.pack.stu.Student; public class Hi { public static void main(String[] args) { Student stu = new Student("lin3615", 26); stu.show(); } } // javac -d . 文件1.java 文件2.java ...能夠鏈接多個一塊兒編譯 // javac -d . Student.java Hi.java // java org.com.pack.pack.Hi // ===================== // 靜態導入 // Student.java package org.com.pack.area; public class Student { public static double PI = 3.14; public static double round(int r) { return PI*r*r; } public static int square(int a) { return a*a; } } // Hi.java package org.com.pack.pack; import static org.com.pack.area.Student.*; public class Hi { public static void main(String[] args) { System.out.println("PI = "+PI); System.out.println("ROUND = "+round(10)); System.out.println("square = "+square(10)); } } // javac -d . Student.java Hi.java // java org.com.pack.pack.Hi // 手動裝箱/拆箱 public class Hi { public static void main(String[] args) { int x = 100; Integer in = new Integer(x); // 手動裝箱 System.out.println(in); Float f = new Float(3.14F); float y = f.floatValue(); // 手動拆箱 System.out.println(y); } } // =================== // ----------- public class Hi { public static void main(String[] args) { int x = 100; Integer in = x; // 自動裝箱 System.out.println(in); Float f = 10.98F; float y = f; // 自動拆箱 System.out.println(f); } } // 轉換成字符串,用封裝中的 toString() 方法 public class Hi { public static void main(String[] args) { int x = 100; Integer in = new Integer(x); String ii = in.toString(); Float f = new Float(10.90F); String ff = f.toString(); Double d = new Double(89.76D); String dd = d.toString(); System.out.println(ii+","+ff+","+dd); } } // ================ // 字符轉爲數值 parseXxx(string str)/ parseXxx(String str, int i),其中 Xxx: Int,Double ... public class Hi { public static void main(String[] args) { String ii = "333"; String dd = "1.99"; int i = Integer.parseInt(ii); // 字符串轉爲整型 double d = Double.parseDouble(dd); // 字符串轉爲浮點 System.out.println(i*d); } } // =================== // -------------- // final 變量,屬性,引用,其中引用的屬性能夠改變 public class Hi { final float PI = 3.14f; final int arrInt[] = {1,2,3,4}; final int num; final String str; { num = 100; } public Hi() { str = "lin3615"; } public static void main(String[] args) { final char c = 'A'; Hi p = new Hi(); System.out.println("c = "+c); System.out.println("PI = "+p.PI); System.out.println("num = "+p.num); System.out.println("Str = "+p.str); for(int i=0; i<4; i++) { System.out.print(p.arrInt[i]+" "); } System.out.println(); for(int j = 0; j< 4; j++) { p.arrInt[j] = p.arrInt[j] * 10; } for(int j = 0; j< 4; j++) { System.out.print(p.arrInt[j]+" "); } } } // ================ // ------------ // final 方法不可繼承,但可重寫 class Base { public final void add(int x, int y) { System.out.println("x+y = "+(x+y)); } } class Sub extends Base { public final void add(int x, int y, int z) { System.out.println("x+y+z = "+(x+y+z)); } } public class Hi { public static void main(String[] args) { Sub sub = new Sub(); sub.add(1,2); sub.add(1,2,3); } } // ============== // 若是子類沒有繼承父類的抽象方法,則報錯 abstract class Person { public static final String contry = "China"; private String name; public void setName(String name) { this.name = name; } public String getName() { return this.name; } public abstract void tell(); // public abstract void say(); } class Student extends Person { public void tell() { System.out.println("my name "+this.getName()+", and "+this.contry); } } public class Hi { public static void main(String[] args) { Student stu = new Student(); stu.setName("lin3615"); stu.tell(); } } // ============= // 調用抽象類中的構造方法,隱含 super(); abstract class Person { public Person() { System.out.println("這是父類的構造方法"); } } class Student extends Person { public Student() { System.out.println("這是 student 方法"); } } public class Hi { public static void main(String[] args) { Student stu = new Student(); } } // 這是父類的構造方法 // 這是 student 方法 // ============== // 顯示用 supper() abstract class Shape { private float width; private float high; public Shape() { } public Shape(float width, float high) { this.width = width; this.high = high; } public void setWidth(float width) { this.width = width; } public float getWidth() { return this.width; } public void setHigh(float high) { this.high = high; } public float getHigh() { return this.high; } public abstract void area(); public abstract void cir(); } class Rectangle extends Shape { public Rectangle() { } public Rectangle(float width, float high) { super(width, high); } public void area() { System.out.println(this.getWidth() * this.getHigh()); } public void cir() { System.out.println(this.getWidth() * this.getWidth()); } } public class Hi { public static void main(String[] args) { Rectangle rec = new Rectangle(1.8f,2.1f); rec.area(); rec.cir(); } } // -============ // 接口 /** 格式 [public] interface 接口名稱 [extends 父接口1,父接口2,..] { [public static final ] 數據類型 變量名 = 初值; // 默認就是 public static final [public abstract] [native ] 返回類型 方法體([參數列表]); // 默認就是 public abstract } */ interface InterShape { public static final float PI = 3.14F; public abstract void getArea(); public abstract void getCir(); } class Round implements InterShape { private float radius; public Round() { } public Round(float radius) { this.radius = radius; } public void setRound(float radius) { this.radius = radius; } public float getRound() { return this.radius; } public void getArea() { System.out.println(this.getRound() * InterShape.PI); } public void getCir() { } } public class Hi { public static void main(String[] args) { Round rr = new Round(3.9F); rr.getArea(); } } // =============== // 抽象類實現接口 interface InterShape { public static final float PI = 3.14f; public abstract void getArea(); public abstract void getCir(); } abstract class Sphere implements InterShape { public abstract void getVolume(); } class Round extends Sphere { private float radius; public Round() { } public float getRadius() { return this.radius; } public Round(float radius) { this.radius = radius; } public void getArea() { System.out.println(InterShape.PI*this.getRadius()); } public void getCir() { System.out.println(InterShape.PI+this.getRadius()); } public void getVolume() { System.out.println(InterShape.PI-this.getRadius()); } } public class Hi { public static void main(String[] args) { Round rr = new Round(10.88f); rr.getArea(); } } // ================ // 接口多重繼承 // [public] interface 子接口 extends 父接口A[,父接口B....]{} interface Ok { public static final float PI = 3.14f; } interface InterShape { public abstract void getArea(); } interface InterTwo extends Ok,InterShape { public abstract void getCir(); } abstract class Sphere implements InterTwo { public abstract void getVolume(); } class Round extends Sphere { private float radius; public Round() { } public float getRadius() { return this.radius; } public Round(float radius) { this.radius = radius; } public void getArea() { System.out.println(InterTwo.PI*this.getRadius()); } public void getCir() { System.out.println(InterTwo.PI+this.getRadius()); } public void getVolume() { System.out.println(InterTwo.PI-this.getRadius()); } } public class Hi { public static void main(String[] args) { Round rr = new Round(10.88f); rr.getArea(); } } // ============ // 對象數組 // 類名 對象數組名[] = new 類名[數組長度] // 動態初始化 class School { private String name; public School(){} public School(String name) { setName(name); } public void setName(String name) { this.name = name; } public String getName() { return this.name; } } public class Hi { public static void main(String[] args) { School school[] = new School[4]; school[0] = new School("一加大學"); school[1] = new School("二個大學"); school[2] = new School("三個大學"); school[3] = new School("aaaaa"); for(School a:school) { System.out.print(a.getName()+" "); } } } // ================ // 靜態初始化 class School { private String name; public School(){} public School(String name) { setName(name); } public void setName(String name) { this.name = name; } public String getName() { return this.name; } } public class Hi { public static void main(String[] args) { School school[] = {new School("aa"), new School("bbb"), new School("cccc")}; for(School aa:school) { System.out.print(aa.getName()+" "); } } } // =================== // 內部類 /* [public ] class 外部類名 { [訪問控制符] [修飾符] 成員; ... [訪問控制符] [修飾符] class 內部類 { [訪問控制符] [修飾符] 成員; } } */ // 定義內部類 class Outer { private String str = "lin3615"; public class Inter { public void add(int x, int y) { System.out.println("x+y = "+(x+y)); } public void getStr() { System.out.println(str); } } public void getInfo() { new Inter().getStr(); } } public class Hi { public static void main(String[] args) { Outer out = new Outer(); out.getInfo(); Outer.Inter oin = new Outer().new Inter(); oin.add(3,5); oin.getStr(); } } // ================= // ========== /* 直接使用 new Inter() 爲接口實例化,但接口自己是不能實例化的, 因此在 new Inter() 以後的花括號 {} 中寫的就是實現接口中的抽象方法 */ interface Inter // 定義接口 { public abstract void getInfo(); // 聲明抽象方法 } class InterClass // 定義類 { public void print() { show(new Inter(){ // 匿名內部類 public void getInfo() { System.out.println("這是匿名內部類實現接口"); } }); } public void show(Inter i) // 傳入接口 { i.getInfo(); // 調用抽象類方法 } } public class Hi { public static void main(String[] args) { new InterClass().print(); // 建立 InterClass 類的實例並調用print()方法 } } // =============