Java 基础三、接口与内部类

1.   在Java程序语言中,接口是对类的一种描述。例如Arrays类中sort方法声明可以对对象进行排序,但前提是对象所属的类必须实现Comparable接口。

public interface Comparable<T>
{
    int compareTo(T other);
 }
 

public interface Comparable{
    int compareTo(Object obj);
}

  接口中的所有方法自动地属于public,不必提供关键字。接口可以定义常量,但不能有自己的实例域,也不能实现方法。这些应该由实现接口的类来完成。(java8 新增特性接口可以有默认实现方法和静态方法)

实现接口的方法:

  • 通过implements关键字将类声明为接口的实现。
  • 在类中实现接口的所有方法。并且所有方法都要写明public。

2.   接口不是类,不能创造接口的实例。但是可以声明接口的变量。接口变量必须引用实现了接口的类的对象。可以用instanceof检查对象是否实现了某个接口。

3.  接口也可以被扩展。例如,假设有一个Moveable接口。

public interface Moveable(
    void move (double x, double y);
}

public interface Powered extends Moveable{
    double milesPerGallon();
    double SOPEED_LIMIT = 100;
}

接口中的数据,默认设为 public static final。

一个类只能继承于一个基类,但可以实现多个接口,class Student implements Comparable, Cloneable.

4.   克隆对象。

Object类的clone方法是protected。自定义的类不能直接使用该方法,需要重写为public。

* @return     a clone of this instance.
     * @throws  CloneNotSupportedException  if the object's class does not
     *               support the {@code Cloneable} interface. Subclasses
     *               that override the {@code clone} method can also
     *               throw this exception to indicate that an instance cannot
     *               be cloned.
     * @see java.lang.Cloneable
     */
    protected native Object clone() throws CloneNotSupportedException;
* @see     java.lang.CloneNotSupportedException
 * @see     java.lang.Object#clone()
 * @since   JDK1.0
 */
public interface Cloneable {
}

值得注意的是如果在重写的clone方法中调用了Object的clone方法,即super.close()。那么这个类就要写明implements Cloneable,否则会抛CloneNotSupportException。Cloneable接口为标记接口,其中并没有定义方法。

深浅拷贝怎么分清,还是只要弄清楚变量和实例到底是怎样的关系,自然迎刃而解,可以看这个java基础一

5.  内部类

  

class TalkingClock
{
   private int interval;
   private boolean beep;
   public TalkingClock(int interval, boolean beep){
      this.interval = interval;
      this.beep = beep;
   }
   public void start(){
      ActionListener listener = new TimePrinter();
      Timer t = new Timer(interval, listener);
      t.start();
   }
   public class TimePrinter implements ActionListener{
      public void actionPerformed(ActionEvent event){
         Date now = new Date();
         System.out.println("At the tone, the time is " + now);
         if (beep) Toolkit.getDefaultToolkit().beep();
          
          if(TalkingClock.this.beep) Toolkit.getDefaultToolkit().beep();// 使用外部类 的正规语法
      }
   }
}
public class InnerClassTest
{
   public static void main(String[] args)
   {
      TalkingClock clock = new TalkingClock(1000, true);
      clock.start();
      TalkingClock.TimePrinter timePrinter = clock.new TimePrinter();        //通过外部类构造公共内部类

      // keep program running until user selects "Ok"
      JOptionPane.showMessageDialog(null, "Quit program?");
      System.exit(0);
   }
}
  • 内部类可以访问其外部类的实例域。内部类在构造时获得了其外部类对象的引用。也可以说是编译器修改了所有内部类的构造器 ,添加 了一个外围类的引用参数。当然这是不可见的。
  • 内部类可以声明为 私有的,这样对于其他类就是隐藏的。只有内部类 可以声明为private。
  • 使用外围类的正规语法: OuterClass.this; 那么同样可以这样使用内部类,outer.new InnerClass(constrution parameters);
  • 局部类,可以在方法或者代码块中定义一个内部类。这种局部内部类的作用域 被限定在声明这个类的块中。而且其不能用public和private声明。它对于这个块以外都是隐藏的
  • 局部类可以访问声明为final的局部变量。
  • 匿名内部类。语法
    new SuperType(construction parameters){
        inner class methods and data;
    }

    SuperType可以是一个接口,也可以是一个类。当是接口时就要实现 接口的方法。由于匿名类没有类名,所以它也不能有构造函数。当扩展 类的时候,构造参数传给基类的构造。当实现接口时,不能传递参数。

  • 在内部类不需要访问外围类对象的时候,应该 使用 静态内部类。 声明 在接口中的内部类会自动声明为public static.
posted @ 2022-09-07 14:44  迷路的圆  阅读(59)  评论(0编辑  收藏  举报