Home > Java > javaTutorial > body text

How to define Java classes and member variable declarations?

WBOY
Release: 2023-04-24 08:25:06
forward
1382 people have browsed it

Declare class

Define class:

class MyClass { 
    // 字段、构造函数和
    // 方法声明
}
Copy after login

This is a class declaration. The class body (the area between the curly braces) contains all the code that provides the life cycle of the objects created from the class:

Constructors for initializing new objects, fields that provide the state of the class and its objects declarations, and methods to implement the behavior of the class and its objects.

The preceding class declaration is minimal. It contains only those components declared by the required classes. You can provide more information about the class at the beginning of the class declaration, such as the name of its superclass, whether it implements any interfaces, etc.

For example:

class MyClass extends MySuperClass implements YourInterface { 
    // 字段、构造函数和
    // 方法声明
}
Copy after login

MyClass is a subclass of MySuperClass and it implements the YourInterface interface.

You can also add modifiers such as public or private at the beginning. Modifiers public and private, which determine whether other classes can access MyClass.

Generally speaking, a class declaration can include these components, in order:

Modifiers, such as public, private, and other modifiers. (However, please note that the private modifier can only be applied to nested classes.)

Class name, the first letter is capitalized by convention.

The name of the parent class (superclass) of the class (if any), preceded by the keyword extends. A class can only extend (subclass) one parent class.

Comma-separated list of interfaces implemented by the class (if any), preceded by the keyword implements. A class can implement multiple interfaces.

Class body, enclosed in curly brackets {}.

Declare member variables

There are several types of variables:

Member variables in the class - these are called for the field.

Variables within a method or code block - these are called local variables.

Variables in method declarations--these are called parameters.

This class Bicycle uses the following lines of code to define its fields:

public int cadence;
public int gear;
public int speed;
Copy after login

The field declaration consists of three parts, in order:

Zero or more modifiers, such as public or private.

Type of field.

The name of the field.

The public keyword identifies these fields as public members, accessible to any object with access to the class.

Access Modifiers

The first (leftmost) modifier used allows you to control what other classes can access a member field.

public modifier - This field is accessible from all classes.

Private modifier——This field can only be accessed within its own class.

protected modifier - can only be accessed in its own package. In addition, its class can also be accessed by subclasses in another package.

public class Bicycle {
    private int cadence;
    private int gear;
    private int speed;
        
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }
    public int getCadence() {
        return cadence;
    }
        
    public void setCadence(int newValue) {
        cadence = newValue;
    }
        
    public int getGear() {
        return gear;
    }
        
    public void setGear(int newValue) {
        gear = newValue;
    }
            public int getSpeed() {
        return speed;
    }
    public void applyBrake(int decrement) {
        speed -= decrement;
    }
    public void speedUp(int increment) {
        speed += increment;
    }
}
Copy after login

Type

All variables must have a type. You can use primitive types such as int, float, boolean, etc. Or you can use a reference type such as a string, array, or object.

Variable Name

All variables, whether fields, local variables or parameters, follow the same naming rules introduced in the language basics course "Variables - Naming" and Agreement.

Method names and class names use the same naming rules and conventions, except for the following differences

The first letter of the class name should be capitalized, and the first letter of the method name should be capitalized. The first (or only) word should be a verb.

The above is the detailed content of How to define Java classes and member variable declarations?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template