Object、String、Date

Object:
概述:Object 是類層次結構的根類。其他所有類都使用 Object 作爲超類。
構造方法:
Object(),
爲什麼子類構造方法默認調用父類的無參構造。
如果沒寫構造方法,爲什麼系統默認會提供一個無參構造方法。

成員方法:
	1) public final Class getClass()
		返回對象的運行時類。
		Class: 類類型,用來描述類型的類。
			String getName(): 返回該類的全限定名
		
		注意事項:
			a. 返回的是運行時類
			b. 用final修飾, 所有對象該方法的行爲一致。
	
	2) public String toString()
		一個對象的字符串形式的簡介,要求簡單易懂。
		默認實現:
			getClass().getName() + "@" + Integer.toHexString(hashCode())
			
		默認實現不能很好的體現對象,所以推薦子類都重寫toString()
		重寫:
			根據關鍵域去重寫
			快捷鍵生成
	
	3) public boolean equals(Object obj)
		判斷兩個對象是否"相等";
		默認實現:
			return this == obj;
			
		實體類:不同對象代表不同實體,採用默認實現即可。
		值類:關鍵域相等,就認爲兩個對象相等,應該重寫equals()
		
		equals方法的協定:
			a. 自反性
			b. 對稱性
			c. 傳遞性
			d. 一致性
			e. 非空性
		Java語法沒有強制要求遵循這些規則,但是如果違反的話,可能會給程序帶來災難性的後果。
		
		重寫:
			a. 子類沒有新的要比較的屬性,就用instanceof進行判斷
			Demo:
			public boolean equals(Object obj) {
				if(obj instanceof Demo) {
					Demo demo = (Demo) obj;
					// 用關鍵屬性進行比較
				}
				return false;
			}

			遵守裏式替換原則
			
			b. 子類有新的要比較的屬性, 就用 getClass() 方法進行判斷
			Demo:
			public boolean equals(Object obj) {
				if (obj == null || obj.getClass() != getClass()) {
					return false;
				}
				Demo demo = (Demo) obj;
				// 用關鍵屬性進行比較
			}
			沒有遵循裏式替換原則
		
			c. 快捷鍵生成 (b方式)
		
		== 和 equals()的比較
			== :	如果比較是基本數據類型, 判斷他們的值是否相等
				如果比較是引用數據類型, 判斷他們是否是同一個對象
			
			equals():	不能比較基本數據類型
				比較引用數據類型, 如果沒有重寫Object的equals(),默認判斷是否是同一個對象。
				如果重寫了,一般是根據該對象的值進行判斷
	4) public int hashCode()
		獲取該對象的散列碼
		
		優秀hash算法:模擬隨機映射。
		默認實現:將對象的內存地址映射成一個整數值。
		
		hashCode()的協定:
			a. 一致性
			b. 如果兩個對象是"相等"的,那麼他們的hashcode也一定相等。
				如果重寫equals(),一定也要重寫hashcode().
			c. 如果兩個對象"不相等", 不要求他們的hashcode一定也不相等。但是最好不一樣,這樣提高hash表的效率。
		
		重寫:
			a.
				int hash= c;
				// 根據每一個關鍵域的hashcode去生成
				31 * i == (i << 5) - i;	
				hash= 31 * field.hashCode + hash
				...
				return hash
			b. 快捷鍵生成
	
	5) protected void finalize()
		垃圾回收器回收該對象之前,會調用該方法。
		作用:可以釋放系統資源
		
		注意事項:
			a. 如果直接調用,相當於手動釋放資源,並不會回收該對象。
			b. 釋放系統資源最好不要放在該方法裏面,推薦用 try...catch...finally ,延遲性。
	
	6) protected Object clone()
		返回該對象的一個克隆
		默認:淺拷貝
		如何實現深拷貝:
			a. 實現Cloneable接口
			b. 調用super.clone(), 完成父類中定義的數據和基本數據類型的拷貝。
			c. 一級一級實現深拷貝。

