?? 4.10.txt
字號:
一、supper關(guān)鍵字
是在子類中使用的,通過super.變量名或方法名來調(diào)用父類中的變量或方法,當(dāng)父類中和子類中都有構(gòu)造方法的時候:
例:public class Su1 { //定義一類,它有一個數(shù)據(jù)成員,兩個構(gòu)造方法,一個無返回值的方法
int money=1;
public Su1(){}
public Su1(int a)
{
money=a;
}
public void say()
{
System.out.println("**************" );
}
}
-------------------------------
public class Su2 extends Su1{
public int iq=0;
public Su2()
{
super(); //調(diào)用父類的默認(rèn)的構(gòu)造方法,此語句可以不寫
}
public Su2(int i,int j)
{
super(i); //調(diào)用父類帶參數(shù)的構(gòu)造方法,其目的是為父類中的數(shù)據(jù)成員賦初值,此語句不可以省略
iq=j;
}
public void say()
{
System.out.println("@@@@@@@@@@");
}
public void fun()
{
System.out.println(super.money); //用super.數(shù)據(jù)成員名來調(diào)用父類中的數(shù)據(jù)成員
System.out.println("*****@@@@@@");
}
}
-------------------------------------------------
public class TestSu2 {
public static void main(String[] args) {
Su2 aa = new Su2(12, 34);
System.out.println(aa.money);
System.out.println(aa.iq);
aa.say();
Su1 bb = new Su2(); //創(chuàng)建一個父類的對象,使它指向子類對象
bb.say();
Su2 cc = (Su2) bb; //引用對象強制轉(zhuǎn)換時,要確保指向該對象
cc.fun();
}
}
二、二維數(shù)組的定義,賦值和輸出其中的元素,例子如下:
public class ErWei {
public static void main(String[] args) {
int[] arr1 = { 1, 2, 3 }; //定義一個一維數(shù)組,并為其初始化
int[] arr2 = new int[4]; //定義一個一維數(shù)組,長度為4
// 為數(shù)組arr2中的元素賦值
arr2[0] = 2;
arr2[1] = 3;
arr2[2] = 4;
arr2[3] = 5;
int[][] myArr = new int[2][]; //創(chuàng)建一個二維數(shù)組,長度為2,每個元素 存儲的是一維數(shù)組
//為二維數(shù)組中的元素賦值
myArr[0] = arr1;
myArr[1] = arr2;
//用嵌套的循環(huán)語句輸出二維數(shù)組中元素的值,其中i表示行數(shù),j表示列數(shù)
for (int i = 0; i < myArr.length; i++) {
int[] temp = myArr[i];
for (int j = 0; j < temp.length; j++) {
System.out.println("myArr[" + i + "][" + j + "]=" + myArr[i][j]);
}
}
}
}
三、靜態(tài)塊:完成初始化,是類加載時執(zhí)行,只執(zhí)行一次。
public class StaticDemo {
public static int a=1; //定義一個類變量
static //定義一個靜態(tài)塊
{
System.out.println("*********");
}
public static void fun() //定義一個靜態(tài)方法
{
System.out.println("-------------------");
}
}
----------------------------
public class TestStatic {
public static void main(String[] args) {
//因為調(diào)用的方法類中有靜態(tài)塊,靜態(tài)塊是當(dāng)類加載時執(zhí)行,只執(zhí)行一次
StaticDemo.fun(); //調(diào)用靜態(tài)方法的方法是類名.靜態(tài)方法名
//通過以下語句說明,使用靜態(tài)成員不需要創(chuàng)建對象,它是沒有意義的,只要改變其中 一個對象的值,
//其它對象的值也發(fā)生改變
StaticDemo.a = 1000;
StaticDemo aa = new StaticDemo();
StaticDemo bb = new StaticDemo();
System.out.println(aa.a);
System.out.println(bb.a);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -