国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java常用類-Object類、Math類

2019-11-07 23:05:21
字體:
來源:轉載
供稿:網友

1:Object類(掌握) (1)Object是類層次結構的根類,所有的類都直接或者間接的繼承自Object類。 (2)Object類的構造方法有一個,并且是無參構造 這其實就是理解當時我們說過,子類構造方法默認訪問父類的構造是無參構造 (3)要掌握的方法: A:toString() 返回對象的字符串表示,默認是由類的全路徑+’@’+哈希值的十六進制表示。 這個表示其實是沒有意義的,一般子類都會重寫該方法。 如何重寫呢?過程我也講解過了,基本上就是要求信息簡單明了。 但是最終還是自動生成。 B:equals() 比較兩個對象是否相同。默認情況下,比較的是地址值是否相同。 而比較地址值是沒有意義的,所以,一般子類也會重寫該方法。 重寫過程,我也詳細的講解和分析了。 但是最終還是自動生成。 (4)要了解的方法: A:hashCode() 返回對象的哈希值。不是實際地址值,可以理解為地址值。 B:getClass() 返回對象的字節碼文件對象,反射中我們會詳細講解 C:finalize() 用于垃圾回收,在不確定的時間 D:clone() 可以實現對象的克隆,包括成員變量的數據復制,但是它和兩個引用指向同一個對象是有區別的。 (5)兩個注意問題; A:直接輸出一個對象名稱,其實默認調用了該對象的toString()方法。 B:面試題 ==和equals()的區別? A:== 基本類型:比較的是值是否相同 引用類型:比較的是地址值是否相同 B:equals() 只能比較引用類型。默認情況下,比較的是地址值是否相同。 但是,我們可以根據自己的需要重寫該方法。

/* * public boolean equals(Object obj):指示其他某個對象是否與此對象“相等”。 * 這個方法,默認情況下比較的是地址值。比較地址值一般來說意義不大,所以我們要重寫該方法。 * 怎么重寫呢? * 一般都是用來比較對象的成員變量值是否相同。 * 重寫的代碼優化:提高效率,提高程序的健壯性。 * 最終版: * 其實還是自動生成。 * * 看源碼: * public boolean equals(Object obj) { * //this - s1 * //obj - s2 * return (this == obj); * } * * ==: * 基本類型:比較的就是值是否相同 * 引用類型:比較的就是地址值是否相同 * equals: * 引用類型:默認情況下,比較的是地址值。 * 不過,我們可以根據情況自己重寫該方法。一般重寫都是自動生成,比較對象的成員變量值是否相同 */public class StudentDemo { public static void main(String[] args) { Student s1 = new Student("林青霞", 27); Student s2 = new Student("林青霞", 27); System.out.PRintln(s1 == s2); // false Student s3 = s1; System.out.println(s1 == s3);// true System.out.println("---------------"); System.out.println(s1.equals(s2)); // obj = s2; //false System.out.println(s1.equals(s1)); // true System.out.println(s1.equals(s3)); // true Student s4 = new Student("風清揚",30); System.out.println(s1.equals(s4)); //false Demo d = new Demo(); System.out.println(s1.equals(d)); //ClassCastException }}class Demo {}

2:Math(掌握) (1)針對數學運算進行操作的類 (2)常見方法(自己補齊) A:絕對值 B:向上取整 C:向下取整 D:兩個數據中的大值 E:a的b次冪 F:隨機數 G:四舍五入 H:正平方根

package cn.itcast_01;/* * Math:用于數學運算的類。 * 成員變量: * public static final double PI * public static final double E * 成員方法: * 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):最大值 (min自學) * public static double pow(double a,double b):a的b次冪 * public static double random():隨機數 [0.0,1.0) * public static int round(float a) 四舍五入(參數為double的自學) * public static double sqrt(double a):正平方根 */public class MathDemo { public static void main(String[] args) { // public static final double PI System.out.println("PI:" + Math.PI); // public static final double E System.out.println("E:" + Math.E); System.out.println("--------------"); // public static int abs(int a):絕對值 System.out.println("abs:" + Math.abs(10)); System.out.println("abs:" + Math.abs(-10)); System.out.println("--------------"); // public static double ceil(double a):向上取整 System.out.println("ceil:" + Math.ceil(12.34)); System.out.println("ceil:" + Math.ceil(12.56)); System.out.println("--------------"); // public static double floor(double a):向下取整 System.out.println("floor:" + Math.floor(12.34)); System.out.println("floor:" + Math.floor(12.56)); System.out.println("--------------"); // public static int max(int a,int b):最大值 System.out.println("max:" + Math.max(12, 23)); // 需求:我要獲取三個數據中的最大值 // 方法的嵌套調用 System.out.println("max:" + Math.max(Math.max(12, 23), 18)); // 需求:我要獲取四個數據中的最大值 System.out.println("max:" + Math.max(Math.max(12, 78), Math.max(34, 56))); System.out.println("--------------"); // public static double pow(double a,double b):a的b次冪 System.out.println("pow:" + Math.pow(2, 3)); System.out.println("--------------"); // public static double random():隨機數 [0.0,1.0) System.out.println("random:" + Math.random()); // 獲取一個1-100之間的隨機數 System.out.println("random:" + ((int) (Math.random() * 100) + 1)); System.out.println("--------------"); // public static int round(float a) 四舍五入(參數為double的自學) System.out.println("round:" + Math.round(12.34f)); System.out.println("round:" + Math.round(12.56f)); System.out.println("--------------"); //public static double sqrt(double a):正平方根 System.out.println("sqrt:"+Math.sqrt(4)); }}(3)案例: A:猜數字小游戲package cn.itcast_02;import java.util.Scanner;/* * 需求:請設計一個方法,可以實現獲取任意范圍內的隨機數。 * * 分析: * A:鍵盤錄入兩個數據。 * int strat; * int end; * B:想辦法獲取在start到end之間的隨機數 * 我寫一個功能實現這個效果,得到一個隨機數。(int) * C:輸出這個隨機數 */public class MathDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("請輸入開始數:"); int start = sc.nextInt(); System.out.println("請輸入結束數:"); int end = sc.nextInt(); for (int x = 0; x < 100; x++) { // 調用功能 int num = getRandom(start, end); // 輸出結果 System.out.println(num); } } /* * 寫一個功能 兩個明確: 返回值類型:int 參數列表:int start,int end */ public static int getRandom(int start, int end) { // 回想我們講過的1-100之間的隨機數 // int number = (int) (Math.random() * 100) + 1; // int number = (int) (Math.random() * end) + start; // 發現有問題了,怎么辦呢? int number = (int) (Math.random() * (end - start + 1)) + start; return number; }} B:獲取任意范圍的隨機數
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青龙| 新宁县| 资兴市| 桂东县| 舒兰市| 抚顺市| 铜梁县| 内丘县| 天门市| 华蓥市| 木兰县| 驻马店市| 山东| 绿春县| 天水市| 镇康县| 汝南县| 马公市| 东山县| 鄯善县| 天门市| 措勤县| 胶州市| 佛坪县| 霍林郭勒市| 津南区| 霍州市| 尉氏县| 游戏| 托克逊县| 沂源县| 崇文区| 元谋县| 将乐县| 淳安县| 宜川县| 和硕县| 乐陵市| 林芝县| 吴堡县| 德钦县|