總結:
	理解: finalize(), clone(), getClass()
	掌握: toString(), equals(), hashcode()
  1. String類的概述和使用
    (1)概述:表示一個不可變的字符序列
    (2)構造方法:
    public String()
    public String(byte[] bytes)
    public String(byte[] bytes,int offset,int length)
    public String(char[] value)
    public String(char[] value,int offset,int count)
    public String(String original)
    public String(StringBuffer sb)
    String s = “hello world”;
    (3)字符串的特點
    A:字符串一旦被賦值,就不能改變。
    注意事項:這個字符串對象的值不能改變,但是指向這個字符串的引用變量可以改變
    B:字面值作爲字符串對象和通過構造方法創建字符串對象的不同
    String s = “hello”;
    String s = new String(“hello”);
    (4)字符串的面試題(看程序寫結果)
    A:和equals()
    String s1 = new String(“hello”);
    String s2 = new String(“hello”);
    System.out.println(s1
    s2);
    System.out.println(s1.equals(s2));

     	String s3 = new String("hello");
     	String s4 = "hello";
     	System.out.println(s3==s4);
     	System.out.println(s3.equals(s4));
     
     	String s5 = "hello";
     	String s6 = "hello";
     	System.out.println(s5==s6);
     	System.out.println(s5.equals(s6));	
     B:字符串的拼接
     	String s1 = "hello";
     	String s2 = "world";
     	String s3 = "helloworld";
     	System.out.println(s3==(s1+s2));
     	System.out.println(s3.equals(s1+s2));
    
     	System.out.println(s3==("hello"+"world"));
     	System.out.println(s3.equals("hello"+"world"));
    

    (5)字符串的功能
    A:判斷功能
    boolean equals(Object obj)
    boolean equalsIgnoreCase(String str)
    boolean contains(String str)
    boolean startsWith(String str)
    boolean endsWith(String str)
    boolean isEmpty()
    B:獲取功能
    int length()
    char charAt(int index)
    int indexOf(int ch)
    int indexOf(String str)
    int indexOf(int ch,int fromIndex)
    int indexOf(String str,int fromIndex)
    String substring(int start)
    String substring(int start,int end)

     C:轉換功能
     	byte[] getBytes()
     	char[] toCharArray()
     	static String valueOf(char[] chs)
     	static String valueOf(int i)
     	String toLowerCase()
     	String toUpperCase()
     	String concat(String str)
    
     D:其他功能
     	a:替換功能 
     		String replace(char old,char new)
     		String replace(String old,String new)
     	b:去空格功能
     		String trim()
     	c:按字典比較功能
     		int compareTo(String str)
     		int compareToIgnoreCase(String str) 
     	d: 分隔
     		String[] split(String regex)
    
  2. StringBuffer
    (1)概述:表示一個字符串緩衝區,它的值時可變的。
    (2)StringBuffer的構造方法
    public StringBuffer()
    public StringBuffer(int capacity)
    public StringBuffer(String str)

    (3)StringBuffer的常見功能
    A:添加功能
    public StringBuffer append(String str)
    public StringBuffer insert(int offset,String str)
    B:刪除功能
    public StringBuffer deleteCharAt(int index)
    public StringBuffer delete(int start, int end)
    C:替換功能
    public StringBuffer replace(int start,int end,String str)
    D:反轉功能
    public StringBuffer reverse()
    E:截取功能(注意這個返回值)
    public String substring(int start)
    public String substring(int start,int end)

    (4)面試題
    A:String,StringBuffer,StringBuilder的區別
    B:StringBuffer和數組的區別?
    C:String作爲形式參數,StringBuffer作爲形式參數。
    不可變的對象當做參數傳遞,相當於值傳遞。

3:Date/DateFormat
(1)Date是日期類,可以精確到毫秒。
A:構造方法
Date() 表示創建對象的系統時間
Date(long time) 基準時間:epoch(1970年1月1日 00:00:00)
B:成員方法
setTime(long time);
getTime()
獲取當前時間的毫秒值:
new Date().getTime();
System.currentTimeMillis(); (推薦使用)
C:日期和毫秒值的相互轉換
案例:你來到這個世界多少天了?
(2)DateFormat針對日期進行格式化和針對字符串進行解析的類,但是是抽象類,所以使用其子類SimpleDateFormat
A:構造方法:
SimpleDateFormat(); // 默認模式
SimpleDateFormat(String pattern); // 指定模式
模式字符:
年: y
月:M
日:d
時:H
分:m
秒:s
B:成員方法
String format(Date date)
Date parse(String s)
C:案例:
製作了一個針對日期操作的工具類。(封裝)
4: Math
(1)Math 類包含用於執行基本數學運算的方法,如初等指數、對數、平方根和三角函數。
(2)方法
public static int abs(int a)
public static double ceil(double a)
public static double floor(double a)
public static int max(int a,int b)
public static double pow(double a,double b)
public static double random() // 僞隨機數
public static int round(float a) // 四捨五入
public static double sqrt(double a) // 平方根,如果是負數,返回NaN
public static double sin(double a)
public static double asin(double a)
public static double cos(double a)
public static double acos(double a)
public static double tan(double a)
public static double atan(double a)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章