d如何从内部构访问外部类私变量

原文

class X {
    private int num;

    struct Y {
        // 如何访问num?
    }
}

我相信这对于嵌套/内部类是可能的,但我不知道是否对于结构体是可能的.

对构不成立,可这样绕过:

class X {
    private int num;

    struct Y {
        X outer;//显式引用.
        int fun() { return outer.num; }
    }
}

outer如何成为X的引用?自动吗?

根据用途,要return Y(this)创建构,
这样:

class X {
    private int num;

    struct Y {
        X outer;
        int fun() { return outer.num; }
    }

    Y y;
    this(){
        y = Y(this);
    }
}

void main(){
    import std.stdio : writeln;
    auto x = new X();
    x.num = 10;
    writeln(x.num);
    writeln(x.y.fun());
}
posted @   zjh6  阅读(6)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示