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

📄 prog.html

📁 java 基础的 一点东西,,可以看看
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD>  <TITLE>Java(TM) Language Basics, Part 1, Lesson 2: Building Applications</TITLE>  <META NAME="AUTHOR" CONTENT="Monica Pawlan">  <META NAME="KEYWORDS" CONTENT="programming, basics, Java 2">  <META NAME="OWNER" CONTENT="Editorial/JDC">  <META NAME="revision" CONTENT="@(#)prog.src	1.32 03/09/00  JDC">    <STYLE TYPE="text/css">    <!--     CODE {font-family: Courier, Monospace;          font-size: 12pt}    PRE {font-family: Courier, Monospace;         font-size: 11pt}      -->  </STYLE></HEAD><!-- Start Body Insert--><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" ALINK="#FF0000" VLINK="#660066"><!-- End Body Insert--><!-- Start PageTop Insert --><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">  <TR ALIGN="CENTER" VALIGN="TOP">    <TD WIDTH="157" ALIGN="LEFT">    <IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT="">    <A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A>    <BR>    <IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD>    <TD>    <FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html">    <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">      <TR>        <TD ALIGN="RIGHT">        <IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>                <TD WIDTH="100%">                <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">          <TR>            <TD BGCOLOR="#CC9966" WIDTH="100%">            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>          </TR>                    <TR>            <TD>            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>          </TR>                    <TR>            <TD BGCOLOR="#CC9966">            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>          </TR>                    <TR>            <TD>            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>          </TR>                    <TR>            <TD BGCOLOR="#CC9966">            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>          </TR>                    <TR>            <TD>            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>          </TR>                    <TR>            <TD BGCOLOR="#CC9966">            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>          </TR>        </TABLE>        </TD>                <TD ALIGN="LEFT">        <IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>        <TD>                <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">          <TR>            <TD VALIGN="CENTER">            <A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD>                          <TD VALIGN="CENTER">            <FONT FACE="Helvetica" SIZE="1">            <INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD>                        <TD VALIGN="CENTER">            <INPUT TYPE="image" SRC="/images/search.button.gif" WIDTH="55" HEIGHT="14" ALT="Search" value="search" BORDER="0"></TD>          </TR>        </TABLE>        </TD>      </TR>    </TABLE>    </FORM>    <P>    <TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0">      <TR VALIGN="TOP">        <TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55"           HEIGHT="18" ALT=""></TD>                  <TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152">        <A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" HEIGHT="42" WIDTH="319" ALT="Java Developer Connection(SM)"></A></TD>      </TR>            <TR VALIGN="TOP">        <TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%">        <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>      </TR>            <TR VALIGN="TOP">        <TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%">        <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>      </TR>            <TR VALIGN="TOP">        <TD><A HREF="/developer/onlineTraining/"><IMG SRC="/images/online-training.gif" WIDTH="165" HEIGHT="22" ALT="Online Training" BORDER=0></A></TD>      </TR>    </TABLE>    </TD>  </TR></TABLE><!-- End PageTop Insert --><!-- Start NavBar Insert --><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT"><!-- tab categories --><TR><TD><A HREF="http://java.sun.com/products/"><IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD></TR><TR><TD><A HREF="/developer/index.html"><IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Java Developer Connection"></A></TD></TR><TR><TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD></TR><TR><TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Online Support"></A></TD></TR><TR><TD><A HREF="/developer/community/index.html"><IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/industry/"><IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="News &amp; Events from Everywhere"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/solutions"><IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Products from Everywhere"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/casestudies"><IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD></TR><TR><TD>    <DIV ALIGN="RIGHT">    <A HREF="/servlet/PrintPageServlet"><IMG SRC="/images/printbutton.gif" WIDTH="155" HEIGHT="25" ALT="Print Button" BORDER="0"></A>    </DIV></TD></TR><TR><TD>&nbsp;</TD></TR><!-- End NavBar Insert --><!-- START SUB-NAV --><TR>  <TD><!-- INSERT SUB-NAV INFO -->  </TD></TR><!-- END SUB-NAV --></TABLE><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR><TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><!-- Template Version 2.0 --><!-- ================== --><!-- Start Main Content --><!-- ================== --><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR><TD VALIGN="TOP"><FONT FACE="Verdana, Arial, Helvetica, sans-serif">  <A NAME="top"></A>  <DIV ALIGN="RIGHT">  <FONT SIZE="-1"><A HREF="/developer/onlineTraining/index.html">Training Index</A></FONT>  <H2>Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> Programming Language Basics, Part 1<BR>  <FONT SIZE="3">Lesson 2: Building Applications</FONT></H2>    <FONT SIZE="-1">[<A HREF="compile.html">&lt;&lt;BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="applet.html">NEXT&gt;&gt;</A>]</FONT></DIV><P>All programs written in the Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> language (Java programs) are built from classes. Because all classes have the same structure and share common elements, all Java programs are very similar. <P>This lesson describes the structure and elements of a simple application created from one class. The next lesson covers the samematerial for applets. <UL>   <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">   <A HREF="#class">Application Structure and Elements</A></FONT>   <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">   <A HREF="#fields">Fields and Methods</A></FONT>   <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">   <A HREF="#const">Constructors</A></FONT>   <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">   <A HREF="#more">More Information</A></FONT></UL><HR><A NAME="class"></A><H3>Application Structure and Elements</H3><IMG SRC="./Art/class.gif" WIDTH="183" HEIGHT="147" ALT="" ALIGN=LEFT HSPACE=10>An application is created from classes. A <CODE>class</CODE> is similar to a <CODE>RECORD</CODE>in the Pascal language or a <CODE>struct</CODE> in theC language in that it stores related data in <I>fields</I>,where the fields can be different types. So you could, forexample, store a text string in one field, an integer in another field, and a floating point in a third field. The difference between a class and a <CODE>RECORD</CODE> or<CODE>struct</CODE> is that a class also defines the <I>methods</I> to work on the data. <P>For example, a very simple class might store a string of text and define one method to set the string and another method to get the string and print it to the console. Methods that work on the data are called <I>accessor</I> methods. <P><IMG SRC="./Art/main.gif" WIDTH="183" HEIGHT="219" ALT="" ALIGN=LEFT HSPACE=10>Every application needs one class with a <CODE>main</CODE> method. This class is the entry point for the program, and is the class name passed to the<CODE>java</CODE> interpreter command to run the application. <P>The code in the <CODE>main</CODE> method executes first when the program starts, and is the control point from which the controllerclass accessor methods are called to work on the data.<P>Here, again, is the <A HREF="./Code/ExampleProgram.java">example program</A> from Lesson 1. It has no fields or accessor methods, but because it is the only class in theprogram, it has a <CODE>main</CODE> method.<BR CLEAR=ALL></FONT><PRE>   class ExampleProgram {      public static void main(String[] args){        System.out.println(&quot;I'm a Simple Program&quot;);      }   }</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><P><A NAME="instance"></A>The <CODE>public static void</CODE> keywords mean the Java<A HREF="#TJVM"><SUP>1</SUP></A> virtualmachine (JVM) interpreter can call the program's <CODE>main</CODE> method to start the program (public)without creating an instance of the class (static), and the programdoes not return data to the Java VM interpreter (void) when itends.<P><IMG SRC="./Art/instance.gif" WIDTH="210" HEIGHT="165" ALT="" ALIGN=LEFT HSPACE=10>An instance of a class is an executablecopy of the class While the class describes thedata and behavior, you need a class instance toacquire and work on data.The diagram at the left shows three instances ofthe <CODE>ExampleProgram</CODE> class by the names: <CODE>FirstInstance</CODE>, <CODE>SecondInstance</CODE>and <CODE>ThirdInstance</CODE>. <P>The <CODE>main</CODE> method is static to give the Java VMinterpreter a way to start the class without creating aninstance of the control class first.  Instances ofthe control class are created in the <CODE>main</CODE>method after the program starts.<P>The <CODE>main</CODE> method for the simple example does not create an instance of the<CODE>ExampleProgram</CODE> class because none is needed. The <CODE>ExampleProgram</CODE> class has noother methods or fields, so no class instance is needed to access them from the <CODE>main</CODE> method. The Java platform lets you execute a class without creating aninstance of that class as long as its static methods do not call any non-staticmethods or fields.<P>The <CODE>ExampleProgram</CODE> class just calls <CODE>println</CODE>, which is a static method in the<CODE>System</CODE> class. The <CODE>java.lang.System</CODE> class, among other things, providesfunctionality to send text to the terminal window where the program was started.It has all static fields and methods.<P>The static fields and methods of a class can be called by another program withoutcreating an instance of the class. So, just as the Java VM interpreter commandcould call the <CODE>static main</CODE> method in the <CODE>ExampleProgram</CODE> class without creating an instance of the <CODE>ExampleProgram</CODE> class, the <CODE>ExampleProgram</CODE> class can call the

⌨️ 快捷键说明

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