定义一个PopupAuthenticator 的类,它是Authenticator类的子类,
在PopupAuthenticator类中定义了一个getPasswordAuthentication()的方法,返回一个PasswordAuthentication类型的对象。
最后是声明一个Authenticator类型 的auth引用,指向PopupAuthenticator类型的实例。
不知道我的理解对不对?

解决方案 »

  1.   

    Authenticator是PopuAuthenticator的父类,将auth声明为Authenticator的实例,但用子类PopuAuthenticator的构造函数创建,那么这个auth实际上就是PopuAuthenticator对象,这句代码就是让你明白使用子类的构造函数创建的对象实际上是子类的对象,尽管将其声明为了父类的实例
      

  2.   

    假定A是B的父类,从本质上来说A a;是说明a是指向一个A对象的引用(或者说句柄),现在a = new B();却将引用a指向了一个B对象。a本应该指向一个A对象,那能不能指向B对象呢?答案是能,因为B是A的子类,B对象具有A对象的所有特征。
      

  3.   

    //定义一个PopupAuthenticator类,继承Authenticator类的所有方法和属性
    class PopupAuthenticator extends Authenticator{
       //新增getPasswordAuthentication方法,返回PasswordAuthentication类型数据,PasswordAuthentication类应该在前面已经有定义的
       public PasswordAuthentication getPasswordAuthentication()
       {
         String username,password;
         username="123";
         password="123";
         return new PasswordAuthentication(username,password);
       }
    }
    ........
    //实例化类
    Authenticator auth=new PopuAuthenticator();
    //然后才能调用类的方法和属性,如
    PasswordAuthentication pa = auth.getPasswordAuthentication();