AP计算机教程1-3:class的例子
class的例子
在Java中定义一个class需要用关键字public class
加上为class起的名字。class的主体需要用花括号{
和}
包括起来。
public class ClassName { }
以下是Java的一个示例class。 Java中的class可以包含field(数据或属性),constructor(初始化field的method),method(行为)以及可以用来进行测试的main method。不过这些项目中的任何一个对于class来说都不是必要的。以下内容可以通过编译(compile),但是如果你试图让计算机执行它,觉得会发生什么呢?
public class FirstName { }
class FirstClass
里面空空如也,所以当我们让计算机执行时,它也不知道该干什么。
当使用Java虚拟机执行一个class时,它将会从main
method开始。以下的class会在屏幕上和你打招呼。
public class SecondClass { public static void main(String[] args) { System.out.println("Hi there!"); } }
class SecondClass
并没有用到什么面向对象的原理。里面唯一的内容是作为static
method(这类method作用于class本身)的main
,而没有任何object method(这类方法可以被class的各个object调用)。现在让我们来创建一个每个object表示一个人的class。每人都有一个名字和一个手机号码,我们将把这些储存在class Person
的field里。我们还可以提供method来存取这些数据。除此之外,我们提供一个constructor来在object最初创建时将数据初始化。
public class Person { /// fields //////////////// private String name; private String cell; /////// constructors //////////////////// public Person(String theName, String theCell) { this.name = theName; this.cell = theCell; } //////////// methods /////////////////////// public String getName() { return this.name; } public void setName(String theName) { this.name = theName; } public String getCell() { return this.cell; } public void setCell(String theCell) { this.cell = theCell; } public String toString() { return "name: " + this.name + ", cell: " + this.cell; } //////////// main for testing ////////////// public static void main(String[] args) { Person p1 = new Person("Deja", "555 132-3253"); System.out.println(p1); Person p2 = new Person("Avery", "555 132-6632"); System.out.println(p2); } }
运行Java程序
当你执行Java程序时,你需要用某种方式把带有main
method的class名提供给Java虚拟机,之后程序就会从这里开始执行。对于面向对象程序,main
method应当创立执行任务的object并操作它们实现程序的功能。以下是class Person
的main
method。
//////////// main for testing ////////////// public static void main(String[] args) { Person p1 = new Person("Alex", "138 9392 3829"); System.out.println(p1); Person p2 = new Person("Jane", "189 8283 1256"); System.out.println(p2); }
可以为main
method创建一个专门的class,但这只是一个习惯问题。为每个class创建自己的main
method来测试相关的method和field也是毫无问题的。class Person
的main
method将会创立两个object并使用toString
method来显示他们的值。当执行System.out.println(object)
时,Java会自动调用该object的class中定义的toString
method。
可以尝试改变姓名和手机号码,然后再次运行,看看结果是否会有什么改变。
field——实例变量
field存储object的数据。为了让object能够完成程序给定的任务,需要把状态和信息记录到field里。field又被称作实例变量(Instance Variables)、对象变量(object variable)或属性(properties)。
在AP计算机考试中,你遇到的field都是private
的。可以把private
理解成你的日记,只有你自己能直接查看它。同理,private
field只能被class内部的代码调用。
class Person
声明了两个field,name
与cell
。前者指的是姓名,后者指的是手机号。这两项资料都是你可能需要了解的个人信息。
/// fields //////////////// private String name; private String cell;
constructor
constructor并不真的构造object(这是new
的工作)。object由class派生而来,之后执行constructor来初始化field或实例变量的值。在AP计算机考试中,你只和public
的constructor打交道。
class Person
有一个接受两个参数的constructor:存储为String
的姓名和存储为String
的手机号。constructor的鲜明特点是名字和class名完全一致,且没有返回类型。
/////// constructors //////////////////// public Person(String theName, String theCell) { this.name = theName; this.cell = theCell; }
method
method定义一个object能做什么或其行为。
在AP计算机考试中,你遇到的method都是public
的,可以在class之外调用。
class Person
有读取姓名/手机号以及存储姓名/手机号的method。从object中获得信息的method又称getter或accessor,给field赋值的方法又称setter或mutator。
//////////// methods /////////////////////// public String getName() { return this.name; } public void setName(String theName) { this.name = theName; } public String getCell() { return this.cell; } public void setCell(String theCell) { this.cell = theCell; } public String toString() { return "name: " + this.name + ", cell: " + this.cell; }
0 条评论