对一语句不太理解,主指导?

javaboy328 2004-08-30 04:15:17
从面向对像的角度,大家帮我分析一下,下面的语句,我有些不理解?多谢!
class PopupAuthenticator extends Authenticator{
public PasswordAuthentication getPasswordAuthentication()
{
String username,password;
username="123";
password="123";
return new PasswordAuthentication(username,password);
}
}
........
Authenticator auth=new PopuAuthenticator();
...全文
54 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
广州秀才 2004-08-30
  • 打赏
  • 举报
回复
//定义一个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();

江南愚子 2004-08-30
  • 打赏
  • 举报
回复
假定A是B的父类,从本质上来说A a;是说明a是指向一个A对象的引用(或者说句柄),现在a = new B();却将引用a指向了一个B对象。a本应该指向一个A对象,那能不能指向B对象呢?答案是能,因为B是A的子类,B对象具有A对象的所有特征。
江南愚子 2004-08-30
  • 打赏
  • 举报
回复
Authenticator是PopuAuthenticator的父类,将auth声明为Authenticator的实例,但用子类PopuAuthenticator的构造函数创建,那么这个auth实际上就是PopuAuthenticator对象,这句代码就是让你明白使用子类的构造函数创建的对象实际上是子类的对象,尽管将其声明为了父类的实例
BillyGao 2004-08-30
  • 打赏
  • 举报
回复
定义一个PopupAuthenticator 的类,它是Authenticator类的子类,
在PopupAuthenticator类中定义了一个getPasswordAuthentication()的方法,返回一个PasswordAuthentication类型的对象。
最后是声明一个Authenticator类型 的auth引用,指向PopupAuthenticator类型的实例。
不知道我的理解对不对?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