Cava classes and objects

更新时间:
复制 MD 格式

Cava supports object-oriented programming (OOP) with a syntax similar to Java and C#. Use the class keyword to define a class, and the new keyword to create objects from it.

Cava supports the following OOP concepts:

  • Encapsulation

  • Abstraction

  • Class

  • Object

  • Instance

  • Method

A Cava class consists of four components:

  • Member variables — hold the data of each object instance

  • Constructors — initialize an object when it is created

  • Member functions — expose operations on the object's data

  • Class functions — belong to the class itself, not to any instance; defined with static

Quick start

The following example defines an Example class, creates an instance, and reads its PI member variable:

class Example {
    double PI;
    Example() {
        PI = 3.1415926;
    }
    static int main() {
        Example example = new Example();  // (1)
        double a = example.PI;            // (2)
        return 0;
    }
}
#Description
1Declare example, instantiate an Example object with new, and call the constructor to initialize it
2Access the PI member variable — a is 3.1415926

Define a class

Use the class keyword followed by the class name and a body enclosed in {}.

class Example {
    public double PI;  // (1)
    Example() {        // (2)
        PI = 3.1415926;
    }
    double getPI() {   // (3)
        return PI;
    }
    static int main() {
        Example example = new Example();
        double a = example.getPI();
        return 0;
    }
}
#Description
1Member variable — holds data for each instance
2Constructor — runs automatically when the object is created
3Member function — lets callers read the member variable

Create an object

Creating an object involves three steps:

  1. Declaration — declare a variable and specify its type.

  2. Instantiation — use the new keyword to allocate the object.

  3. Initialization — specify a constructor after new to set up the object's initial state.

Example example = new Example();

After the object is created, use the variable to access its member variables and member functions.

Member variables

Member variables hold the data of their object. Declare them inside the class body with a type and a name.

class Example {
    double PI;  // (1)
    Example() {
        PI = 3.1415926;  // (2)
    }
}
#Description
1Declare PI as a double member variable
2Initialize PI in the constructor — the only valid place to set an initial value
Note

Assigning a value at the point of declaration is not supported. Always initialize member variables in a constructor.

// Invalid: cannot assign a value when declaring a member variable
class Example {
    double PI = 3.1415926;
}

Constructors

A constructor runs automatically after an object is created. Two rules apply:

  • Its name must match the class name exactly.

  • It has no return value.

Cava supports multiple constructors (constructor overloading). The new expression determines which constructor is called based on the arguments passed.

class Example {
    public double PI;
    Example() {              // (1)
        this.PI = 3.1415926;
    }
    Example(double customPI) {  // (2)
        this.PI = customPI;
    }
    double getPI() {
        return PI;
    }
    static int main() {
        Example example1 = new Example();      // (3)
        double a = example1.getPI();
        Example example2 = new Example(3.14);  // (4)
        double b = example2.getPI();
        return 0;
    }
}
#Description
1Constructor A — no parameters; sets PI to 3.1415926
2Constructor B — accepts a customPI parameter
3Calls Constructor A; a is 3.1415926
4Calls Constructor B with 3.14; b is 3.14

Member functions

A member function encapsulates operations on the object's data. Member variables are directly accessible inside a member function by name.

If a local variable inside the function shares a name with a member variable, the local variable takes precedence. Prefix the member variable name with this. to access it explicitly.

class Example {
    public double PI;
    Example() {
        this.PI = 3.1415926;
    }
    void getPI() {       // (1)
        return PI;       // (2)
    }
    static int main() {
        Example example = new Example();
        double a = example.getPI();  // (3)
        return 0;
    }
}
#Description
1Declare a member function getPI()
2Access the member variable directly — no this. needed when there is no name conflict
3Call the member function through the object

Member functions support overloading: define multiple functions with the same name but different parameter types or counts. The compiler selects the best match based on the call's arguments.

Class functions

A class function belongs to the class itself, not to any individual instance. Define it with static and call it directly on the class name — no object required.

class Example {
    static double getPI() {      // (1)
        return 3.1415926;
    }
    static int main() {
        double a = Example.getPI();  // (2)
        return 0;
    }
}
#Description
1A class function: static means it belongs to the class
2Call the class function directly on the class name, without creating an object

Instance members vs. class members

Member variable / Member functionClass function (static)
Belongs toEach object instanceThe class itself
AccessThrough an object: example.getPI()Through the class: Example.getPI()
SharedEach instance has its own copyShared across all instances

Limitations

Cava does not support class inheritance.