官术网_书友最值得收藏!

  • The Java Workshop
  • David Cuartielles Andreas G?ransson Eric Foster Johnson
  • 571字
  • 2021-06-11 13:05:21

Introduction

A Java class is a template that is used to define data types. Classes are composed of objects carrying data and methods that are used to perform operations on that data. Classes can be self-contained, extend other classes with new functionalities, or implement features from other classes. In a way, classes are categories that allow us to define what kind of data can be stored within them, as well as the ways in which that data can be handled.

Classes tell the compiler how to build a certain object during runtime. Refer to the explanation of what objects are in the Working with Objects in Java topic.

The basic structure of a class definition looks like this:

class <name> {

    fields;

    methods;

}

Note

Class names should start with a capital letter, as in TheClass, Animal, WordCount, or any other string that somehow expresses the main purpose of the class. If contained in a separate file, the filename containing the source should be named like the class: TheClass.java, Animal.java, and so on.

The Anatomy of a Class

There are different software components in classes. The following example shows a class that includes some of the main ones.

Example01.java

1  class Computer {

2      // variables

3      double cpuSpeed;  // in GHz

4  

5      // constructor

6      Computer() {

7          cpuSpeed = 0;

8      }

9  

10     //methods

11     void setCpuSpeed ( double _cpuSpeed ) {

12         cpuSpeed = _cpuSpeed;

13     }

The outcome of this example is:

2.5

Process finished with exit code 0

The previous code listing shows the definition of a basic class called Computer, which includes variables and methods to deal with one of the properties of the class computer – in this case, cpuSpeed. The code shows two different classes. The first one is the blueprint for how to define objects of the Computer type in your programs. The second one, Example01, is the one that will be executed after compilation and will make an instance of the Computer class in the form of an object called myPC.

There is one more component inside the class, the constructor, which is optional, as Java includes a default constructor for all your classes. Constructors are used to initializing the basic properties of classes, and so are used when assigning values to variables, for instance. In our case, the operation performed by the constructor is initializing the cpuSpeed variable with a value of 0:

// constructor

Computer() {

    cpuSpeed = 0;

}

It is also possible for constructors to have parameters. You could have the constructor of the class be this:

// constructor

Computer( double _c ) {

    cpuSpeed = _c;

}

In this way, you could call the constructor with:

Computer myPC = new Computer( 2.5 );

That would also require a parameter. In addition, classes can have more than one constructor. This will be explained later in the chapter.

主站蜘蛛池模板: 策勒县| 长汀县| 泾阳县| 龙南县| 沁阳市| 乃东县| 老河口市| 安多县| 于田县| 宁南县| 东方市| 开封县| 荆门市| 罗田县| 庄河市| 杨浦区| 天津市| 庆安县| 库车县| 泰宁县| 晋州市| 宜良县| 东辽县| 安仁县| 景谷| 夏邑县| 乌鲁木齐县| 阿勒泰市| 应城市| 南木林县| 淮北市| 渝中区| 奈曼旗| 余江县| 邛崃市| 龙陵县| 九江县| 徐闻县| 竹北市| 华池县| 来宾市|