Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » Java与OOP初步  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请教:内部类的问题
午夜蓝调





发贴: 6
积分: 0
于 2009-10-17 18:19 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

class P {
class Contents{
  private int i=11;
  public int value(){return i;}
  }
class Destination{
  private String label;
  Destination(String whereto){
    label=whereto;
    }
  String readLabel(){return label;}
}

public void ship(String dest){
  Contents c=new Contents();
  Destination d=new Destination(dest);
  System.out.println(d.readLabel());
}
public Destination to(String s) {
  return new Destination(s);
  }
public Contents cont(){
  return new Contents();
  
}
public static void main(String[] args) {
P p=new P();
p.ship("sdfsdfsdf");
P q=new P();
P.Contents c=q.cont();
P.Destination d=q.to("78888");
  }

}

这个程序中
public Destination to(String s) {
  return new Destination(s);
  }
public Contents cont(){
  return new Contents();
  
}
这段声明to和cont方法是什么方法?为什么要声明? 前面为什么还要各跟一个Destination和Contents?请教各位高手,谢谢


JiafanZhou edited on 2009-10-23 18:48

作者 Re:请教:内部类的问题 [Re:午夜蓝调]
JiafanZhou



版主


发贴: 736
积分: 61
于 2009-10-23 19:01 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
午夜蓝调 wrote:
这段声明to和cont方法是什么方法?为什么要声明? 前面为什么还要各跟一个Destination和Contents?请教各位高手,谢谢

to() and cont() are methods no difference from other methods of the P class.

PS: consider to give your classes and methods meaningful names will help others to understand code.

Destination and Contents are inner classes of P class. (as your title has already suggested). Some attentions need to be noted when using inner classes.
- Inner classes are members of the class who contains them. So you should just treat them as members.
- Inner classes have visibility control like other members. (you can declare inner classes as private, public and even static)
- The compiled inner class will have the following name: xxx$yyy (where xxx is the "base" class, and yyy is the inner class)
- Invoke the inner classes the same way as invoke a method or a field.
-...



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923