DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: kebingoo
今日帖子: 3
在线用户: 1
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 msdtx (yong) ★☆☆☆☆ -
普通会员
2021/11/9 22:48:06
标题:
怎么避免私有类型向外暴露 浏览:1659
加入我的收藏
楼主: 一直有这样一个问题,在使用类时,自己的private字段里可能用到不对外的数据结构,应该怎样申明才能不向外传递这些私有数据类型。
----------------------------------------------
-
作者:
男 roadrunner (roadrunner) ★☆☆☆☆ -
盒子活跃会员
2021/11/9 23:20:55
1楼: 就声明为private就能防止对外传递了
----------------------------------------------
-
作者:
男 doersoft (hnysoft.com) ★☆☆☆☆ -
普通会员
2021/11/9 23:46:58
2楼: Type
  AClass = class(XXX)
  private
    私有
  protected
    内部
  public
    公开
  end;

如果你说的是数据类型,可以不定义在Interface段,放到Impl实现段中,就嗵本单元内使用。
----------------------------------------------
delphi|vue|golang hnysoft|hnyerp+mes+srm
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2021/11/10 6:29:20
3楼: unit XYZ;
...
interface  // section to export to external units
...
type
... ABCexternal = class // class(EFG)
... private  ( all class in this unit )
... strict private ( just this class )
... protected (this unit and sub-class (inheriting) in another units
... published ( = public for Object Inspector use )
... public (all classes in all units )
...
implementation // section use internal this unit

type   // only this class
... ABCinternal = class // class(EFG)
... private
... strict private
... protected
... published
... public

end.
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 janker (janker) ★☆☆☆☆ -
盒子活跃会员
2021/11/10 13:20:15
4楼: 现在的语法还有 strict private,同一个单元的其它类都不能访问
----------------------------------------------
-
作者:
男 lookhere (lalala) ★☆☆☆☆ -
普通会员
2021/11/10 22:07:18
5楼: 就是这样,如果你说的是数据类型,可以不定义在Interface段,放到Impl实现段中,就嗵本单元内使用。
interface
type
  TClassA = class
  private
    fInner: Pointer
  public
    procedure aaa;
  end;
implementation
type
  TInnerType = class/record/xxx
    xxx: intger;
  end;

end;
----------------------------------------------
-
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2021/11/10 22:25:34
6楼: any FRIEND-class can access PRIVATE attributes/methods from MOTHER-class

->> to AVOID IT, do it "STRICT PRIVATE"

type
  MOTHER-CLASS....
   private  //<----------
      Mother-Fields...
  end;

   FRIEND-class = class(MOTHER-class)
   ...

implementation

FRIEND-class.Methods
begin
 showmessage( Mother-Fields )
end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
作者:
男 msdtx (yong) ★☆☆☆☆ -
普通会员
2021/11/14 12:22:29
7楼: 现在是在interface里申明一种兼容类型,implementation里复制出来用absolute还原。丑归丑,还能用。
传说中的名字空间污染...
----------------------------------------------
-
作者:
男 bluestorm8 (bluestorm) ▲▲△△△ -
普通会员
2021/11/15 12:23:36
8楼: 可以在implementation前只定义接口(interface), 类的定义放到implementation后面
----------------------------------------------
-
作者:
男 wr960204 (武稀松) ★☆☆☆☆ -
盒子活跃会员
2021/11/15 16:00:15
8楼: 声明接口或者抽象类,在implementation里面实现,或者在其他单元的implementation段实现
----------------------------------------------
武稀松http://www.raysoftware.cn
作者:
男 flcop (flcop) ▲▲▲▲△ -
普通会员
2021/11/15 22:31:47
9楼: 如果delphi的版本不低于delphi2005,可以使用嵌套类:

{ ... }

interface

type
  TMyClass = class
  private type
    TMyChild = class
    private
      FValue: Integer;
    end;
  private
    FMyChild: TMyChild;
  end;

implementation

{ ... }

当然,这种方式也有缺点,一是可读性下降了,2 二是一旦嵌套类可复用,比较难剥离,所以最好根据实际场景来权衡。
----------------------------------------------
-
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行60.54688毫秒 RSS