爲SCJP認證考試而努力-3

這一節繼續來學習修飾語:static(靜態的)、abstract(抽象的),在課後的小結中還會遇到final(常量)、native(本地的)。
package operation;

class MyClass {
        public static int iMyVal = 0;

        // public int iMyVal = 0;
        public static void amethod() {
                System.out.println("MyClass");
        }
}

/**
* @author 木炭 修飾符static
*/

public class Stat extends MyClass {
        int i;

        // public void amethod(){}//編譯錯誤,一個非static(普通的)方法不能在子類中重寫爲static 方法
        public static void amethod() {
                System.out.println("amethod in StaOver");
        }

        /**
         * @param args
         */

        public static void main(String[] args) {
                // i = i + 2;//編譯期錯誤,不能在一個static 方法內部訪問一個非static 變量。
                MyClass m1 = new MyClass();
                m1.iMyVal = 1;// The static field MyClass.iMyVal should be accessed in a
                // static way
                MyClass m2 = new MyClass();
                m2.iMyVal = 2;
                MyClass m3 = new MyClass();
                m3.iMyVal = 99;
                // 三個實例中的int iMyVal 都有對應各自實例的不同值。
                System.out.println("m1.iMyVal=" + m1.iMyVal);
                // 標記一個變量爲 static 表明每個類只能有一個副本存在,上面的多個副本的用法是錯誤的,所以最後輸出:m1.iMyVal=99

                MyClass.amethod();// 一個static方法,輸出:MyClass
                Stat st = new Stat();
                st.amethod();// 輸出:MyClass。
                // 一個非static(普通的)方法不能在子類中重寫爲static 方法,但static方法可以重寫static方法
        }// End of main
}
 
 
package operation;

//如果一個類有一個或多個abstract方法,或者繼承了不準備運行的abstract 方法,則它必須聲明爲abstract
//如果不修飾abstract(接口),The compiler will complain that the Base class is not declared as abstract.
abstract class Base {
        abstract public void myfunc();

        public void another() {
                System.out.println("Another method");
        }
}

/**
* @author 木炭 測試接口類,可以使用這個模板換成其他類型,例如finl
*/

public class Abs extends Base {
        /**
         * @param args
         */

        public static void main(String args[]) {
                Abs a = new Abs();
                a.amethod();// 運行結果:My func
        }

        // 一個abstract 類可以有非abstract 方法,但是任何擴展它的類必須實現所有的abstract 方法。
        public void myfunc() {
                System.out.println("My func");
        }

        public void amethod() {
                myfunc();
        }
}
 
 
 
問題 )當你試着編譯運行下面的代碼的時候,可能會發生什麼?
public class MyMain{
public static void main(String argv){
System.out.println("Hello cruel world");}
}
1) The compiler will complain that main is a reserved word and cannot be used for a class
2) The code will compile and when run will print out "Hello cruel world"
3) The code will compile but will complain at run time that no constructor is defined
4) The code will compile but will complain at run time that main is not correctly defined
答案 4) The code will compile but will complain at run time that main is not correctly defined
main 的簽名包含一個String 參數,而不是string 數組。
問題 )下面的哪個是Java 修飾符?
1) public
2) private
3) friendly
4) transient
答案 1) public 2) private 4) transient
雖然有些文本使用friendly 來表示可見性,但它不是一個Java 保留字。
問題 )你爲什麼可能會定義一個native 方法呢?
1) To get to access hardware that Java does not know about
2) To define a new data type such as an unsigned integer
3) To write optimised code for performance in a language such as C/C++
4) To overcome the limitation of the private scope of a method
答案 1) To get to access hardware that Java does not know about
3) To write optimised code for performance in a language such as C/C++
雖然創建“純正的Java”代碼值得鼓勵,但是爲了允許平臺的獨立性,我們不能將此作爲信
仰,有很多時候,我們是需要native 代碼的。
問題 )當你試着編譯運行下面的代碼的時候,可能會發生什麼?
private class Base{}
public class Vis{
transient int iVal;
public static void main(String elephant[]){}
}
1) Compile time error: Base cannot be private
2) Compile time error indicating that an integer cannot be transient
3) Compile time error transient not a data type
4) Compile time error malformed main method
答案 1) Compile time error: Base cannot be private
一個Base 類這樣的頂級類不能定義爲private。
Illegal modifier for the class Base; only public, abstract & final are permitted
問題 )下面的哪一個聲明是合法的?
1) public protected amethod(int i)
2) public void amethod(int i)
3) public void amethod(void) 
4) void public amethod(int i)
答案 2) public void amethod(int i)
選項3 C/C++ 寫法在這裏不能有,選項4 方法的返回類型必須緊跟着出現在方法名之前。
 
 
其他文章請查看:http://blog.csdn.net/xingdian119
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章