【正文】
和方法稱為類的成員(成員變量和成員方法),而構造方法不是類的成員。 // Create a Circle object ? (The area of the circle of radius ? + + is + ())。由于它們是同一個對象,因此通過變量 b2對對象的改變也將影響 b1所對應的對象 8 .2 簡單類型變量和對象類型變量的區(qū)別 ? 這種情況描繪如下: 9 .3 垃圾回收 清除對象 java運行使系統(tǒng)有一個垃圾回收進程負責清除不再使用的對象。 也可以一步完成 Circle mycircle = new Circle()。1 第 5章 使用對象和類編寫程序 理解對象和類以及他們之間的關系 學習如何定義類和創(chuàng)建類的對象 理解構造方法的使用 了解對象的引用和如何向方法傳遞對象 理解實例變量和實例方法 理解類變量、類常量和類方法 理解變量的作用域 熟悉 java API的組織 使用字符串類 2 汽車設計圖 第 1輛 第 2輛 第 3輛 圖 汽車類與汽車的實例 對象和類 3 pi radius height area() volume () 成員變量 成員方法 圓柱體類 4 .1 聲明和創(chuàng)建 對象 1. 創(chuàng)建對象 通過創(chuàng)建一個對象可創(chuàng)建類的一個實例 , 也稱實例化此類。 7 .2 簡單類型變量和對象類型變量的區(qū)別 ? 初始化對象 每個變量代表一個存儲值的內存地址。 ? 垃圾回收器 垃圾回收器定期掃描內存,對于被應用的對象加上標記,按可能的路徑掃描結束后清除未加標記的對象。 ? } ? } ? 14 訪問對象的數(shù)據(jù)和方法 ? // Define a circle ? class Circle ? { ? double radius = 。 ? 每個類中至少有一個構造方法。 ? Circle(double r) ? { ? radius = r。 // Print areas for radius 1, 2, 3, 4, and 5. int n = 5。 問范圍說明符 當前類 所有子類 包內所有類 所有包中的類 private X protected X X X public X X X X package X X 27 可見性修飾符與訪問器方法 class Circle { private double radius。 // Increase myCircle39。 31 類成員與實例成員 2. 類方法與實例方法 二者區(qū)別: ? 實例方法能訪問當前對象的實例變量也能訪問類變量;類方法只能訪問類變量而不能訪問實例變量。 Depth o = new Depth()。 } // . . . 這段代碼僅執(zhí)行一次 —— 首次生成那個類的一個對象時,或者首次訪問屬于那個類的一個 static成員時(即便從未生成過那個類的對象)。 } 38 例 public double getRadius() { return radius。 // Create circle2 Circle circle2 = new Circle(5)。 } // Print circle information public static void printCircle(Circle c) { (radius ( + () + ) and number of Circle objects ( + () + ))。//y得到的值是 3,不是 f 中沒有” int x=3。 (三角形型的三邊是 :++,++,++,)。 long t2 = (d)。 } 48 // Add a rational number to this rational public Rational add(Rational secondRational) { long n=numerator*() + denominator*()。 return new Rational(n, d)。 Rational r2 = new Rational(2, 3)。 ? 相同的字符串常量屬于同一個對象,占用同一塊空間 ,例如 : // 程序 public class TestConstString{ public static void main(String args[ ]) { String str1=Hello, str2=Hello。,39。 3. public String(char value[ ], int offset, int count) 例如: char a[ ]={39。}。 例如: String str = abc。 String str = new String(data)。 s1=s2。 a=( )。 String s2=new String(s1)。 8. public String substring(int begin, int end ) 提取 string對象中從 begin開始,到 end1結束的子串,返回提取的子串。/39。 ? else ? (s + is not a palindrome)。 71 StringBuffer類對象的緩沖區(qū) ? StringBuffer類對象有一塊 緩沖區(qū) ,字符串被存放在緩沖區(qū)中,緩沖區(qū)的大小可以隨程序的需要進行調整。例如: StringBuffer s2=new StringBuffer(2)。)擴充新增加的部分 。 // 設置長度為 8 (80)。 (capacity = +( ))。 s1=s2。 public class BufferToString{ // 程序 510 public static void main(String args[ ]) { String s1。 輸出結果為: str= Hello Java! 84 例 ? public class TestMulTableUsingStringBuffer ? { ? // Main method ? public static void main(String[] args) ? { ? // Create a string buffer ? StringBuffer strBuf = new StringBuffer()。 85 例 ? // Append the number title to the buffer ? ( | )。 i=9。)。 89 例子 import .*。 } } 90 例 import 。 StringTokenizer st = new StringTokenizer(s)。 StringTokenizer fenxi=new StringTokenizer(s,“ ,”)。 ? // Get end time ? long endTime = ()。 ? for (int j=1。 j=9。 ? // Append the title to the buffer ? ( Multiplication Table + 39。 s1=(Java!).toString( )。 39。 例如: StringBuffer str=new StringBuffer(Hello,Java)。 (Length = +( ))。 程序 69演示了這幾個方法的具體應用。 例如: String s1=Java。 ? 當修改對象的內容時,只要 StringBuffer對象包含的字符個數(shù)沒有超出容量,就不會分配新的空間,而直接在原空間內進行修改。 ? // The index of the last character in the string ? int up = () 1。\\39。 例如: String s1=Hello , s2=Java , s3。 ((s2))。i。 } } 61 String類常用方法 1. public int length( ): 求串長 。 String s3=new String(s1)。a39。 // 字符串 str的內容是 av 4. public String(String value) 采用 value對象的值構造一個