⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch36.htm

📁 Java_by_Example,初级经典例子哦,珍藏版本
💻 HTM
📖 第 1 页 / 共 4 页
字号:
</BLOCKQUOTE><P>Often, you can call static methods of the class, like <TT>parseInt()</TT>,to perform an operation on a value. But, you can also create objectsof the class and operate directly on that object's value. To giveyou some idea of what you can do with the wrapper classes, table36.2 lists the methods of the <TT>Integer</TT> class.<BR><P><CENTER><B>Table 36.2&nbsp;&nbsp;Methods of the Integer Class.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=224><I><B>Method</B></I></TD><TD WIDTH=274><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=224><TT>Integer(int)</TT></TD><TD WIDTH=274>One of the class's constructors.</TD></TR><TR VALIGN=TOP><TD WIDTH=224><TT>Integer(String)</TT></TD><TD WIDTH=274>One of the class's constructors.</TD></TR><TR VALIGN=TOP><TD WIDTH=224><TT>doubleValue()</TT> </TD><TD WIDTH=274>Returns the integer as a double value.</TD></TR><TR VALIGN=TOP><TD WIDTH=224><TT>equals(Object)</TT></TD><TD WIDTH=274>Compares the integer to another object.</TD></TR><TR VALIGN=TOP><TD WIDTH=224><TT>floatValue()</TT></TD><TD WIDTH=274>Returns the integer as a float value.</TD></TR><TR VALIGN=TOP><TD WIDTH=224><TT>getInteger()</TT></TD><TD WIDTH=274>Gets a property of an integer.</TD></TR><TR VALIGN=TOP><TD WIDTH=224><TT>hashCode()</TT></TD><TD WIDTH=274>Returns a hashcode for the integer.</TD></TR><TR VALIGN=TOP><TD WIDTH=224><TT>intValue()</TT></TD><TD WIDTH=274>Returns the integer as an int value.</TD></TR><TR VALIGN=TOP><TD WIDTH=224><TT>longValue()</TT></TD><TD WIDTH=274>Returns the integer as a long value.</TD></TR><TR VALIGN=TOP><TD WIDTH=224><TT>parseInt(String, int)</TT></TD><TD WIDTH=274>Converts a string to an int value.</TD></TR><TR VALIGN=TOP><TD WIDTH=224><TT>parseInt(String)</TT></TD><TD WIDTH=274>Converts a string to an int value.</TD></TR><TR VALIGN=TOP><TD WIDTH=224><TT>toString(int, int)</TT></TD><TD WIDTH=274>Converts an integer to a string.</TD></TR><TR VALIGN=TOP><TD WIDTH=224><TT>toString(int)</TT></TD><TD WIDTH=274>Converts an integer to a string.</TD></TR><TR VALIGN=TOP><TD WIDTH=224><TT>toString()</TT></TD><TD WIDTH=274>Converts an integer to a string.</TD></TR><TR VALIGN=TOP><TD WIDTH=224><TT>valueOf(String, int)</TT></TD><TD WIDTH=274>Creates an Integer object from a string.</TD></TR><TR VALIGN=TOP><TD WIDTH=224><TT>valueOf(String)</TT></TD><TD WIDTH=274>Creates an Integer object from a string.</TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleUsingtheDataTypeWrappers">Example: Using the Data-Type Wrappers</A></H3><P>Suppose that you need an integer data field in a class, but youwant to be able to use all of the <TT>Integer</TT> class's methodsin order to manipulate that value. First, you declare an objectof the <TT>Integer</TT> class and then call the class's constructor.Then, you can access the class's methods, as shown in Listing36.1. Figure 36.1 shows the applet running under Appletviewer.<P><A HREF="f36-1.gif"><B> Figure 36.1 : </B><I>This is IntApplet running under Appletviewer.</I></A><P><HR><BLOCKQUOTE><B>Listing 36.1&nbsp;&nbsp;IntApplet.java: Using the </B><I>Integer</I><B>Class.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;public class IntApplet extends Applet{    public void paint(Graphics g)    {        Integer value = new Integer(125);        long longValue = value.longValue();        float floatValue = value.floatValue();        String str = value.toString() + &quot;    &quot; +            String.valueOf(longValue) + &quot;    &quot; +            String.valueOf(floatValue);        g.drawString(str, 50, 75);    }}</PRE></BLOCKQUOTE><HR><P><IMG ALIGN=RIGHT SRC="pseudo.gif" HEIGHT=94 WIDTH=94 BORDER=1><BLOCKQUOTE>Tell Java that the applet uses the classes in the <TT>awt</TT>package.<BR>Tell Java that the applet uses the classes in the <TT>applet</TT>package.<BR>Derive the <TT>IntApplet</TT> class from Java's <TT>Applet</TT>.<BR>    Override the <TT>paint()</TT> method.<BR>        Create an <TT>Integer</TT> object with a value of 125.<BR>        Convert the integer to long and float values.<BR>        Create a display string and display it.</BLOCKQUOTE><H3><A NAME="TheISystemIClass">The <I>System</I> Class</A></H3><P>The <TT>System</TT> class enables you to make system-level functioncalls to do things like perform simple I/O, get the current time,handle directories, copy arrays, get environment variables, getinformation about memory, and so on. You would use the class'sI/O methods, for example, in a standalone applet in order to displaytext on the screen. Table 36.3 lists the more useful of the <TT>System</TT>class's methods and their descriptions.<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>In addition to its many methods, the <TT>System</TT> class also defines standard input and output streams that you can use in your programs. For example, when you call the <TT>println()</TT> method, you're using an output stream.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><BR><P><CENTER><B>Table 36.3&nbsp;&nbsp;The Most Useful Methods of the</B><I>System</I><B> Class.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=205><I><B>Method</B></I></TD><TD WIDTH=266><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=205><TT>arraycopy()</TT></TD><TD WIDTH=266>Copies an array.</TD></TR><TR VALIGN=TOP><TD WIDTH=205><TT>CurrentTimeMillis()</TT></TD><TD WIDTH=266>Gets the current time in milliseconds.</TD></TR><TR VALIGN=TOP><TD WIDTH=205><TT>ex0it(int)</TT></TD><TD WIDTH=266>Ends the program.</TD></TR><TR VALIGN=TOP><TD WIDTH=205><TT>GetProperties()</TT></TD><TD WIDTH=266>Returns the current system properties.</TD></TR><TR VALIGN=TOP><TD WIDTH=205><TT>GetProperty()</TT></TD><TD WIDTH=266>Returns a specific property.</TD></TR><TR VALIGN=TOP><TD WIDTH=205><TT>load()</TT></TD><TD WIDTH=266>Loads a dynamic library.</TD></TR><TR VALIGN=TOP><TD WIDTH=205><TT>setProperties()</TT></TD><TD WIDTH=266>Set the system properties.</TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleGettingSystemProperties">Example: Getting System Properties</A></H3><P>Frequently, it's handy to know something about the system on whichyour application is running. That's why the <TT>System</TT> classmakes it easy for you to find this information. Listing 36.2,for example, is a stand-alone application that displays Java'sversion, Java's class path, the OS name, and the OS version. Figure36.2 shows the output from the program.<P><A HREF="f36-2.gif"><B> Figure 36.2 : </B><I>The SystemApp application displays system properties</I></A><P><HR><BLOCKQUOTE><B>Listing 36.2&nbsp;&nbsp;SystemApp.java: An Application ThatDisplays SystemInformation.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>public class SystemApp{    public static void main(String args[])    {        System.out.println(&quot;&quot;);        System.out.println(&quot;------------------------------&quot;);        String str = &quot;Java Version: &quot; +            System.getProperty(&quot;java.version&quot;);        System.out.println(str);        str = &quot;Java Class Path: &quot; +            System.getProperty(&quot;java.class.path&quot;);        System.out.println(str);        str = &quot;OS Name: &quot; +            System.getProperty(&quot;os.name&quot;);        System.out.println(str);        str = &quot;OS Version: &quot; +            System.getProperty(&quot;os.version&quot;);        System.out.println(str);        System.out.println(&quot;------------------------------&quot;);    }}</PRE></BLOCKQUOTE><HR><P><IMG ALIGN=RIGHT SRC="pseudo.gif" HEIGHT=94 WIDTH=94 BORDER=1><BLOCKQUOTE>Declare the <TT>SystemApp</TT>.<BR>    Define the <TT>main()</TT> method.<BR>        Display blank and dashed lines.<BR>        Get and display the Java version number.<BR>        Get and display Java's class path setting.<BR>        Get and display the OS name.<BR>        Get and display the OS version number.<BR>        Display an ending dashed line.<BR></BLOCKQUOTE><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>The System class's getProperty() method accepts a string identifier for the property you want. The strings you can use are file.separator, java.class.path, java.class.version, java.home, java.vendor, java.vendor.url, java.version, line.separator, os.arch, os.name, os.version, path.separator, user.dir, user.home, and user.name.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="TheIMathIClass">The <I>Math</I> Class</A></H3><P>If you need to do a lot of mathematical work in your applets andapplications, you'll be glad to have the <TT>Math</TT> class atyour disposal. Using the <TT>Math</TT> class, you can performmany types of calculations just by calling the appropriate methods.Table 36.4 lists the <TT>Math</TT> class's methods and their descriptions:<BR><P><CENTER><B>Table 36.4&nbsp;&nbsp;Methods of the </B><I>Math</I><B>Class.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=166><I><B>Method</B></I></TD><TD WIDTH=424><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>abs()</TT></TD><TD WIDTH=424>Returns the absolute value of a given number.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>Acos()</TT></TD><TD WIDTH=424>Returns the arc cosine of a value.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>asin()</TT></TD><TD WIDTH=424>Returns the arc sine of a value.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>atan()</TT></TD><TD WIDTH=424>Returns the arc tangent of a value.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>atan2()</TT></TD><TD WIDTH=424>Converts rectangular coordinates to polar coordinates.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>ceil()</TT></TD><TD WIDTH=424>Returns the smallest whole number greater than or equal to the given value.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>cos()</TT></TD><TD WIDTH=424>Returns the cosine of an angle.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>floor()</TT></TD><TD WIDTH=424>Returns the largest whole number less than or equal to the given value.</TD></TR><TR VALIGN=TOP><TD WIDTH=166><TT>IEEEremainder()</TT></TD><TD WIDTH=424>Returns the remainder of a floating-point division.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -