【翻译】The Java Tutorial中的Class概念
2009-02-23 18:16
357 查看
原文链接地址:
Lesson: Object-Oriented Programming Concepts
What Is a Class?
原文:
What Is a Class?
A class is a blueprint or prototype from which objects are created. This section defines a class that models the state and behavior of a real-world object. It intentionally focuses on the basics, showing how even a simple class can cleanly model state and behavior.什么是类?
类是对象的蓝图或者原型。这一部分会详细说明类是怎样模拟现实世界中对象的状态和行为的。它是基础,它会告诉你就是一个简单的类也能很好地模拟对象的状态和行为。
在现实世界中,你会发现很多独特的对象具有相同的种类。比如成千上万辆自行车,制作方法和模型都是相同的。每一个辆自行车都是从相同的设备和图纸中制造出来的,所以具有相同的组件。在面向对象的术语中,我们就说这辆自行车是自行车这个对象的中的一个实例。类就是对象的蓝图。
下面给出自行车这个类的用法:
class Bicycle {
int cadence = 0;
int speed = 0;
int gear = 1;
void changeCadence(int newValue) {
cadence = newValue;
}
void changeGear(int newValue) {
gear = newValue;
}
void speedUp(int increment) {
speed = speed + increment;
}
void applyBrakes(int decrement) {
speed = speed - decrement;
}
void printStates() {
System.out.println("cadence:"+cadence+" speed:"+speed+" gear:"+gear);
}
}
上面的Java语法可能对于来说是很新鲜的,但是这个类的设计是基于前面所讨论的自行车对象。字段cadence,speed和gear都是对象的状态,方法changeCadence,changeGear,seepUp等等定义了与外界的交互。
你可能注意到自行车类不包含main方法。这是因为上面的不是一个完整的应用程序,它仅仅是可能会在自行车对象中被使用的蓝图。它的功能是创建一个自行车的新对象,这个对象可以在其他的程序中使用。
下面这个BicycleDemo类就是创建两个单独的自行车对象,还要调用它们的方法:
class BicycleDemo {
public static void main(String[] args) {
// Create two different Bicycle objects
Bicycle bike1 = new Bicycle();
Bicycle bike2 = new Bicycle();
// Invoke methods on those objects
bike1.changeCadence(50);
bike1.speedUp(10);
bike1.changeGear(2);
bike1.printStates();
bike2.changeCadence(50);
bike2.speedUp(10);
bike2.changeGear(2);
bike2.changeCadence(40);
bike2.speedUp(10);
bike2.changeGear(3);
bike2.printStates();
}
}
其打印的结果是:
cadence:50 speed:10 gear:2
cadence:40 speed:20 gear:3
相关文章推荐
- 【翻译】The Java Tutorial中的Object概念
- 【翻译】The Java Tutorial中的"继承"概念
- Python Programming with the Java(TM) Class Libraries: A Tutorial for Building Web and Enterprise App
- The Java EE 5 Tutorial 中文版 (翻译) (第三章)
- The Java EE 6 Tutorial 中文版 (翻译) 第32章 JPA简介 (一) 实体
- Theano Tutorial文档翻译(一):词汇
- The type java.util.Optional cannot be resolved. It is indirectly referenced from required .class....
- If the only tool you have is Java, everything looks like a class
- java.lang.LinkageError: JAXB 2.0 API is being loaded from the bootstrap classloader, but this RI(xxx) needs 2.1 API
- Unable to find a javac compiler; com.sun.tools.javac.Main is not on the classpath.Perhaps JAVA_HOME
- Apache OFBiz Development The Beginners Tutorial 翻译之四 运行OFBiz
- Eclipse error: The type java.lang.Object cannot be resolved. It is indirectly referenced from the required .class files
- javax.el.PropertyNotFoundException: The class 'java.lang.String' does not have the property 'XXX'
- The Java EE 6 Tutorial Invoking Other Web Resources
- The Java EE 6 Tutorial Finalizing a Servlet
- java.lang.LinkageError: JAXB 2.0 API is being loaded from the bootstrap classloader, but this RI ***
- Java中Could not find the main class.program will exit和java.lang.UnsupportedClassVersionError 的解决方法
- 继续The JavaTM Tutorial之旅。
- 什么是异常,我们为什么要关心它--The Java Tutorial--Exception第一节
- The longest class name in Java and .NET Framework