问题已经解决:
#lang racket
(define Person%
(class object%
(init-field name age) ; 定义初始化字段
(super-new) ; 调用父类的构造函数
; 定义公共方法来设置字段值
(define/public (set-name new-name)
(set! name new-name))
(define/public (set-age new-age)
(set! age new-age))
; 定义公共方法来获取字段值
(define/public (get-name)
name)
(define/public (get-age)
age)))
; 创建 Person 类的实例
(define person (new Person% [name "Alice"] [age 30]))
; 调用方法设置字段值
(send person set-name "Bob")
(send person set-age 25)
; 调用方法获取字段值并打印
(display (send person get-name)) ; 输出: Bob
(newline)
(display (send person get-age)) ; 输出: 25
(newline)
(define Student%
(class Person%
(init-field student-id) ; 添加额外的字段
(super-new) ; 调用父类的构造函数并传递参数
; 定义公共方法来获取 student-id 字段值
(define/public (get-student-id)
student-id)
; 覆盖父类的 get-info 方法,添加学生 ID 的信息
(define/public (get-info)
(format "Name: ~a, Age: ~a, Student ID: ~a" (send this get-name) (send this get-age) student-id))))
; 创建 Student 类的实例
(define student (new Student% [name "Bob"] [age 20] [student-id "S12345"]))
; 调用方法获取字段值并打印
(display (send student get-info)) ; 输出: Name: Bob, Age: 20, Student ID: S12345
(newline)
【 在 zhangxp024 的大作中提到: 】
: AI给出的示例程序:
: #lang racket
: (define Person%
: ...................
--
FROM 223.215.89.*