Liny_@NotePad

沉迷ACG中

SCJP复习笔记之修饰符。。

YOYO posted @ 2009年11月11日 23:44 in 【Java SE】 with tags SCJP , 1876 阅读

看一下记一下。。有很多以前没有注意到的地方……

1)static方法不可重写,根据调用的类来确定,测试代码:

  1. package org.yoyo.test.qualifiers;
  2.  
  3. public class TestOverrideStaticMethod {
  4.  
  5.         public static void main(String[] args) {
  6.                 Base.hello();   // Hello, Base!
  7.                 Derived.hello();//      Hello, Derived!
  8.                
  9.                 Derived d = new Derived();
  10.                 Base b = new Derived();
  11.                
  12.                 d.hello();      //    Hello, Derived!
  13.                 b.hello();      //    Hello, Base!
  14.                
  15.                 d.hello("test");        //      Derived: test
  16.                 b.hello("test");        //      Base: test
  17.         }
  18.  
  19. }
  20.  
  21. class Base {
  22.        
  23.         public static void hello() {
  24.                 System.out.println("Hello, Base!");
  25.         }
  26.        
  27.         public void hello(String msg) {
  28.                 System.out.println("Base: " + msg);
  29.         }
  30.        
  31. }
  32.  
  33. class Derived extends Base {
  34.        
  35.         public static void hello() {
  36.                 System.out.println("Hello, Derived!");
  37.         }
  38.        
  39.         @Override
  40.         public void hello(String msg) {
  41.                 System.out.println("Derived: " + msg);
  42.         }
  43.        
  44. }

2)abstract类也可以跑main方法……这个是可以理解的 但是米有试过 囧,遂测试下:

  1. package org.yoyo.test.qualifiers;
  2.  
  3. public abstract class TestAbstractClassMain {
  4.        
  5.         public abstract void aAbstractMethod();
  6.  
  7.         public static void main(String[] args) {
  8.                 System.out.println("main in abstract class..");
  9.         }
  10.  
  11. }

3)transient:都快忘了它是做什么用的。。原来是在序列化中被忽略的 囧。

  1. package org.yoyo.test.qualifiers;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.io.Serializable;
  8.  
  9. public class TestTransient {
  10.  
  11.         public static void main(String[] args) {
  12.                 TestClass test = new TestClass();
  13.  
  14.                 try {
  15.                         FileOutputStream stream = new FileOutputStream("c:\\test.obj");
  16.                         ObjectOutputStream out = new ObjectOutputStream(stream);
  17.                         out.writeObject(test);
  18.                         out.close();
  19.                 } catch (Exception e) {
  20.                         e.printStackTrace();
  21.                 }
  22.  
  23.                 try {
  24.                         FileInputStream stream = new FileInputStream("c:\\test.obj");
  25.                         ObjectInputStream in = new ObjectInputStream(stream);
  26.                         TestClass test1 = (TestClass) in.readObject();
  27.                         System.out.println(test1.i);    //  i = 3
  28.                         System.out.println(test1.j);    //  j = 2
  29.                         in.close();
  30.                 } catch (Exception e) {
  31.                         e.printStackTrace();
  32.                 }
  33.         }
  34. }
  35.  
  36. class TestClass implements Serializable {
  37.  
  38.         private static final long serialVersionUID = 1L;
  39.  
  40.         int i = 3;
  41.  
  42.         transient int j = 2;
  43.  
  44. }

 4)volatile:这个从来没有用过……囧

5)native:也是从来没有用过……在JNI里再详细看看……它的作用是
· To get to access hardware that Java does not know about 
· To write optimised code for performance in a language such as C/C++
如果没有调用native方法,即时方法本身没有被实现也不会报UnsatisfiedLinkError,说明方法加载时是在第一次调用时……

  1. package org.yoyo.test.qualifiers;
  2.  
  3. public class TestNative {
  4.        
  5.         public static void main(String[] args) {
  6.                 System.out.println("test success");
  7.                 //hello();
  8.         }
  9.        
  10.         public native static void hello();
  11.  
  12. }

6)private\protected\public就不说了……final也不记了……题目很多……

7)synchronized,同步,这个等线程章节做具体复习……

其他,public void amethod(void) 是不合法的,这和C++不同 囧。

另外贴下访问修饰符权限表

  所有成员 子类成员 同一个包 本类成员
public \(^o^)/YES! \(^o^)/YES! \(^o^)/YES! \(^o^)/YES!
protected   \(^o^)/YES! \(^o^)/YES! \(^o^)/YES!
default   同一个包中 \(^o^)/YES! \(^o^)/YES!
private       \(^o^)/YES!

这里的同一个包 并非单纯在同一个目录下即可,类文件中定义的包名应该要完全相同,否则就会出错。


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter