🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.lang.java
Article #18762

Re: [tij]����һ������

#18762
From: star�ı���
Date: Wed, 04 Feb 2004 06:15
66 lines
3179 bytes

Although it��s unnecessary, you can be more explicit using a cast, which has
the effect of calling toString( ):
��λ����������printhamster����ض�����˵�Ķ���ǰ���Ǹ�printer.
������λ����������һ��:
In general, however, you��ll want to do something more than call Object 
methods, so you��ll run up against the type-casting issue again. You must
assume you��ve gotten an Iterator to a sequence of the particular type you're 
interested in, and cast the resulting objects to that type (getting a 
run-time exception if you��re wrong). 

�� �� gjyalpha.bbs@freecity.cnzju.net (Java Enthusiast) �Ĵ������ᵽ: ��
: ��  ��: [tij]����һ������
: ����վ: Ʈ��ˮ�Ƽ� (Wed Feb  4 12:42:33 2004)
: ת��վ: NanKai!news.happynet.org!news.ccie.net.cn!Freecity
: 
: ��think in java 2 Ӣ�İ��457-458ҳ�����İ�329ҳ
: 
: ������һ������
: //: c09:HamsterMaze.java
: // From 'Thinking in Java, 2nd ed.' by Bruce Eckel
: // www.BruceEckel.com. See copyright notice in CopyRight.txt.
: // Using an Iterator.
: import java.util.*;
: 
: class Hamster {
:   private int hamsterNumber;
:   Hamster(int i) { hamsterNumber = i; }
:   public String toString() {
:     return "This is Hamster #" + hamsterNumber;
:   }
: }
: 
: class Printer {
:   static void printAll(Iterator e) {
:     while(e.hasNext())
:       System.out.println(e.next());
:   }
: }
: 
: public class HamsterMaze {
:   public static void main(String[] args) {
:     ArrayList v = new ArrayList();
:     for(int i = 0; i < 3; i++)
:       v.add(new Hamster(i));
:     Printer.printAll(v.iterator());
:   }
: } ///:~
: 
: Bruce˵��
: ��Ȼû�б�Ҫ������Ҳ�����ֶ����ת�ͣ�Ч��͵���toString()��ͬ
:      System.out.println((String)e.next());
: 
: ���Ǵ������Ǿ�ij�������䡣����ı�����û������ģ�����ִ���ڻ��д�ġ�
: ClassCastException��
: 
: 
: 
: 
: --
: gjyalpha ��˭��������gjyalpha ��
: 
: �� ��Դ:��Ʈ��ˮ�Ƽ� freecity.cnzju.net��[FROM: gjyalpha]
--
�� ��Դ:���Ұ��Ͽ�վ bbs.nankai.edu.cn��[FROM: 202.202.248.187] 
Technical Details
Message-ID: <4AWFVi$AaP@bbs.nankai.edu.cn>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!feed2.newsreader.com!newsreader.com!maily.cic.tsinghua.edu.cn!news.happynet.org!NanKai

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f6cb0,2363e8f6452ab7b5,start
X-Google-Attributes: gidf6cb0,public
X-Google-ArrivalTime: 2004-02-03 22:16:15 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!feed2.newsreader.com!newsreader.com!maily.cic.tsinghua.edu.cn!news.happynet.org!NanKai
From: abcdefghijkl.bbs@bbs.nankai.edu.cn (star�ı���)
Newsgroups: cn.bbs.comp.lang.java
Subject: Re: [tij]����һ������
Date: 04 Feb 2004 06:15:01 GMT
Organization: �Ұ��Ͽ�վ
Message-ID: <4AWFVi$AaP@bbs.nankai.edu.cn>
X-Filename: Java/M.1075875068.A