`
TracyKelvin
  • 浏览: 22639 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

Learning the Java Language (1.2)

    博客分类:
  • 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_for_the_Web_with_Servlets

    It supports the latest versions of the Java Servlet, JavaServer Pages (JSP), and Java Expression Language (EL) specifications. ##### Inside Servlets - **`javax.servlet` Package**: This package ...

    外文翻译 stus MVC

    JavaServer Pages (JSP) technology provides the glue between the page designer and the Java developer. If you have worked on a large-scale Web application, you understand the term change. Model-View-...

    Hello Android.rar

    4.1 Learning the Basics..... . 73 4.2 Adding Graphics to Sudoku.... 78 4.3 Handling Input.....87 4.4 The Rest of the Story..... 93 4.5 Making More Improvements.... 103 4.6 Fast-Forward >>.....103...

    Python程序设计(第二版).chm

    Climbing the GUI Learning Curve Section 6.5. The End of the Tutorial Section 6.6. Python/Tkinter for Tcl/Tk Converts Chapter 7. A Tkinter Tour, Part 1 Section 7.1. "Widgets and Gadgets and ...

    Scipy - Scipy Lecture Notes

    **1.2 The Scientific Python Ecosystem** The scientific Python ecosystem consists of a suite of tools and libraries that work together seamlessly to provide a comprehensive environment for scientific ...

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    3.5.2. Wiring with the Spring Expression Language 3.6. Summary Chapter 4. Aspect-oriented Spring 4.1. What is aspect-oriented programming? 4.1.1. Defining AOP terminology 4.1.2. Spring’s AOP support ...

    entity.framework.learning.guide

    在深入使用EF的过程中,开发者需熟悉CSDL(Conceptual Schema Definition Language)、MSL(Mapping Specification Language)及SSDL(Store-Specific Schema Definition Language)三种模式文件的加载与使用。...

    掌握Turing完成任务的XLNet模型.md

    XLNet(eXtreme Learning with Large-scale Unlabelled Data)是一种基于Transformer架构的语言模型,它引入了一种创新性的预训练方法——排列语言模型(Permutation Language Model, PLM)。这一方法不同于传统的自...

    C++PracticeC++PracticeC++PracticeC++Practice

    cout , I am learning C++ Language." ; return 0; } ``` **2. 编写 C++ 程序,显示您的姓名、班级、学号和通信地址。** ```cpp #include using namespace std; int main() { cout 姓名:张三" ; cout 班级:...

Global site tag (gtag.js) - Google Analytics