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

📄 java入门(8).htm

📁 一本很容易入手
💻 HTM
📖 第 1 页 / 共 5 页
字号:
              <TBODY>
              <TR>
                <TD><SPAN class=titleblk>Java入门(8) 创建新类</SPAN><BR>
                  <BLOCKQUOTE class=sumblk><STRONG>摘要</STRONG><BR>
                    <P>  阅读完本章后,大家应能根据需要定义并使用一个用户类,并能够根据需求说明这个用户类的特性。另外,还应该能够使用继承的方法创建子类,实现类的最大化使用。</P>(2002-08-29 
                    14:10:16)</BLOCKQUOTE>
                  <HR width="98%" noShade SIZE=4>
                  <STRONG>By <A 
                  href="mailto:wing@linuxaid.com.cn">Wing</A></STRONG>, 出处:<A 
                  href="http://www.linuxaid.com.cn/articles/3/5/www.linuxaid.com.cn">fjxufeng</A><BR><BR><SPAN 
                  class=contentblk>
                  <P><B>  本章目标:</B></P>
                  <P>  阅读完本章后,大家应能根据需要定义并使用一个用户类,并能够根据需求说明这个用户类的特性。另外,还应该能够使用继承的方法创建子类,实现类的最大化使用。</P>
                  <P><B>  8.1 定义并使用一个新类</B></P>
                  <P><B>  传授新知</B></P>
                  <P>  在前面几章中,我们遇到了不少用Java语言编写的程序,它们可以分为两类:</P>
                  <P>  1) Java应用程序;</P>
                  <P>  2) Java小应用程序。</P>
                  <P>  它们都有一个共同的特点,那就是整个程序中只包含一个类。不同在于,这个唯一的类的定义不尽相同。</P>
                  <P>  在Java应用程序中,形如:</P>
                  <TABLE cellSpacing=0 cellPadding=5 width="100%" 
                  bgColor=#cccccc border=1>
                    <TBODY>
                    <TR>
                      <TD><PRE><CODE>
public class testBranch1
</CODE></PRE></TD></TR></TBODY></TABLE>
                  <P>  而在Java小应用程序中,则形如:</P>
                  <TABLE cellSpacing=0 cellPadding=5 width="100%" 
                  bgColor=#cccccc border=1>
                    <TBODY>
                    <TR>
                      <TD><PRE><CODE>
public class VariableScope extends Applet
</CODE></PRE></TD></TR></TBODY></TABLE>
                  <P>  其实,在这些程序中都使用到了其它类。在小应用程序中,很明显,所有的类都是从Applet类中继承来的。在应用程序中,也使用了其它的类,不过不是十分明显,大家想想,System.out.println是从哪来的呢?它是从其它类中继承过来的,它包含在java.lang包中,无须指明,编译器能够自动处理。</P>
                  <P>  Java语言的开发工具包(JDK)中就包含了许许多多的已开发定义的类,我们可以通过使用它们迅速地构建自己的程序。如果你能够熟练地使用它们就能够使用Java语言写出自己所需要的程序。而如果你想要成为Java语言的高手,就一定要学会自己定义类,以供今后的程序使用。下面我们一起来看一下下面这个例子。</P>
                  <P><B>  实例说明</B></P>
                  <P>  1.首先,我们使用文字编辑软件输入下面这个源程序。</P>
                  <P>  源程序:Birthday.java</P>
                  <TABLE cellSpacing=0 cellPadding=5 width="100%" 
                  bgColor=#cccccc border=1>
                    <TBODY>
                    <TR>
                      <TD><PRE><CODE>
public class Birthday 
{
public String year;
public String month;
public String day;
public Birthday()
{
year=”0000”;
month=”00”;
day=”00”;
}
public Birthday(String y,String m,String d)
{
year=y;
month=m;
day=d;
}
public String getBirthday()
{
String fullbirthday=month+”/”+”/”+day+”/”+year;
return fullbirthday;
}
}
</CODE></PRE></TD></TR></TBODY></TABLE>
                  <P>  2.编译这个程序,如果顺利完成,将在当前目录下生成一个名为Birthday.class的文件;</P>
                  <TABLE cellSpacing=0 cellPadding=5 width="100%" 
                  bgColor=#cccccc border=1>
                    <TBODY>
                    <TR>
                      <TD><PRE><CODE>
