github.com/suiyunonghen/DxCommonLib@v0.5.3/BaseObject.go (about)

     1  package DxCommonLib
     2  
     3  type GDxBaseObject struct {
     4  	fSubChilds []interface{} //子对象
     5  	UseData		interface{}  //用户数据
     6  }
     7  
     8  type IDxInheritedObject interface {
     9  	SubChild(idx int) interface{}
    10  	SubChildCount() int
    11  	Destroy()
    12  	SubInit()
    13  	LastedSubChild()interface{}				//最后一个继承者
    14  }
    15  
    16  
    17  func (obj *GDxBaseObject) SubInit(subObj interface{}) {
    18  	if obj.fSubChilds == nil {
    19  		obj.fSubChilds = make([]interface{}, 3)
    20  	}
    21  	for _, v := range obj.fSubChilds {
    22  		if v == subObj {
    23  			return
    24  		}
    25  	}
    26  	obj.fSubChilds = append(obj.fSubChilds, subObj)
    27  }
    28  
    29  func (obj *GDxBaseObject)LastedSubChild() interface{}  {
    30  	if obj.fSubChilds == nil{
    31  		return  nil
    32  	}
    33  	return obj.fSubChilds[len(obj.fSubChilds)-1]
    34  }
    35  
    36  func (obj *GDxBaseObject)Free()  {
    37  	//执行Destroy过程
    38  	if i := obj.SubChildCount() - 1; i >= 0{
    39  		obj.SubChild(i).(IDxInheritedObject).Destroy()
    40  
    41  	}else{
    42  		obj.Destroy()
    43  	}
    44  }
    45  
    46  func (obj *GDxBaseObject)Destroy()  {
    47  	//释放的过程,后面继承的重写此方法则可
    48  
    49  }
    50  
    51  func (obj *GDxBaseObject) SubChild(idx int) interface{} {
    52  	if obj.fSubChilds == nil {
    53  		return nil
    54  	}
    55  	if idx >= 0 && idx < len(obj.fSubChilds) {
    56  		return obj.fSubChilds[idx]
    57  	}
    58  	return nil
    59  }
    60  
    61  func (obj *GDxBaseObject) SubChildCount() int {
    62  	if obj.fSubChilds == nil {
    63  		return 0
    64  	}
    65  	return len(obj.fSubChilds)
    66  }