Java内部类问题,

zhudonhua 2006-04-17 10:47:37
interface Destination
{
String readLabel();
}
class Parcel
{
public Destination dest(String s)
{
class PDestination implements Destination
{
private String label1;

private PDestination(String whereTo)
{
label1 = whereTo;
}
public String readLabel()
{
return label1;
}
}

return new PDestination(s);
}

public static void main(String[] args)
{
Parcel p = new Parcel();
Destination d = p.dest("Tanizania");
System.out.println(d.readLabel());
}

}

运行结果

Tanizania:

疑惑:

不是类的构造函数申明了private????怎么还可以生成对象??

具体见thinking in java(2E) P264



...全文
329 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhudonhua 2006-04-22
  • 打赏
  • 举报
回复

个人感觉这是设计原则的问题,不是向上转型的问题;也即,外部类可以访问内部类的私有成

员,因为整个内部类本来就属于外部类,那外部类当然可以访问自己的东西(内部类的任何

属性):下面这个例子可以执行成功


public class InnerClass
{
public class PDestination
{
private String label;

public PDestination(String whereTo)
{
label = whereTo;
}

public String readLabel()
{
return label;
}
}

public String readLabel()
{
PDestination dest = new PDestination("Tanizania");

return dest.label;/*这里直接访问内部类对象dest的私有属性label

不是通过dest.readLabel()来获取label属性 */
}

public static void main(String[] args)
{

InnerClass inner = new InnerClass();

System.out.println(inner.readLabel());

}
}

运行结果:
Tanizania


zhudonhua 2006-04-22
  • 打赏
  • 举报
回复
class Parcel
{
public Destination dest(String s)
{
class PDestination implements Destination
{
private String label1;

private PDestination(String whereTo)
{
label1 = whereTo;
}
public String readLabel()
{
return label1;
}
}

return new PDestination(s);
}

public static void main(String[] args)
{
Parcel p = new Parcel();
Destination d = p.dest("Tanizania");
System.out.println(d.readLabel());
}

}

不好意思,上面打错了,return new PDestination(s);语句是在类PDestination外面,但在

dest函数里面的,呵呵

个人感觉这是设计原则的问题,不是向上转型的问题;也即,外部类可以访问内部类的私有成

员,因为整个内部类本来就属于外部类,那外部类当然可以访问自己的东西(内部类的任何

属性)

以上仅是我的个人意见,没有确认过!呵呵


tomison 2006-04-22
  • 打赏
  • 举报
回复
其实你这样理解就行了,有同个类中,肯定可以访问私有变量啦,
public Destination dest(String s)
{
class PDestination implements Destination
{
private String label1;

private PDestination(String whereTo)
{
label1 = whereTo;
}
public String readLabel()
{
return label1;
}

return new PDestination(s);/* 是在同个类中,所以访问私有变量没问题 */
}
还有什么疑问?可以提出来,一起研究!
cyxlsm 2006-04-21
  • 打赏
  • 举报
回复
类的私有的构造函数可以被其内部类访问
Dan1980 2006-04-21
  • 打赏
  • 举报
回复
return new PDestination(s);/* 构造函数private!! */

构造函数是private的,没错!但是new PDestination(s)是在方法里面(即该类里面)运行的,并不是在外部运行的,所以没问题。而方法的返回类型是Destination,所以这里作了一个向上转型,相当于return (Destination) new PDestination(s); 也就是说,方法把new PDestination(s)当作一个Destination对象返回出来了,而Destination接口是public的,因此也没问题。楼主还有问题么?
anyezhitong 2006-04-21
  • 打赏
  • 举报
回复
没想到还有人和我的疑问一样啊。
我觉的可能和接口有关系。这个实际上是对接口的一种向上转型的特殊方式。。
不过无法确认。
zhudonhua 2006-04-21
  • 打赏
  • 举报
回复
eein(eein)

我指的是在dest方法中无法生成对象PDestination

public Destination dest(String s)
{
class PDestination implements Destination
{
private String label1;

private PDestination(String whereTo)
{
label1 = whereTo;
}
public String readLabel()
{
return label1;
}

return new PDestination(s);/* 构造函数private!! */
}

如果这里可以生成对象,那不是违反了面向对象基本的设计原则啊??
另外:
Thinking in java(2E)指的是 Thinking in java 第2版




zhudonhua 2006-04-21
  • 打赏
  • 举报
回复
Dan1980

能不能具体讲一下啊!
eein 2006-04-21
  • 打赏
  • 举报
回复
System.out.println(d.readLabel());

所调用的并不是直接访问类的私有变量,而是通过类的公有方法去访问的.而在这个方法的返回值由于方法声明是公有的,所以该返回值也是公有的.不在是类中所声明的私有了!
即可认为
private String label1;

return label1;
不是同一变量.
否则公有方法返回的私有,那这个方法也就没有意义了!
Dan1980 2006-04-21
  • 打赏
  • 举报
回复
Thinking in Java上不是说了“内部类是外部类的一个‘窗子’么”,就像你出门的时候明明把门都锁好了,但小偷还是可以从窗子里入室行窃一样。
zdsxj2002 2006-04-21
  • 打赏
  • 举报
回复
我理解 public private 只是申明做用域。
并不是说明。私有的就不能构造对象。如果在作用域内。也是可以的。


thinking in java(2E)没看过。
做鸡真好吃 2006-04-21
  • 打赏
  • 举报
回复
Mark~
zhudonhua 2006-04-20
  • 打赏
  • 举报
回复
public Destination dest(String s)
{
class PDestination implements Destination
{
private String label1;

private PDestination(String whereTo)
{
label1 = whereTo;
}
public String readLabel()
{
return label1;
}
}

return new PDestination(s);
}

return 语句是在PDestination类定义之外阿,而且类的构造函数又是私有的,而且没有任何方法
来返回其对象,即使是单例模式也不行阿!不信,试一试就知道了阿!



fankobe 2006-04-18
  • 打赏
  • 举报
回复
虽然构造器是申明了private,但是dest方法没有,dest方法本身就有权限使用找个内部类的(原因就是他是找个方法的内部类),因此所dest方法可以创建内部类的对象。当然如果你直接通过构造器创建对象是不行的,以为外围类没有找个权限,所以通过dest方法就可以创建了,dest方法创建后就返回这个对象。
xiayunfei148 2006-04-18
  • 打赏
  • 举报
回复
PDestination是dest()的一部分,而不是Destination的一部分,这楼主首先应该知道.所以在dest之外不能访问PDestination,看一下return,dest这个方法返回对象,构造器是private的一定不能通过他来创建对象的,只能通过方法来返回了,
Singleton 设计模式的类构造器也是private的,也是通过一个方法来返回这个类对象的,不是类的构造函数申明了private就没办法来生成对象的.
dbyang 2006-04-18
  • 打赏
  • 举报
回复
同意楼上的说法。
zhudonhua 2006-04-18
  • 打赏
  • 举报
回复

还是不明白,楼上能不能说清楚一点啊??是不是打错字了啊??
熊云昆 2006-04-17
  • 打赏
  • 举报
回复
readLabel()函数不是private的,它当然可以返回值,包括private值,只是返回那个值而已。

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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