c:javastudy&gt; javac Birthday.java
</CODE></PRE></TD></TR></TBODY></TABLE>
                  <P>  3.然后输入以下命令,运行这个程序:</P>
                  <TABLE cellSpacing=0 cellPadding=5 width="100%" 
                  bgColor=#cccccc border=1>
                    <TBODY>
                    <TR>
                      <TD><PRE><CODE>
c:javastudy&gt; java Birthday
</CODE></PRE></TD></TR></TBODY></TABLE>
                  <P>  执行这个程序,将输出一些信息,如下图所示:</P><A 
                  href="Java入门(8).files/8-1.jpg"><IMG alt=8-1 
                  src="Java入门(8).files/8-1.jpg" width=450 border=0></A> 
                  <P><B>  图8-1 执行Birthday类的输出</B></P>
                  <P>  Java语言责怪我们没有定义main方法。</P>
                  <P>  4.接下来,我们再用文字编辑软件输入以下源程序。</P>
                  <P>  源程序:useBirthday.java</P>
                  <TABLE cellSpacing=0 cellPadding=5 width="100%" 
                  bgColor=#cccccc border=1>
                    <TBODY>
                    <TR>
                      <TD><PRE><CODE>
public class useBirthday 
{
public static void main(String argv[])
{
Birthday birthday1=new Birthday();
Birthday birthday2=new Birthday("1949","10","01");
System.out.println(birthday1.getBirthday());
System.out.println(birthday2.getBirthday());
}
}
</CODE></PRE></TD></TR></TBODY></TABLE>
                  <P>  3.使用javac编译后,执行以下命令,运行这个程序:</P>
                  <TABLE cellSpacing=0 cellPadding=5 width="100%" 
                  bgColor=#cccccc border=1>
                    <TBODY>
                    <TR>
                      <TD><PRE><CODE>
c:javastudy&gt; java useBirthday
</CODE></PRE></TD></TR></TBODY></TABLE>
                  <P>  执行这个程序,将输出一些信息,如下图所示:</P><A 
                  href="Java入门(8).files/8-2.jpg"><IMG alt=8-2 
                  src="Java入门(8).files/8-2.jpg" width=450 border=0></A> 
                  <P><B>  图8-2 执行useBirthday类的输出</B></P>
                  <P><B>  传授新知</B></P>
                  <P>  我们先来看一下第二个程序useBirthday.java,这是个Java应用程序,main方法中共有四条语句。前两条,看上去象是定义变量:</P>
                  <TABLE cellSpacing=0 cellPadding=5 width="100%" 
                  bgColor=#cccccc border=1>
                    <TBODY>
                    <TR>
                      <TD><PRE><CODE>
Birthday birthday1=new Birthday();
Birthday birthday2=new Birthday("1949","10","01");
</CODE></PRE></TD></TR></TBODY></TABLE>
                  <P>  但是,我们从来没有向大家介绍过Birthday这种变量类型呀!是的,在Java语言中并没有Birthday这种变量类型。</P>
                  <P>  可是这条语句在编译过程中并没有报错呀!这是因为,Java语言编译器在useBirtday.java程序所在目录中发现了包含Birthday类定义的Birthday类:Birthday.class。</P>
                  <P>  也就是说,这两条语句定义了2个属于Birthday类的对象:birthday1和birthday2。从Birthday.java程序中,我们可以看到Birthday类包含三个字符型成员变量:</P>
                  <TABLE cellSpacing=0 cellPadding=5 width="100%" 
                  bgColor=#cccccc border=1>
                    <TBODY>
                    <TR>
                      <TD><PRE><CODE>
public String year;
public String month;
public String day;
</CODE></PRE></TD></TR></TBODY></TABLE>
                  <P><B>  一些提示:</B></P>
                  <P>  我们将Birthday称为类,而将birthday1和 
                  birthday2称为对象。请大家一定要明明白白地知道其中原委。类是一类事物,而对象则是一个个体。</P>
                  <P>  成员变量是一个形象的术语,表示这些变量属于这个对象,属于这个类。</P>
                  <P>  同时,分别使用new Birthday()和new 
                  Birthday(“1949”,”10”,”01”)为它们赋初值。</P>
                  <P>  1)Birthday birthday1=new Birthday();</P>
                  <P>  在这一句中,调用的是Birthday类中的Birthday()方法,我们从Birthday.java中可以看到这个方法实现:</P>
                  <TABLE cellSpacing=0 cellPadding=5 width="100%" 
                  bgColor=#cccccc border=1>
                    <TBODY>
                    <TR>
                      <TD><PRE><CODE>
