为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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章