Java教程2:创建基础类
在第一个教程中,我们了解了如何创建基本的Java程序,并讨论了“原始”数据类型。
在本教程中,我们将对原始程序进行一些扩展,以便从基本类创建一个对象。
在面向对象编程中,程序被认为是由各种相互作用的“对象”组成的。对象由称为类的模板组成,每个类通常会定义一些数据和一些方法,这些
方法基本上是包含实现类行为的代码的过程。
让我们修改教程1中的BASIC程序,以便从我们定义的类中实际创建一个新对象。
public class Animal {
public static void main(String [] args) {
Animal animal = new Animal();
}
}
此程序不输出任何内容。像我们以前的程序一样,它定义了一个类(称为“Animal”)。当您编译此代码并运行它时,Java将查找“main”方法并
执行它(此“main”方法在类中的位置没有区别,但如果您想将其作为程序执行,则该类必须有一个)。然后,main方法声明一个’animal’类型
的变量。然后,它使用new关键字创建一个“Animal”类类型的新对象,并将其分配给变量。让我们向Animal类添加一些数据。我们将添加一个包含一些数据的字符串变量。我们还将向该类添加一个名为“Speak”的“方法”。该方法将实现Animal类的某些行为;也就是说,它将显示我们要添加的字符串。请注意,该方法看起来有点像“main”方法。“main”方法实际上只是一个特别命名的方法,声明为“static”(稍后将详细介绍)。尽管现在这看起来很复杂,但随着您对Java的熟悉和练习创建自己的类,它会变得更加清晰。
public class Animal {
// Declare and initialize some String data.
private String voice = "meouww";
public static void main(String [] args) {
// Declare a variable called 'animal' of the type 'Animal'.
// Initialize the variable with a new 'Animal'.
Animal animal = new Animal();
// Call (run) the speak() method.
animal.speak();
}
// Define a 'speak' method, which does nothing but
// print (i.e. display) the String data.
public void speak() {
System.out.println(voice);
}
}
meouww
// Not allowed; voice is declare private
System.out.println(animal.voice);
…但我们可以写:
// Allowed; speak is declare public.
animal.speak();
…因为speak()方法声明为Public,因此可以从类外部使用。
构造函数
通常,您希望在从类创建对象时运行一些初始化代码。您可以使用一种称为构造函数的特殊方法来实现这一点。构造函数在创建对象时运行。构造函数必须与类同名,并且必须声明为public。它不能有返回类型(我们还没有讨论返回类型!)。
在这里,我们更改了示例,以便构造函数运行一些代码。但大多数情况下,构造函数只是用于初始化实例变量(初始化意味着给出变量的实际值)。在许多情况下,我们也可以在声明实例变量的同时,甚至在构造函数运行之前初始化实例变量。我们已经用我们的私有“voice”变量做到了这一点,将其赋值为“meouww”。
public class Animal {
// Declare and initialize some String data.
private String voice = "meouww";
// This is a special method called a 'constructor'.
// It runs automatically when any object is created
// from the class.
public Animal() {
System.out.println("Constructor running!");
}
// The program starts here, even though this is by no
// means the first line in the file, or even in the class.
public static void main(String [] args) {
// Declare a variable called 'animal' of the type 'Animal'.
// Initialize the variable with a new 'Animal'.
Animal animal = new Animal();
// Call (run) the speak() method.
animal.speak();
}
// Define a 'speak' method, which does nothing but
// print (i.e. display) the String data.
public void speak() {
System.out.println(voice);
}
}
Constructor running!
meouww
关于字符串的一些注意事项
在显示字符串数据之前,我们使用字符串类型来保存字符串数据。事实上,每当您在Java中键入两个语音标记并将它们分配给一个字符串时,您就创建了一个新的字符串对象。这是Java内置的一个非常方便的快捷方式。您还可以使用加号将字符串添加在一起。然而,要小心!在内存或处理能力紧张的情况下,或者在具有大量迭代的循环中,您必须始终记住,键入两个语音标记并将它们分配给一个字符串,或者将它们添加到现有字符串中,实际上会创建一个新的String对象,而String对象是相当重要的对象。这里有一个程序可以说明这一点。
public class Animal {
// The program starts here
public static void main(String [] args) {
// Declare a variable called 'animal' of the type 'Animal'.
// Initialize the variable with a new 'Animal'.
Animal animal = new Animal();
// Call (run) the speak() method.
animal.speak();
}
public void speak() {
// This looks harmless and is not unreasonable, but keep in mind
// that each time we use the + we are creating a new string, NOT
// simply increasing the size of the existing string.
String sum = "hello" + " " + "there";
System.out.println(sum);
// In fact the above code is equivalent to the following:
// Declare and initialize some strings.
String string1 = "hello";
String string2 = " "; // This string is just a space
String string3 = "there";
String sum2 = string1 + string2 + string3;
System.out.println(sum2);
}
}
hello there
hello there
常见问题FAQ
- 程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
- 请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!