public Birthday()
{
year=”0000”;
month=”00”;
day=”00”;
}
</CODE></PRE></TD></TR></TBODY></TABLE>
                  <P>  也就是说,它将对象birthday1的三个成员变量year,month和day分别赋予了初值“0000”,“00”和“00”。</P>
                  <P>  2) Birthday birthday2=new Birthday("1949","10","01");</P>
                  <P>  在这一条语句中,虽然方法名同是Birthday,但它带上了参数,所以它调用的是Birthday类中的Birthday(String 
                  y,String m,String d)方法。这个方法的实现是:</P>
                  <TABLE cellSpacing=0 cellPadding=5 width="100%" 
                  bgColor=#cccccc border=1>
                    <TBODY>
                    <TR>
                      <TD><PRE><CODE>
public Birthday(String y,String m,String d)
{
year=y;
month=m;
day=d;
}
</CODE></PRE></TD></TR></TBODY></TABLE>
                  <P>  也就是将其所带的参数值,分别赋值给三个成员变量。在本例中,“1949”赋值给year,“10”赋值给month,“01”赋值给day。</P>
                  <P>  这个程序中另两条语句则十分相似:</P>
                  <TABLE cellSpacing=0 cellPadding=5 width="100%" 
                  bgColor=#cccccc border=1>
                    <TBODY>
                    <TR>
                      <TD><PRE><CODE>
System.out.println(birthday1.getBirthday());
System.out.println(birthday2.getBirthday());
</CODE></PRE></TD></TR></TBODY></TABLE>
                  <P>  很明显,这两条语句的用途是打印出birthday1和birthday2两个对象的一些信息。什么信息呢?我们可以发现这两条语句中都使用了Birthday类中的getBirthday方法。</P>
                  <P><B>  一些提示:</B></P>
                  <P>  大家注意观察在上面的语句是如何调用getBirthday方法的:</P>
                  <TABLE cellSpacing=0 cellPadding=5 width="100%" 
                  bgColor=#cccccc border=1>
                    <TBODY>
                    <TR>
                      <TD><PRE><CODE>
 birthday1.getBirthday() 
</CODE></PRE></TD></TR></TBODY></TABLE>
                  <P>  前面是对象名,后面是方法名,中间用“.”连接。请记住这个“.”,它经常被使用。形象的说,它就表示“的”。即birthday1的getBirthday方法。同样,我们可以使用这样在方法来访问它的成员变量:birthday1.year,即对象birthday1的成员变量year。</P>
                  <P><B>  我们就来看一下这个方法做了什么:</B></P>
                  <TABLE cellSpacing=0 cellPadding=5 width="100%" 
                  bgColor=#cccccc border=1>
                    <TBODY>
                    <TR>
                      <TD><PRE><CODE>
 public String getBirthday()
 {
 String fullbirthday=month+”/”+”/”+day+”/”+year;
 return fullbirthday;
 }
</CODE></PRE></TD></TR></TBODY></TABLE>
                  <P>  大家还记得“+”在字符串操作中的用途吧!对,我们在第6章中就跟大家说过,它是用来进行字符串合并的。我们通过month+”/”+”/”day+”/”+year语句,把birthday1和birthday2两个对象的三个成员变量组成了一个“月/日/年”的常用日期表示方法。</P>
                  <P>  然后,getBirthday方法将这个用“月/日/年”表示法表示的生日日期返回(使用return方法)给System.out.println,这样,我们就得到了如图8-2的输出:</P>
                  <TABLE cellSpacing=0 cellPadding=5 width="100%" 
                  bgColor=#cccccc border=1>
                    <TBODY>
                    <TR>
                      <TD><PRE><CODE>
00/00/0000
10/01/1949
</CODE></PRE></TD></TR></TBODY></TABLE>
                  <P>  通过上面的实例与讲解,我们可以得出创建一个新类的方法:</P>
                  <P>  1) 构思所需类的成员变量和成员方法;</P>
                  <P>  2) 用以下格式来编写类:</P>
                  <TABLE cellSpacing=0 cellPadding=5 width="100%" 
                  bgColor=#cccccc border=1>
                    <TBODY>
                    <TR>
                      <TD><PRE><CODE>
类修饰符 class 类名
{
成员变量定义;
……
成员方法定义;

⌨️ 快捷键说明

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