Mastering Cangjie Classes: From Definition to Object Creation
Object-oriented programming is a cornerstone of modern software development, and Cangjie provides robust support for classes that enable developers to build scalable, maintainable applications. In this first part of our series, we'll explore the fundamental concepts of Cangjie classes, from basic definitions to creating and using objects.
Understanding Classes vs Structs
Before diving into class definitions, it's crucial to understand the key differences between classes and structs in Cangjie:

Classes are reference types - When you assign a class instance to another variable, both variables point to the same object
Structs are value types - Assignment creates a copy of the struct
Classes support inheritance - You can create hierarchies of related classes
Structs don't support inheritance - They're standalone data structures

Defining Your First Class
A class definition in Cangjie starts with the class keyword, followed by the class name and a definition body enclosed in curly braces:
cangjieclass Rectangle {
let width: Int64
let height: Int64

public init(width: Int64, height: Int64) {
    this.width = width
    this.height = height
}

public func area() {
    width * height
}

}
This simple Rectangle class demonstrates the basic components:

Member variables (width and height)
Constructor (init function)
Member function (area)

Working with Member Variables
Cangjie classes support two types of member variables:
Instance Member Variables:

Accessed through object instances
Can be initialized with default values or in constructors
Use let for immutable or var for mutable variables

cangjieclass Rectangle {
let width = 10 // Default value
let height: Int64 // Must be initialized in constructor

init(h: Int64) {
    height = h
}

}
Static Member Variables:

Belong to the class itself, not instances
Accessed through the class name
Must have initial values if no static initializer exists

cangjieclass Rectangle {
let width = 10
static let height = 20
}

let l = Rectangle.height // l = 20
Static Initializers
For complex static variable initialization, Cangjie provides static initializers:
cangjieclass Rectangle {
static let degree: Int64

static init() {
    degree = 180
}

}
Important rules:

Only one static initializer per class
Must initialize all uninitialized static variables
Cannot be modified with access modifiers

Constructors: Building Objects
Cangjie supports both regular and primary constructors:
Regular Constructors:
cangjieclass Rectangle {
let width: Int64
let height: Int64

public init(width: Int64, height: Int64) {
    this.width = width
    this.height = height
}

public init(size: Int64) {
    this.width = size
    this.height = size
}

}
Primary Constructors:
Primary constructors simplify class definitions by combining parameter declaration with member variable definition:
cangjieclass Rectangle {
public Rectangle(let width: Int64, let height: Int64) {}
}
You can also mix member variable parameters with regular parameters:
cangjieclass Rectangle {
public Rectangle(name: String, let width: Int64, let height: Int64) {}
}
Constructor Execution Order
Understanding the order of operations during object creation is crucial:

Initialize member variables with default values (outside primary constructor)
Call parent class constructor (if not explicitly called)
Execute constructor body code

Finalizers: Cleanup Operations
Cangjie classes can define finalizers for cleanup when objects are garbage collected:
cangjieclass C {
var p: CString

init(s: String) {
    p = unsafe { LibC.mallocCString(s) }
}

~init() {
    unsafe { LibC.free(p) }
}

}
Finalizer limitations:

No parameters, return type, or modifiers
Cannot be called explicitly
Only non-open classes can have finalizers
Execution timing and thread are unpredictable

Creating and Using Objects
Once you've defined a class, creating objects is straightforward:
cangjielet r = Rectangle(10, 20)
Accessing members:
cangjielet r = Rectangle(10, 20)
let width = r.width // Access member variable
let height = r.height
let a = r.area() // Call member function
Modifying objects:
For mutable member variables, you can modify values after creation:
cangjieclass Rectangle {
public var width: Int64
public var height: Int64
// ... constructors
}

let r = Rectangle(10, 20)
r.width = 8 // Modify member variable
r.height = 24
Reference Behavior
Remember that classes are reference types. This means:
cangjievar r1 = Rectangle(10, 20)
var r2 = r1 // Both variables point to same object
r1.width = 8 // Changes affect both r1 and r2
let a1 = r1.area() // a1 = 192
let a2 = r2.area() // a2 = 192 (same object!)
Key Takeaways

Classes in Cangjie are reference types that support inheritance
Member variables can be instance-based or static
Constructors can be regular or primary, with primary constructors offering cleaner syntax
Static initializers handle complex static variable setup
Finalizers provide cleanup capabilities with specific limitations
Objects share references when assigned, unlike value types

In the next part of our series, we'll explore member functions, access modifiers, and the powerful This type that enables advanced object-oriented patterns.

posted on 2025-06-29 22:58  xsir01  阅读(8)  评论(0)    收藏  举报