您的位置:首页 > 编程语言 > Java开发

《Java编程技巧1001条》第374-401条:小心使用依赖日期的函数,

2017-12-21 13:57 169 查看



《Java编程技巧1001条》第9部分 Java日期函数 第374条 小心使用依赖日期的函数,
 374 Beware When Relying on Date Formats
374 记住与Date格式相关的时刻
When youwork with the Date class object, be aware that Sun has reported several bugs inthe Date class. One bug involves the toString method, which gives differentoutput on different platforms. If your program parses the string or relies onthe presence of specific fields, you should carefully verify that the stringscontain the text you expect. For example, on a Sun Solaris computer, callingthe Date class toString method will result in this output:
当你利用Date类对象工作时,必须留心Sun公司已报导的Date类中的几个漏洞. 一个漏洞包含在toString方法中,它在不同的平台上会有不同的结果.如果你的程序要分析有关的string,或者依赖于所得string的某些域,则你必须小心验证你所期望的文本字符串. 例如,在Sun的Solaris计算机上, 调用Date类的toString方法将产生以下结果:
 Tue Sep 10 12:22:56PDT 1996
 
OnMicrosoft Windows NT and Windows 95, the toString method produces the followingdate string:
而在MicrosoftWindows NT 和 Windows 95上,toString方法将生成以下日期字符串:
 Tue Sep 10 12:22:561996
 


401 Storing Different Objects in an Array
401 在一个数组中存放不同的对象
The arrays you have created in the previous tips only contain a single type 
of object. As it turns out, you can also create arrays that hold many 
different types of objects. The most generic array that you can declare in 
Java is of type Object. Every Java object is a subclass of the Object 
class. Within an array, you can store any Java objects reference. However, 
to retrieve and use an object from an Object array, you must use the 
instanceof operator. The following code demonstrates how you can store 
images and rectangles in a single array:
前面你所创建的各种数组仅包含单种类型的对象. 当需要时,你也可以创建包含多种对
象类型的数组. 在Java中,你可以声明的最根本的数组是Object型数组. 每一个Java
对象是Object类的子类. 在一数组中,你可以用来存放任意的Java对象引用. 但是,为了
从Object数组中抽取(retrieve)和使用(use)一个object,你必需使用instanceof函数.
以下这些语句说明了怎样在单一的数组中存放图象和矩形:
import java.awt.*;
import java.awt.image.*;
import java.applet.*;

public class arrayOfObjectsApplet extends Applet {

   Object array[] = new Object[4];

   public void init()
     {
       array[0] = getImage(getCodeBase(), "a.gif");
       array[1] = getImage(getCodeBase(), "b.gif");
       array[2] = new Rectangle(0,26,26, 26);
       array[3] = new Rectangle(26,0,26, 26);
     }

   public void paint(Graphics g)
     {
      g.setColor(Color.black);

      for (int i = 0; i < array.length; i++)
        {
          if (array[i] instanceof Image)
            {
              Image img = (Image) array[i];
              g.drawImage((Image)array[i],i*26,i*26,this);
            }
          else if (array[i] instanceof Rectangle)
            {
              Rectangle r = (Rectangle) array[i];
              g.fillRect(r.x, r.y, r.width, r.height);
            }
        }
     }
  }
As you have learned, Java applets use interfaces to allow one class to 
access the data and methods of another. Sometimes, you will find it more 
convenient to create an interface that all objects in an array implement. 
In this way, you can then call the methods of the interface without having 
to know what the objects are.
正如你所了解的,Java应用块利用interfaces(接口)使一个类能访问另一个类的数据
和方法. 有时候,你会发现建立一个接口把所有对象都放在一数组工具中将更方便.
这样,你不需要知道对象是什么就可以调用接口方法. 

1001 Java Programming Tips
Page 
PAGE
18
 of 
numpages 
12

filename 
jtip050b.doc

TIME \@ "MMMM d, yyyy"
August 11, 1996

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: