J2SE基礎:2.對象的創建與使用
A:值傳遞:基本數據類型傳遞都是值傳遞
B:引用傳遞(地址傳遞):對象數據類型都是引用傳遞。
2:類變量與成員變量(實例變量,對象變量)
類變量:通過類名調用,類變量被全部的實例共享。
final static int MAX = 20;//Java中定義常量
對象變量:通過對象調用(對象必須new出來)。
3:類方法與成員方法(實例方法,對象方法)
類方法:通過類名調用,在類方法中不能使用thiskeyword。
由於this代表當前對象。
成員方法:通過對象調用(對象必須new出來)。
4:
構造方法
銷毀方法(finalize)
銷毀方法是在對象被銷毀的時候進行調用的。
當一個對象在堆區沒有一個明白的引用指向它的時候,Java虛
擬機覺得該對象是沒用的。
垃圾回收器是用於回收堆區分配的對象。垃圾回收器僅僅會回收3次的
的內存。垃圾回收器是虛擬機自己主動調用的。(堆區內存不夠的情況下調用)
可是能夠通過System.gc()來強制執行垃圾回收器。
5:static靜態塊與對象塊
尋找main方法--->載入類--->載入類的靜態塊代碼(僅僅初始化一次)
--->載入類的靜態方法和靜態變量(僅僅初始化一次)---->對象塊方法
--->對象的構造方法--->調用對象的方法--->運行對象的銷毀方法。
//成績類 class Score { int english; int math; int chinese; Score(){ } Score(int english,int math,int chinese){ this.english = english; this.math = math; this.chinese = chinese; } } class Student{ int stuid; String stuname; String stusex; //將成績類做為學生類的一個屬性。Score score; public Student(Score score){ this.score = score; } public int getTotalScore(){ return this.score.english +this.score.math +this.score.chinese; } public void changeScore(Score score){ score.chinese = 0; score.math = 0; } } public class Test_02{ public static void main(String args[]){ Score score_one = new Score(70,60,65); //score_one.english = 70; //score_one.math = 60; //score_one.chinese = 65; Score score_two = new Score(); score_two.english = 11; score_two.math = 12; score_two.chinese = 13; Score score_three = new Score(45,46,47); //score_three.english = 45; //score_three.math = 46; //score_three.chinese = 47; Student stu_one = new Student(score_three); Student stu_two = new Student(score_two); Student stu_three = new Student(score_one); /* System.out.println(stu_one.getTotalScore()); System.out.println(stu_two.getTotalScore()); score_three.english = 70; stu_one.score.math = 23; System.out.println(stu_one.getTotalScore()); System.out.println(stu_two.getTotalScore()); */ System.out.println(stu_one.getTotalScore());//138 stu_one.changeScore(score_two); System.out.println(stu_one.getTotalScore());//138 stu_one.changeScore(score_three); System.out.println(stu_one.getTotalScore());//45 System.out.println(stu_two.getTotalScore()); } }
public class Test_03 { int id; final static int MAX = 20; public static void main(String args[]){ //Test_03 test = new Test_03(); //System.out.println(test.MAX); System.out.println(Test_03.MAX); } }
class Person { int personid; String personname; public Person(){ System.out.println("對象的構造方法"); this.personid = 1; this.personname = "中國人"; } public void method(){ System.out.println("運行方法"); } public void finalize(){ System.out.println("對象被銷毀了"); this.personid = 0; this.personname = null; } } public class Test_04 { public static void main(String args[]){ Two(); System.gc(); } public static void Two(){ //創建對象 Person person = new Person(); //用對象 person.method(); } }
public class Test_05 { //載入類時。最早運行的一塊初始化內容。static{ System.out.println("靜態塊"); } //載入類時,靜態方法與靜態變量都已經放到內存的靜態區域中了。
public static void staticMethod(){ System.out.println("static方法"); } //對象塊的內容,在對象初始化之前運行的內容 { System.out.println("對象塊方法"); } //對象的構造方法 public Test_05(){ System.out.println("構造方法"); } public void objectMethod(){ System.out.println("對象方法"); } public static void main(String args[]){ Test_05.staticMethod(); Test_05 test = null; test = new Test_05(); test.objectMethod(); Test_05 test2 = null; test2 = new Test_05(); test2.objectMethod(); } } class Two { static{ System.out.println("Two的靜態塊"); } }
J2SE基礎:2.對象的創建與使用