gpt4 book ai didi

flutter - 有没有办法在 dart 中实现内部类?

转载 作者:行者123 更新时间:2023-12-04 14:55:36 26 4
gpt4 key购买 nike

关闭。这个问题是opinion-based .它目前不接受答案。












想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题.

5 个月前关闭。




Improve this question



//access to a Constant if it was like another language 
ImageWeather.Desktop.cloud;

//What i wanted ( not working because there is no inner class)
class ImageWeather {
class Desktop {
static const String cloud = 'assets/images/DesktopCloud.png';
static const String noCloud = 'assets/images/DesktopNoCloud.png';
static const String rain = 'assets/images/DesktopRain.png';
}
class Phone{
static const String cloud = 'assets/images/PhoneCloud.png';
static const String noCloud = 'assets/images/PhoneNoCloud.png';
static const String rain = 'assets/images/PhoneRain.png';
}
}

//access to a String like an inner class with new code
ImageWeather.desktop.cloud;


class ImageWeather {
static final Desktop desktop = Desktop();
static final Phone phone = Phone();
}

class Desktop {
get cloud => 'assets/images/DesktopCloud.png';
get noCloud => 'assets/images/DesktopNoCloud.png';
get rain => 'assets/images/DesktopRain.png';
}
class Phone {
get cloud => 'assets/images/PhoneCloud.png';
get noCloud => 'assets/images/PhoneNoCloud.png';
get rain => 'assets/images/PhoneRain.png';
}
我目前正在学习 Dart,但我发现我不能使用内部类。
有没有办法在 Dart 中编写类似于内部类的东西?

最佳答案

使用内部类是好是坏是主观的。不同的开发者会有不同的看法。
但是,如果您想制作 DesktopPhone仅对 ImageWeather 可见,您可以在单个包中编写三个类并重命名“内部”类 _Desktop_Phone ,从而使它们仅在该包中可见。

关于flutter - 有没有办法在 dart 中实现内部类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68085754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com