简体   繁体   English

Dart 中“toString”和“as String”的区别?

[英]Difference between "toString" and "as String" in Dart?

Is there any difference between .toString and as String in Dart? Dart中的.toStringas String有区别吗?

toString() is a method on Object and is therefore available on every object. The method is used to get a string representation of the object: toString()Object上的一个方法,因此在每个 object 上都可用。该方法用于获取 object 的字符串表示形式:

A string representation of this object.此 object 的字符串表示形式。

Some classes have a default textual representation, often paired with a static parse function (like int.parse).某些类具有默认的文本表示,通常与 static 解析 function(如 int.parse)配对。 These classes will provide the textual representation as their string represetion.这些类将提供文本表示作为它们的字符串表示。

Other classes have no meaningful textual representation that a program will care about.其他类没有程序会关心的有意义的文本表示。 Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.这些类通常会覆盖 toString 以在检查 object 时提供有用的信息,主要用于调试或日志记录。

https://api.dart.dev/stable/2.13.4/dart-core/Object/toString.html https://api.dart.dev/stable/2.13.4/dart-core/Object/toString.html

as String is a typecast in Dart and is used to tell the analyzer/compiler that whatever it assumes, you are now going to tell it that your object is in fact a String at runtime. as String是 Dart 中的类型转换,用于告诉分析器/编译器无论它假定什么,您现在要告诉它您的 object 实际上在运行时是一个String You can hereafter use the object like a String .您以后可以像使用String一样使用 object 。

But the compiler will add a check at runtime and if the object is not compatible with the interface of String , your application will crash because you have lied to the compiler.但是编译器会在运行时添加检查,如果 object 与String的接口不兼容,您的应用程序将崩溃,因为您欺骗了编译器。

It is therefore two entire different things and is used for different purposes.因此,它是两个完全不同的东西,用于不同的目的。 You can eg not use as String on an object which is not already a String .例如,您可以不在还不是 String 的 object 上使用as String String

The safest you can do is just call toString() since toString() on String will just return itself.最安全的做法就是调用toString() ,因为String上的toString()只会返回自身。

They are completely different!他们是完全不同的!

.toString() is a method to represent data of a object but as String is a type cast which tries to convert the object itself to a String . .toString()是一种表示 object 数据的方法,但as String是一种类型转换,它试图将 object 本身转换为String

Imagine you have a class named Person假设你有一个名为 class 的Person

class Person {
  String firstName;
  String lastName;
  Person(
     this.firstName,
     this.lastName,
  );
}

now casting person to a String will lead to an _CastError error since Person is not a String or a subtype of String(inherited classes from String class)现在将 person 转换为 String 将导致_CastError错误,因为 Person 不是 String 或 String 的子类型(继承自 String 类的类)

final person = Person('sajad', 'abd');
final personAsString = person as String;

Meanwhile, the method .toString() will represent you object in a String.同时,方法.toString()将在字符串中代表您 object。

final person = Person('sajad', 'abd');
final personToString = person.toString();
print(personToString); // result: Instance of 'Person'

.toString() is defined for every class in dart and you can override it in you custom classes .toString()是为 dart 中的每个 class 定义的,您可以在自定义类中覆盖它

for example you can override it in Person class to represent firstname and lastname of person例如,您可以在Person class 中覆盖它以表示人的名字和姓氏

class Person {
  String firstName;
  String lastName;
  Person(
     this.firstName,
     this.lastName,
  );

  @override
  String toString() => 'Person(firstName: $firstName, lastName: $lastName)';
}

And now the result of现在的结果

final person = Person('sajad', 'abd');
final personToString = person.toString();
print(personToString);

would be将会

Person(firstName: sajad, lastName: abd)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM