【正文】
Smember m2 = new Smember()。 (“In static block”)。 } } 不論產(chǎn)生多少個(gè)對(duì)象, 屬于類的靜態(tài)變量只有一份, 即只占有一份存儲(chǔ)空間 。 static boolean b = true。 C ethod c2 = new C ethod(false)。 if (a == null) (“Day dream”)。 } new Tree()。 i 5。 } void info() { prt(“Tree is ” + height + “ feet height”)。 Tree() { prt(“Planting a seeding”)。 h = ()。 h = 20。 (() +“ ” +())。 (實(shí)例化對(duì)象 ) 1. 對(duì)象的實(shí)例化通過構(gòu)造方法 (constructor)來實(shí)現(xiàn) 2. 構(gòu)造方法的名字與類名相同 3. 構(gòu)造方法沒有返回值 4. 構(gòu)造方法可以有多個(gè),構(gòu)成方法的重載(overload) 47 ? 例 : 對(duì)象的實(shí)例化和初始化 class jex6_8 { public static void main(String args[]) { Qangle q1 = new Qangle()。 ()。 } … … } class Son extends Father { … … void display() { … …。 v=2。 u+=v。 } } void p( ) { u=3。 for (i=1。s methods and modify the accessible variables within th object. 42 ? 例 class Test { public static void main(String args[]) { ex A = new ex()。 class Test { private void addV(Vector v) { (s3)。 (())。 (x + + y)。 } } 41 ? 方法參數(shù) Pass by Value public static void main(String args[]) { int x=3, y=4。 } } class Area { double width, height。 Area myArea。 print(x)。 Test t = new Test()。 print(x)。 static void print() { … } } class Test { public static void main(String args[]) { (“A=“ + + “ B=“ + )。 class B { void test() { A a = new A()。 ()。 ()。 import 。 ()。 ()。 ^ print() has private access in A ()。 class B { void test() { A a = new A()。 } } package abc。 } ? 類型 :基本類型,復(fù)合類型 (數(shù)組、類和接口 ) 類成員 26 ? 訪問控制修飾符 ? 公共訪問控制符- public ? 被所有類訪問 ? 默認(rèn)訪問控制符 ? 被同一包中其他類訪問 ? 私有訪問控制符- private ? 被該類自身訪問 ? 保護(hù)訪問控制符- protected ? 該類自身、同一個(gè)包中的其他類、其他包中的子類訪問 ? 私有保護(hù)訪問控制符- private protected ? 該類自身、所有子類訪問 訪問控制修飾符 27 ? 非訪問控制修符 ? 靜態(tài)變量 static ? 屬于類的變量 ? 最終變量 final ? 值在程序的執(zhí)行過程中不會(huì)改變 ? 易失變量 volatile ? 可能同時(shí)被多個(gè)線程所控制和修改 非訪問控制修符 28 ? public (公共變量 /公共方法 ) ? 容許全權(quán)訪問,無任何限制 (先構(gòu)造對(duì)象,再訪問 ) public (公共變量 /公共方法 ) class A { public int x。 class test3 { … … } 25 ? 對(duì)象具有狀態(tài)和行為 ? 成員變量定義 [訪問權(quán)限修飾符 ] 類型 變量名 。 } } 一個(gè)簡單的類 22 ? 包 (Package)的概念 package 。 score2 = d2。 stuNumber = s2。 } … … … … … … } 類的定義 18 ? 類的定義格式 [類的修飾符 ] class 類名 [extends 父類名 ] [implements 接口名 ] { … … … … … … } ? 類的修飾符 ? public: 公共類,可以被其他類所使用, declares that the class can be used by any class regardless of its package (無任何限制 ) ? 無修飾 /默認(rèn)說明 : a class can be used only by other classes in the same package (僅僅能在同一個(gè) 包 中的其他類引用 ) ? abstract: declares that the class cannot be instantiated (宣布該類不能被實(shí)例化 ) ? final: declares that the class cannot be subclassed (宣布該類不能有子類 ) 類的描述 19 ? 類的修飾符 ? final Declares that the class cannot be subclassed.(宣布該類不能有子類 ) final class ChessAlgorithm { . . . } class BetterChessAlgorithm extends ChessAlgorithm { . . . } Can39。 } } () () () () () () () () () 多態(tài)性 (續(xù) ) 16 ? 程序是對(duì)象的集合,而對(duì)象是類的實(shí)例化 ? 源程序就是一個(gè)個(gè)的 Java類 ? Java本身提供的類