关于JAVA类的资料翻译

原文链接
What Is a Class?
In the real world, you’ll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.

The following Bicycle class is one possible implementation of a bicycle:

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);
}

}
The syntax of the Java programming language will look new to you, but the design of this class is based on the previous discussion of bicycle objects. The fields cadence, speed, and gear represent the object’s state, and the methods (changeCadence, changeGear, speedUp etc.) define its interaction with the outside world.

You may have noticed that the Bicycle class does not contain a main method. That’s because it’s not a complete application; it’s just the blueprint for bicycles that might be used in an application. The responsibility of creating and using new Bicycle objects belongs to some other class in your application.

Here’s a BicycleDemo class that creates two separate Bicycle objects and invokes their methods:

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();
}

}

The output of this test prints the ending pedal cadence, speed, and gear for the two bicycles:

cadence:50 speed:10 gear:2
cadence:40 speed:20 gear:3
译文:
什么是类?
在现实生活中,你经常会找到许多个别的对象都是相同的。实际上可能存在成千上万辆自行车,他们出自相同的制造商以及模具都是相同的。每辆自行车可能是通过相同的设备模板制造,因此他们也拥有相同的部件。在面向对象的术语里,我们称你的自行车被认为是自行车类中的一个实例对象。一个类是个别的对象被创建的模板。
下面的自行车类是一辆自行车的可能实现:
Java编程语言的语法可能对你来说比较陌生,但是这个类的设计是基于之前对自行车类的讨论。速度、节奏以及档位字段表现的是这个对象的状态,而方法(改变节奏,改变档位,提速等等。)定义的是它与外部世界的相互交流。
你可能会注意到自行车类没有包含一个主方法。这是因为他不是一个完整的应用;它只是一个可能会用于应用中的自行车模型。创建以及使用一个新的自行车对象的职责属于应用程序中的其他类。
这是一个用于创建两个单独的自行车对象以及调用他们方法的自行车Demo。
这个测试输出打印了结束时两辆自行车踏板的节奏,速度,以及档位。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值