《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
相关文章推荐
- (转)C#日期函数使用大全
- c#疑难(二)之Access数据库日期数据类型的使用方法和其日期函数简介
- Calendar使用--> 获取星期几、一周日期的函数, calendar.add(Calendar.DATE, 1);日期加1
- 有关日期的SAP函数使用
- SQL中CONVERT(转化日期类型、格式化日期)函数使用
- JSP中有关时间和日期类的使用与JSP中的字符替换函数 str_replace()实现
- 使用函数优化字符串转数字/日期……,非数字将返回空
- 改善C++ 程序的150个建议学习之建议20:使用memcpy()系列函数时要足够小心
- TO_DATE()函数:日期转换时使用RR格式的注意事项
- SAP 使用较频繁的日期时间处理函数总结
- Lua使用os.date函数也要小心 推荐
- javascript中不存在块级作用域,所以要小心使用在块级作用域中的函数声明所带来的作用域混乱.
- Excel使用TEXT()函数使日期保持为日期格式显示,concatenate()
- 在函数内的 'rand' 中对带副作用的或依赖于时间的运算符的使用无效
- SAP 使用较频繁的日期时间处理函数总结
- sql日期相关函数的使用方法
- SAP 使用较频繁的日期时间处理函数总结
- 对于日期指定部分的加减 使用DATEADD函数就可以轻松实现
- javascript日期格式化函数,跟C#中的使用方法类似