標籤:有一個 ++ java學習 for 構造方法 類繼承 對象 分享圖片 代碼
day03
1.參考型別數組
1)先聲明後賦值
Student[] stus = new Studenr[3];
stus[0] = new Student(“zhangsan”,25,”LF”);
stus[1] = new Student(“lisi”,26,”JMS”);
stus[2] = new Student(“wangwu”,27,”SD”);
stus[1].age = 36;//給 stus中第2個元素的年齡賦值為36
2)聲明的同時賦值
Student[] stus = new Studenr[]{
new Student(“zhangsan”,25,”LF”),
new Student(“lisi”,26,”JMS”),
new Student(“wangwu”,27,”SD”)
};
3)int[]型的數組------------數組的數組
int[][] arr = new int[3][];
arr[0] = new int[2];
arr[0] = new int[2];
arr[0] = new int[2];
a[1][0] = 100;//給 arr中第2個元素的第1個元素賦值為100
4)遍曆數組的數組
int[][] arr = new int[3][4];
for(int i = 0;i<arr.length;i++){
for(j = 0;j<arr[i].length;j++){
a[i][j] = 100;
}
}
//參考型別數組測試類別public class StuTest { public static void main(String[] args) { Stu[] stus = new Stu[3]; //建立Stu數組對象 stus[0] = new Stu("zhangsan",25,"LF"); //建立Stu對象 stus[1] = new Stu("lisi",26,"JMS"); stus[2] = new Stu("wangwu",28,"SD"); System.out.println(stus[1].age); //26 for(int i=0;i<stus.length;i++){ System.out.println(stus[i].name); stus[i].sayHi(); } Stu[] ss = new Stu[]{ new Stu("zhangsan",25,"LF"), new Stu("lisi",26,"JMS"), new Stu("wangwu",28,"SD") }; int[][] arr = new int[3][]; arr[0] = new int[2]; arr[1] = new int[3]; arr[2] = new int[2]; arr[1][0] = 100; int[][] as = new int[3][4]; for(int i=0;i<as.length;i++){ for(int j=0;j<as[i].length;j++){ as[i][j] = 100; } } }}
2.繼承
1)作用:代碼複用
2)通過extends來實現繼承
3)超類/父類:所有衍生類別所共有的屬性和行為
衍生類別/子類:衍生類別所特有的屬性和行為
4)衍生類別繼承超類後,衍生類別具有:超類的+衍生類別的
5)一個超類可以有多個衍生類別
一個衍生類別只能有一個超類 -----------單一繼承
6)繼承具備傳遞性
7)java規定:構造衍生類別之前必須先構造超類
在衍生類別的構造方法中若沒有調用超類的構造方法
-----則預設super()調用超類的無參構造方法
在衍生類別的構造方法中若調用超類的構造方法
-----則不再預設提供
super()調用超類構造方法必須放在最上面
3.super:指代當前對象的超類對象
super的用法:
1)super.成員變數名 ------------------訪問超類的成員變數
2)super .方法名 ------------------------調用超類的方法
3)super() --------------------------------調用超類的構造方法
//super的示範public class SuperDemo { public static void main(String[] args) { Boo o = new Boo(); }}class Coo{ Coo(int a){ }}class Doo extends Coo{ Doo(){ super(5); } /* //如下代碼,寫不寫都有 Doo(){ super(); } */}class Aoo{ Aoo(){ System.out.println("超類的構造方法"); }}class Boo extends Aoo{ Boo(){ //super(); //預設的(調用超類的構造方法) System.out.println("衍生類別的構造方法"); }}
Java學習二(物件導向)day03