final   最终的类,不可以被继承,如果用来修饰方法,说明方法不可以重写和覆盖abstract 没有方法体的方法叫抽象方法,如private abstract void proMethod();  那么此类也该声明是abstract abstract Test{};抽象的类不能实例化对象,如不能Test t=new Test();因为Test类声明为abstract;如果类Test1继承了类Test,那么类Test 1类要实现抽象类Test中的抽象方法如:class Test1 extented Test{  pubic void proMethod(){};} 如果类Test1并没明实现基类Test中的抽象方法,那么类Test1也应为声明为抽象的类。 如:有一个动物的基类,里面有一个睡觉的方法,动物睡觉的姿势不一样,有站着睡的,有躺着睡的,所以基类中睡觉的方法不确定,那么我们就可以给睡觉这个方法声明成抽象的,然后子类继承后实现里面的睡觉方法               public protected default private 同类       1             1              1          1      同包       1              1             1          子类       1               1通用性  1垃圾回收机制class Garbage{
 int index;
 static int count;
 Garbage(){
  count++;
  System.out.println("此类被实例化了"+count+"次");
  index++;
  setID(count);
 
 }
 void setID(int id){
  index=id;
 }
 protected void finalize(){
  System.out.println(index);
 }
 public static void main(String[] args){
  new Garbage();
  new Garbage();
  new Garbage();
  new Garbage();
  new Garbage();
  new Garbage();
  new Garbage();
  new Garbage();
  new Garbage();
  System.gc();
 }
}为什么要用 void setID(int id)这个方法来给setID负值呢,直接用index=count不可以吗?

解决方案 »

  1.   

    是这段代码:
    垃圾回收机制class Garbage{
     int index;
     static int count;
     Garbage(){
      count++;
      System.out.println("此类被实例化了"+count+"次");
      index++;
      setID(count);
     
     }
     void setID(int id){
      index=id;
     }
     protected void finalize(){
      System.out.println(index);
     }
     public static void main(String[] args){
      new Garbage();
      new Garbage();
      new Garbage();
      new Garbage();
      new Garbage();
      new Garbage();
      new Garbage();
      new Garbage();
      new Garbage();
      System.gc();
     }
    }为什么要用 void setID(int id)这个方法来给setID负值呢,直接用index=count不可以吗?