Сценарий: Наследование
Этот коммит содержится в:
родитель
d340d11c77
коммит
ca4c4a0fd6
3 изменённых файлов: 58 добавлений и 7 удалений
|
@ -19,9 +19,10 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
type class struct {
|
type class struct {
|
||||||
name string
|
name string
|
||||||
Struct *ast.StructType
|
baseInterface string
|
||||||
methods []*ast.FuncDecl
|
Struct *ast.StructType
|
||||||
|
methods []*ast.FuncDecl
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClass(name string) *class {
|
func NewClass(name string) *class {
|
||||||
|
@ -33,6 +34,7 @@ func (c *class) Name() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *class) String() (code string) {
|
func (c *class) String() (code string) {
|
||||||
|
c.checkForBaseInterface()
|
||||||
code += c.classDefinitionToString()
|
code += c.classDefinitionToString()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -42,7 +44,13 @@ func (c *class) MethodsString() (code string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *class) classDefinitionToString() (code string) {
|
func (c *class) classDefinitionToString() (code string) {
|
||||||
code = "class " + c.name + " {"
|
code += "class " + c.name
|
||||||
|
|
||||||
|
if len(c.baseInterface) > 0 {
|
||||||
|
code += ": public " + c.baseInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
code += " {"
|
||||||
code += "public:\n"
|
code += "public:\n"
|
||||||
|
|
||||||
code += c.structToString()
|
code += c.structToString()
|
||||||
|
@ -54,6 +62,27 @@ func (c *class) classDefinitionToString() (code string) {
|
||||||
code += "};\n"
|
code += "};\n"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
func (c *class) checkForBaseInterface() {
|
||||||
|
if c.Struct.Fields.NumFields() == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(c.Struct.Fields.List[0].Names) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
n := c.Struct.Fields.List[0].Names[0]
|
||||||
|
if n.Name != "_baseInterface" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
i, ok := c.Struct.Fields.List[0].Type.(*ast.Ident)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.baseInterface = i.Name
|
||||||
|
return
|
||||||
|
}
|
||||||
func (c *class) structToString() (code string) {
|
func (c *class) structToString() (code string) {
|
||||||
code += handleExpr(c.Struct)
|
code += handleExpr(c.Struct)
|
||||||
code += "\n"
|
code += "\n"
|
||||||
|
|
|
@ -6,9 +6,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleExpr(expr ast.Expr) string {
|
func handleExpr(expr ast.Expr) (code string) {
|
||||||
// spew.Dump(expr)
|
// spew.Dump(expr)
|
||||||
code := ""
|
|
||||||
switch e := expr.(type) {
|
switch e := expr.(type) {
|
||||||
case *ast.BasicLit:
|
case *ast.BasicLit:
|
||||||
code += handleBasicLit(e)
|
code += handleBasicLit(e)
|
||||||
|
@ -31,7 +30,7 @@ func handleExpr(expr ast.Expr) string {
|
||||||
case *ast.ArrayType:
|
case *ast.ArrayType:
|
||||||
code += handleArray(e)
|
code += handleArray(e)
|
||||||
}
|
}
|
||||||
return code
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleBasicLit(bl *ast.BasicLit) string {
|
func handleBasicLit(bl *ast.BasicLit) string {
|
||||||
|
|
|
@ -50,6 +50,29 @@ public:
|
||||||
virtual void Add(int) = 0;
|
virtual void Add(int) = 0;
|
||||||
virtual void Add2(int,double) = 0;
|
virtual void Add2(int,double) = 0;
|
||||||
};
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
Сценарий: Наследование
|
||||||
|
* Исходник:
|
||||||
|
```
|
||||||
|
package test
|
||||||
|
|
||||||
|
type Device interface {
|
||||||
|
}
|
||||||
|
|
||||||
|
type DeviceClass struct {
|
||||||
|
_baseInterface Device
|
||||||
|
}
|
||||||
|
```
|
||||||
|
* Результат:
|
||||||
|
```
|
||||||
|
class Device {
|
||||||
|
public:
|
||||||
|
};
|
||||||
|
class DeviceClass: public Device {
|
||||||
|
public:
|
||||||
|
Device* _baseInterface;
|
||||||
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
Сценарий: Класс: свойство типа интерфейс - указатель
|
Сценарий: Класс: свойство типа интерфейс - указатель
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче