Dart 如何在颤振中分割飞镖类?
我做了以下测试,但不起作用:Dart 如何在颤振中分割飞镖类?,dart,flutter,Dart,Flutter,我做了以下测试,但不起作用: //main.dart class Test { static const a = 10; final b = 20; final c = a+1; } //part.dart part of 'main.dart'; class Test { final d = a +1; //<---undefined name 'a' } //main.dart 课堂测试 { 静态常数a=10; 最终b=20; 最终c=a+1; } //
//main.dart
class Test
{
static const a = 10;
final b = 20;
final c = a+1;
}
//part.dart
part of 'main.dart';
class Test
{
final d = a +1; //<---undefined name 'a'
}
//main.dart
课堂测试
{
静态常数a=10;
最终b=20;
最终c=a+1;
}
//飞镖
“主飞镖”的一部分;
课堂测试
{
final d=a+1;//Dart不支持分部类。part
和part
将库拆分为多个文件,而不是一个类
Dart中的私有(标识符以\u
开头)是每个库,通常是*.Dart
文件
main.dart
part 'part.dart';
class Test {
/// When someone tries to create an instance of this class
/// Create an instance of _Test instead
factory Test() = _Test;
/// private constructor that can only be accessed within the same library
Test._();
static const a = 10;
final b = 20;
final c = a+1;
}
part of 'main.dart';
class _Test extends Test {
/// private constructor can only be called from within the same library
/// Call the private constructor of the super class
_Test() : super._();
/// static members of other classes need to be prefixed with
/// the class name, even when it is the super class
final d = Test.a +1; //<---undefined name 'a'
}
part 'section.dart';
class _PageState extends State<Page> {
build(BuildContext context) {
// ...
_buildSection(context);
// ...
}
}
part of 'page.dart';
extension Section on _PageState {
_buildSection(BuildContext context) {
// ...
}
}
部分省道
part 'part.dart';
class Test {
/// When someone tries to create an instance of this class
/// Create an instance of _Test instead
factory Test() = _Test;
/// private constructor that can only be accessed within the same library
Test._();
static const a = 10;
final b = 20;
final c = a+1;
}
part of 'main.dart';
class _Test extends Test {
/// private constructor can only be called from within the same library
/// Call the private constructor of the super class
_Test() : super._();
/// static members of other classes need to be prefixed with
/// the class name, even when it is the super class
final d = Test.a +1; //<---undefined name 'a'
}
part 'section.dart';
class _PageState extends State<Page> {
build(BuildContext context) {
// ...
_buildSection(context);
// ...
}
}
part of 'page.dart';
extension Section on _PageState {
_buildSection(BuildContext context) {
// ...
}
}
main.dart'的一部分;
类测试扩展了类测试{
///只能从同一库中调用私有构造函数
///调用超类的私有构造函数
_测试():超级;
///其他类的静态成员需要加前缀
///类名,即使它是超级类
final d=Test.a+1;//我面临同样的问题。我基于扩展的变体:
page.dart
part 'part.dart';
class Test {
/// When someone tries to create an instance of this class
/// Create an instance of _Test instead
factory Test() = _Test;
/// private constructor that can only be accessed within the same library
Test._();
static const a = 10;
final b = 20;
final c = a+1;
}
part of 'main.dart';
class _Test extends Test {
/// private constructor can only be called from within the same library
/// Call the private constructor of the super class
_Test() : super._();
/// static members of other classes need to be prefixed with
/// the class name, even when it is the super class
final d = Test.a +1; //<---undefined name 'a'
}
part 'section.dart';
class _PageState extends State<Page> {
build(BuildContext context) {
// ...
_buildSection(context);
// ...
}
}
part of 'page.dart';
extension Section on _PageState {
_buildSection(BuildContext context) {
// ...
}
}
我只是用像Swift这样的扩展关键字来扩展它
// class_a.dart
class ClassA {}
// class_a+feature_a.dart
import 'class_a.dart';
extension ClassA_FeatureA on ClassA {
String separatedFeatureA() {
// do your job here
}
}
请忽略编码约定,这只是一个示例。如果对私有成员的访问不是问题,您可以导入其他文件。我的回答没有回答哪部分问题,或者您想完成的哪部分会给您带来麻烦?我在Fulter中遇到以下错误:重定向构造函数的返回类型“Test”不正确可签名到“Test”。类“Object”没有名为“”的构造函数。抱歉,我忘了添加扩展测试-fixedThanks,它现在可以工作了!所以如果我想将类测试拆分为多个文件,只需创建多个从测试扩展的类,对吗?这取决于您想要拆分它的原因。上面的模式是(如上所述)用于代码生成,其中一个文件是手工编写的,另一个文件是自动生成的。对于纯手工编写的类来说,这应该是不必要的。如果类太大,您可能需要专门化您的类。哇,这看起来是使用部分文件的一种很酷的方式(如C#visual Studio)。你知道有什么信息支持或反对这种做法吗?@adrianvintu iOS开发者在swift上使用这种方式。编译后,这与一个大类相同。