標籤:一個 abs 物件類型 比較 length 方法 標準 cas 大寫
一、多態
1.多態存在於繼承和介面中。
2.不確定行為放在父類中。
3.子類必須重寫父類中的不確定行為。
二、抽象類別
1.關鍵字 abstract。
例:public abstract class Shap{
public abstract void View();
}
2.抽象類別中放不確定的行為方法。
3.不能構建執行個體,因為有抽象方法。
4.抽象類別中有建構函式,方法和屬性。
子類預設調用父類的無參建構函式。
如果父類中是有參建構函式,子類也要有建構函式來調用,關鍵字 super。
三、介面
1.放多態方法。
2.建立——> Interface。
3.子類的關鍵字 implements 類名。
四、封裝類
1.封裝類就是物件類型。
2.Integer
(1)可接收null值,但是涉及計算時,不要賦null值,會報null 指標異常。
(2)Integer b = new Integer(100);
Integer b = 100; // 有自動裝箱功能。
System.out.println( b++ ); //自動拆箱功能
例:Integer a = 200;
Integer b = 200;
System.out.println(a == b); //false
== 同一對象比較
System.out.println( a.equals(b) ) // true
equals 對象的值比較
(3)方法
intValue(), doubleValue(), byteValue(), floatValue(), longValue(), shortValue(), toString(),parseInt()//字串轉為整型數字
例:int V1 = b.intValue();
3.Byte
(1)Byte a = 100;
(2)方法和Integer 大同小異。
Byte b = a.parseByte("100"); //要是標準的數字字串轉為位元組。
4.Boolean
(1)Boolean a = true;
(2)方法
a.booleanValue();
a.toString();
a.parseBoolean() ;
例:String s = "true";
boolean a = Boolean.parseBoolean(s);
System.out.println(a); // true
當s為其他值時 顯示 false
5.Character
(1)Character c = ‘a‘;
(2)方法
charValue()
isLetter() //是否是一個字母
isDigit() //是否是一個數字字元
isWhiteSpace() //是否是一個空格
isUpperCase() //是否大寫字母
toUpperCase() //指定大寫形式
五、字串 String
1.String message = new String("hello world");
String hello = "hello world";
2.字串池
例:(1)String message = ”hello world”;
String hello = "hello world";
System.out.println(message == hello); //true
當字串池已經有hello world了 兩個對象就會用字串中的同一個hello world。
(2)String message = new String( ”hello world”);// 如果加上 .intern(),就會利用字串池,否則new的會不利用字串池
String hello = "hello world";
System.out.println(message == hello); //false
3.字串的不可變性
String a ="hello";
String b = "hello‘;
b = b + "world";
System.out.println(a); //hello
此時字串池中有 hello 、world、hello world
4.減少字串池中的垃圾
StringBuffer bf = new StringBuffer();
bf.append("hello");
bf.append("world");
bf.toString();
只是字串池中只有 hello world
5.String 的方法
(1)length(); //字串長度
(2)equals(); //對象的值比較
(3)indexOf(); //字串首次在尋找的字串的位置,下標從0開始,沒有找到返回-1
(4)lastIndexOf(); //從末尾尋找字串首次出現的位置,下標值也是從開始數
(5)replace(換,替); //t替換指定的字串
(6)split(); //返回一個數組,拆分字串
例:String stu1 = "李明-20-男";
String stu[] = stu1.split("-");
System.out.println(stu[1]); // 20
(7)substring(開始,結束); // 截取指定長度字串,沒有結束值,截取到末尾
(8)toUppercase(); //小寫轉大寫
(9)toLowerCase(); //大寫轉小寫
(10)charAt(); //取字串中的第n個字元
(11)concat(); //連結字串
(12)contains(); //該字串是否存在於尋找的字串中
(13)endsWith(); //判斷末尾包含該字串嗎,可判斷檔案類型
例:a.endsWith(".mp4");
(14)startsWith(); //判斷開頭包含該字串嗎,可判斷URL協議
例:url.startsWith("http://‘);
(15)equalsIgnore(); //忽略大小寫,判斷值比較
(16)getByte(); //得到位元組
java基礎 第十一章(多態、抽象類別、介面、封裝類、String)