一键导入
cangjie-interface
仓颉语言接口。当需要了解仓颉语言的接口定义、接口实现(单个/多个)、接口继承、默认实现(实例/静态)、sealed接口、泛型成员、Any类型、属性(prop)在接口中的使用、菱形继承解决方案等特性时,应使用此 Skill。
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
仓颉语言接口。当需要了解仓颉语言的接口定义、接口实现(单个/多个)、接口继承、默认实现(实例/静态)、sealed接口、泛型成员、Any类型、属性(prop)在接口中的使用、菱形继承解决方案等特性时,应使用此 Skill。
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
通用算法与数据结构实现参考——排序、搜索、动态规划、图算法、字符串处理的多语言惯用写法和模板代码(Python/C++/Java/Go/Cangjie)
仓颉语言算法与数据结构实现参考——排序、搜索、动态规划、图算法、字符串处理、集合操作的惯用写法和模板代码
仓颉编程语言基本概念和规则,当需要了解关键字、标识符、程序结构、变量定义(let/var/const)、值类型与引用类型、作用域规则、表达式(if/while/for-in/break/continue)、函数等基本概念时,应使用此 Skill
仓颉语言基本数据类型。当需要了解仓颉语言的整数、浮点、布尔、字符(Rune)、字符串(String)、Unit、Nothing、元组(Tuple)、数组(Array/VArray)、区间(Range)类型以及基本运算符的语法和规则时,应使用此 Skill。
仓颉程序与C程序互操作指导,包括foreign声明、CFunc、inout参数、unsafe块、调用约定、类型映射(基础类型/结构体/CPointer/VArray/CString)、C回调仓颉、内存管理等特性时,应使用此 Skill
仓颉语言类。当需要了解仓颉语言的类定义、抽象类、构造函数(init/主构造函数)、终结器(~init)、继承(单继承/sealed)、重写(override)、重定义(redef)、成员变量、成员函数、属性(prop)、访问修饰符、This类型、对象创建等特性时,应使用此 Skill。
| name | cangjie-interface |
| description | 仓颉语言接口。当需要了解仓颉语言的接口定义、接口实现(单个/多个)、接口继承、默认实现(实例/静态)、sealed接口、泛型成员、Any类型、属性(prop)在接口中的使用、菱形继承解决方案等特性时,应使用此 Skill。 |
interface I { ... } — 定义抽象类型,不包含数据,定义类型的行为public(不允许额外访问修饰符)。实现者须使用 publicinterface 隐式 open(open 修饰符可选)interface Flyable {
func fly(): Unit
}
class Bird <: Flyable {
public func fly(): Unit {
println("Bird flying")
}
}
class Airplane <: Flyable {
public func fly(): Unit {
println("Airplane flying")
}
}
// 接口作为参数类型,实现多态
func fly(item: Flyable): Unit {
item.fly()
}
main() {
fly(Bird()) // 输出:Bird flying
fly(Airplane()) // 输出:Airplane flying
}
sealed interfacesealed interface — 仅同包内可继承/实现/扩展sealed 隐含 public/open 语义sealed 接口的子接口仍可被 sealed 修饰或不使用 sealedsealed 接口的子接口被 public 修饰且不被 sealed 修饰,则该子接口可在包外被继承/实现/扩展package A
sealed interface Shape {
func area(): Float64
}
class Circle <: Shape {
var radius: Float64
public init(r: Float64) { radius = r }
public func area(): Float64 { 3.14159 * radius * radius }
}
// 包外不能实现 Shape(sealed),但可通过非 sealed 的 public 子接口间接实现
class Foo <: I { ... } — 须实现 I 的所有成员。Foo 成为 I 的子类型class C <: I1 & I2 { ... }(无顺序要求)interface Addable {
func add(other: Int64): Int64
}
interface Subtractable {
func sub(other: Int64): Int64
}
class MyInt <: Addable & Subtractable {
var value = 0
public func add(other: Int64): Int64 { value + other }
public func sub(other: Int64): Int64 { value - other }
}
cangjie-extension Skill)class,实现者可返回其子类mut 函数:接口中声明的 mut 函数,class 实现时忽略 mut 修饰(class 实例成员函数始终可修改实例状态),struct 实现时须匹配 mut 修饰符mut 修饰符须匹配;类型须相同override/redef 修饰符可选(无论接口中是否有默认实现)open class Base {}
class Sub <: Base {}
interface I {
func f(): Base
}
class C <: I {
public func f(): Sub { // 允许返回 Base 的子类型
Sub()
}
}
mut 函数实现示例interface Resettable {
mut func reset(): Unit
}
struct Counter <: Resettable {
var count: Int64 = 0
public mut func reset(): Unit { // struct 须匹配 mut
count = 0
}
}
class Logger <: Resettable {
var entries = 0
public func reset(): Unit { // class 忽略 mut
entries = 0
}
}
interface I3 <: I1 & I2 { ... }I3 须实现 I1、I2、I3 的所有成员interface Addable {
func add(other: Int64): Int64
}
interface Subtractable {
func sub(other: Int64): Int64
}
// Calculable 继承了 Addable 和 Subtractable,并添加新成员
interface Calculable <: Addable & Subtractable {
func mul(other: Int64): Int64
func div(other: Int64): Int64
}
class MyInt <: Calculable {
var value = 0
public func add(other: Int64): Int64 { value + other }
public func sub(other: Int64): Int64 { value - other }
public func mul(other: Int64): Int64 { value * other }
public func div(other: Int64): Int64 { value / other }
}
main() {
let myInt = MyInt()
// MyInt 同时是 Calculable、Addable、Subtractable 的子类型
let add: Addable = myInt
let calc: Calculable = myInt
}
override(实例函数)/ redef(静态函数)修饰符可选interface I1 {
func f(a: Int64): Int64 { a } // 有默认实现
func f1(a: Int64): Unit // 无默认实现
}
interface I2 <: I1 {
func f(a: Int64): Int64 { a + 1 } // 须提供新的默认实现
func f1(a: Int64): Unit {} // 可以仅声明,也可提供默认实现
}
interface SayHi {
func say(): String { "hi" }
}
class A <: SayHi {} // 继承默认实现
class B <: SayHi {
public func say(): String { "hi, B" } // 覆盖默认实现
}
func f<T>() where T <: I { T.staticFunc() }interface NamedType {
static func typename(): String {
"interface NamedType"
}
}
class A <: NamedType {}
main() {
println(NamedType.typename()) // 输出:interface NamedType
println(A.typename()) // 输出:interface NamedType
}
interface NamedType {
static func typename(): String
}
class A <: NamedType {
public static func typename(): String { "A" }
}
class B <: NamedType {
public static func typename(): String { "B" }
}
func printTypeName<T>() where T <: NamedType {
println("the type is ${T.typename()}")
}
main() {
printTypeName<A>() // 输出:the type is A
printTypeName<B>() // 输出:the type is B
}
interface SayHi {
func say(): String { "hi" }
}
interface SayHello {
func say(): String { "hello" }
}
// 两个接口都有 say 的默认实现,产生冲突
class Foo <: SayHi & SayHello {
public func say(): String { "Foo" } // 须自行实现,否则编译错误
}
open 语义的泛型实例/静态函数import std.collection.ArrayList
interface M {
func foo<T>(a: T): T
static func toString<T>(b: ArrayList<T>): String where T <: ToString
}
class C <: M {
public func foo<S>(a: S): S { a } // 泛型参数名可不同
public static func toString<T>(b: ArrayList<T>) where T <: ToString {
var res = ""
for (s in b) {
res += s.toString()
}
res
}
}
Any 类型interface Any {}Any;所有非接口类型默认实现 AnyAny 的子类型main() {
var any: Any = 1
any = 2.0
any = "hello, world!"
}
interface HasName {
prop name: String
mut prop id: Int64
}
class User <: HasName {
var _id: Int64 = 0
public prop name: String {
get() { "User" }
}
public mut prop id: Int64 {
get() { _id }
set(v) { _id = v }
}
}
mut,实现类型也须有 mut,类型须相同public,不可声明额外的访问修饰符publicinterface I {
func f(): Unit
}
open class C <: I {
protected func f() {} // ❌ Error: 须为 public
}