添加结构体组合, interface x : a + b {}
This commit is contained in:
30
ExampleCodes/3-Structure.fig
Normal file
30
ExampleCodes/3-Structure.fig
Normal file
@@ -0,0 +1,30 @@
|
||||
import std.io;
|
||||
|
||||
struct Point
|
||||
{
|
||||
x: Int; // type specifiers are optional
|
||||
y: Int; // type specifiers are optional
|
||||
|
||||
// x and y are private fields, can only reached by internal context
|
||||
|
||||
public func toString() -> String
|
||||
{
|
||||
return "(" + (x as String) + "," + (y as String) + ")";
|
||||
}
|
||||
// public func toString() {} is ok
|
||||
}
|
||||
|
||||
// make points
|
||||
|
||||
var p1 := new Point{1, 2};
|
||||
io.println(p1.toString()); // (1,2)
|
||||
|
||||
var p2 := new Point{x: 2, y: 3};
|
||||
io.println(p2.toString()); // (2,3)
|
||||
|
||||
var x := 114;
|
||||
var y := 514;
|
||||
|
||||
var p3 := new Point{y, x}; // shorthand mode, can be unordered, auto match field and variable!
|
||||
// = Point{x: x, y: y}
|
||||
io.println(p3.toString()); // (114,514)
|
||||
2
ExampleCodes/4-Interface.fig
Normal file
2
ExampleCodes/4-Interface.fig
Normal file
@@ -0,0 +1,2 @@
|
||||
import std.io;
|
||||
|
||||
Reference in New Issue
Block a user