Сценарий: Наследование
Этот коммит содержится в:
родитель
d340d11c77
коммит
ca4c4a0fd6
3 изменённых файлов: 58 добавлений и 7 удалений
|
@ -20,6 +20,7 @@ var (
|
|||
|
||||
type class struct {
|
||||
name string
|
||||
baseInterface string
|
||||
Struct *ast.StructType
|
||||
methods []*ast.FuncDecl
|
||||
}
|
||||
|
@ -33,6 +34,7 @@ func (c *class) Name() string {
|
|||
}
|
||||
|
||||
func (c *class) String() (code string) {
|
||||
c.checkForBaseInterface()
|
||||
code += c.classDefinitionToString()
|
||||
return
|
||||
}
|
||||
|
@ -42,7 +44,13 @@ func (c *class) MethodsString() (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 += c.structToString()
|
||||
|
@ -54,6 +62,27 @@ func (c *class) classDefinitionToString() (code string) {
|
|||
code += "};\n"
|
||||
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) {
|
||||
code += handleExpr(c.Struct)
|
||||
code += "\n"
|
||||
|
|
|
@ -6,9 +6,8 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
func handleExpr(expr ast.Expr) string {
|
||||
func handleExpr(expr ast.Expr) (code string) {
|
||||
// spew.Dump(expr)
|
||||
code := ""
|
||||
switch e := expr.(type) {
|
||||
case *ast.BasicLit:
|
||||
code += handleBasicLit(e)
|
||||
|
@ -31,7 +30,7 @@ func handleExpr(expr ast.Expr) string {
|
|||
case *ast.ArrayType:
|
||||
code += handleArray(e)
|
||||
}
|
||||
return code
|
||||
return
|
||||
}
|
||||
|
||||
func handleBasicLit(bl *ast.BasicLit) string {
|
||||
|
|
|
@ -50,6 +50,29 @@ public:
|
|||
virtual void Add(int) = 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;
|
||||
};
|
||||
```
|
||||
|
||||
Сценарий: Класс: свойство типа интерфейс - указатель
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче