
使用”{}”括起來的一段代碼
根據位置可分類
定義在方法中的使用{}括起來的代碼
【資料圖】
public class CodeBlockDemo { public void test(){ System.out.println("test");{ System.out.println("測試"); } } public static void main(String[] args) { CodeBlockDemo codeBlockDemo = new CodeBlockDemo();//因為test是非static類型,所以不能在static的main里直接調用 codeBlockDemo.test();{ System.out.println("main"); } }}
直接寫在類中,用{}括起來的代碼
public class CodeBlockDemo { { System.out.println("構造代碼塊"); } public void test(){ System.out.println("test");{ System.out.println("測試"); } } public static void main(String[] args) { CodeBlockDemo codeBlockDemo = new CodeBlockDemo(); codeBlockDemo.test();{ System.out.println("main"); } }}
通過反編譯工具我們發現它將這段代碼加在了構造方法中,在創建對象時默認調用了方法進行初始化,因此先打印了構造代碼塊
每次代碼運行的時候會將構造代碼塊中的代碼添加到每個構造方法前面,當使用this()時不會添加例如
public class CodeBlockDemo { int a; int b; public CodeBlockDemo(){ System.out.println("構造方法"); } public CodeBlockDemo(int a){ this.a=a; } public CodeBlockDemo(int a,int b){ this(a); this.b=b; } { System.out.println("構造代碼塊"); } public void test(){ System.out.println("test");{ System.out.println("測試"); } } public static void main(String[] args) { CodeBlockDemo codeBlockDemo = new CodeBlockDemo(); codeBlockDemo.test();{ System.out.println("main"); } }}
反編譯得
使用static聲明的代碼塊,在程序載入的時候優先執行(意味著不需要創建對象也會執行)
數據庫連接等其他需要準備好的代碼會放在static中public class CodeBlockDemo { int a; int b; static { System.out.println("靜態代碼塊"); } public CodeBlockDemo(){ System.out.println("構造方法"); } public CodeBlockDemo(int a){ this.a=a; } public CodeBlockDemo(int a,int b){ this(a); this.b=b; } { System.out.println("構造代碼塊"); } public void test(){ System.out.println("test");{ System.out.println("測試"); } } public static void main(String[] args) { CodeBlockDemo codeBlockDemo = new CodeBlockDemo(); codeBlockDemo.test();{ System.out.println("main"); } }}
多線程的時候會使用,用來給分享空間進行加鎖操作
就是包,對應到文件就是多級管理目錄
一般定義package會放置在Java文件的第一行
完全定義名:包名+類名
當需要引入非lang包的其他java類的時候,需要使用import,否則每次在使用某個類的時候都需要將類的完全限定名寫上
注意:當一個java文件中需要使用多個同名的類時,只能import一個,其余的用完全限定名
用法:
import java.包名.類名:導入具體的類,推薦使用import 包名.*:導入當前包的所有類靜態導包:
當需要使用一個或某個類的多個方法時,但又不想重復寫類名時,可以使用靜態導包。
import static java.lang.Math.*...System.out.println(sqrt(2));System.out.println(Math.sqrt(2));//兩者效果一樣
package com.ak27;public class Dog { String name; int age; String color; public Dog(){ } public Dog(String name,int age,String color){ this.age=age; this.color=color; this.name=name; } public void eat(){ System.out.println("eating meat"); } public void play(){ System.out.println("basketball"); } public void show(){ System.out.println("name:"+this.name); System.out.println("age:"+this.age); System.out.println("color:"+this.color); }}
package com.ak27;public class DogTest { public static void main(String[] args) { Dog dog = new Dog(); dog.name="旺財"; dog.age=-11; dog.color="black"; dog.show(); }}
??dog.age=-11;?
?如果任何一個處理類都可以直接對Dog進行賦值操作,那么當值不正確的時候,可能會產生額外結果。如何在賦值的同時添加邏輯判斷------封裝。
package com.ak27;public class Dog { String name; private int age;//訪問修飾符保障不是誰都可以訪問賦值 String color; public Dog(){ } public Dog(String name,int age,String color){ this.age=age; this.color=color; this.name=name; }/*------------------------封裝 --------------------------*/ public void setAge(int age){ if(age>0){ this.age = age; }else{ System.out.println("輸入年齡不規范"); } }/*------------------------------------------------------*/ public void eat(){ System.out.println("eating meat"); } public void play(){ System.out.println("basketball"); } public void show(){ System.out.println("name:"+this.name); System.out.println("age:"+this.age); System.out.println("color:"+this.color); }}
package com.ak27;public class DogTest { public static void main(String[] args) { Dog dog = new Dog(); dog.name="旺財"; //int age = -12; dog.setAge(-11); dog.color="black"; dog.show(); }}
將類的某些信息隱藏在類內部,不允許外部程序直接訪問,而是通過該類提供的方法來實現對隱藏信息的操作和訪問
使用封裝可以保障數據的規范,不符合規范的數據將無法進行操作。
1.隱藏類的內部實現細節
2.只能通過提供的方法進行訪問,其他無法訪問
3.可以根據需求添加復雜的邏輯判斷語句
4.方便修改實現
面向對象的封裝(狹義):
將類中的屬性設置為私有屬性,提供共有的外部方法程序進行調用,可以實現豐富的細節操作廣義的封裝:
可以將完成特點功能的代碼塊封裝成一個方法,供不同的程序進行調用我們程序設計要追求“高內聚,低耦合”。
高內聚就是類的內部數據操作細節自己完成,不允許外部干涉;
低耦合 :僅暴露少量的方法給外部使用。
package com.ak27;public class Dog { private String name; private int age; private String color; public Dog(){ } public Dog(String name,int age,String color){ this.age=age; this.color=color; this.name=name; } public void setAge(int age){ if(age>0){ this.age = age; }else{ System.out.println("輸入年齡不規范"); } } public int getAge(){ return this.age; } public void setName(String name){ this.name=name; } public String getName(){ return this.name; } public void setColor(String color){ this.color=color; } public String getColor(){ return this.color; } public void eat(){ System.out.println("eating meat"); } public void play(){ System.out.println("basketball"); } public void show(){ System.out.println("name:"+this.name); System.out.println("age:"+this.age); System.out.println("color:"+this.color); }}
package com.ak27;public class DogTest { public static void main(String[] args) { Dog dog = new Dog(); dog.setName("旺財"); dog.setAge(12); dog.setColor("Black"); System.out.println(dog.getAge()); System.out.println(dog.getName()); System.out.println(dog.getColor()); dog.show(); }}
類的屬性的處理:
一般使用private. (除非本屬性確定會讓子類繼承)提供相應的get/set方法來訪問相關屬性. 這些方法通常是public,從而提供對屬性的讀取操作。 (注意:boolean變量的get方法是用:is開頭!)方法的屬性的處理:
一些只用于本類的輔助性方法可以用private,希望其他類調用的方法用public當方法的參數值時基本數據類型的時候,不會改變原來的值
當方法的參數值是引用類型的時候,如果改變了該引用類型的值,會改變原來的對象的值
package com.ak27;public class Change { public static void test(Point point){ int x = point.getX(); int y = point.getY(); point.setX(y); point.setY(x); } public static void main(String[] args) { Point point =new Point(2,3); test(point); System.out.println(point.getX()); System.out.println(point.getY()); }}
package com.ak27;public class Point { private int x; private int y; public Point(int x,int y){ this.x=x; this.y=y; } public void setX(int x) { this.x=x; } public int getX() { return x; } public void setY(int y){ this.y=y; } public int getY(){ return y; }}