dart 变量

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/coderinchina/article/details/91412952

目前市面上关于flutter的资源还是不多的,其实很多资料都是从官网上学习的,个人认为官网是很好的学习资料,也是你个人学习能力的一个认可,我们学习Java都是有一个main函数 这是入口 那dart语言呢?

https://dart.dev/samples

上面是dart的官网例子 我们只要对看他就行

Hello World
Every app has a main() function. To display text on the console, you can use the top-level print() function:

void main() {
  print('Hello, World!');
}

这是官网给我们的解释  英文也不难看懂,对着Java发现他省略了很多修饰符比如public  函数print()不是使用类或者对象去调用

我们这博客是讲变量,那么看看官网给我的解释

Variables
Even in type-safe Dart code, most variables don’t need explicit types, thanks to type inference

翻译:即使在类型安全的Dart代码中,由于类型推断,大多数变量也不需要显式类型

var name = 'Voyager I';
var year = 1977;
var antennaDiameter = 3.7;
var flybyObjects = ['Jupiter', 'Saturn', 'Uranus', 'Neptune'];
var image = {
  'tags': ['saturn'],
  'url': '//path/to/saturn.jpg'
};

上面是官网给的例子

如果有一点JavaScript的基础,发现很像,在js中也是使用var去定义一个变量

var x;
void main() {
  print(x);
}

这个打印出来的是null,这个和Java不同 Java是根据不同的数据类型 会默认有一个初始化的值 而dart 是动态语言,也就是说一个变量是什么类型是根据给它的赋值去判断其类型的,而一旦给变量赋值了,如果再赋值给其他类型编译器就会报错

void main() {
  var x  ;
  x = 'zhouguizhi';
  x = 10;
  print(x);
}

这是可以的,因为变量x并没有开始赋值,它可以是int 也可以是字符串

一旦赋值后再给它赋值不同类型就会报错

对比我们Java中还有常量,是怎么表示的呢?

Read more about variables in Dart, including default values, the final and const keywords, and static types

我们点击Read More会进入到下一个界面,

https://dart.dev/guides/language/language-tour#variables

这里解释的很清楚

The type of the name variable is inferred to be String, but you can change that type by specifying it. If an object isn’t restricted to a single type, specify the Object or dynamic type, following design guidelines

这是官网的说明 翻译:

name变量的类型被推断为String,但是您可以通过指定它来更改该类型。如果对象不局限于单一类型,请按照设计指南指定对象或动态类型

void main() {
  dynamic x = 'Bob';
  x = 10;
  print(x);
}

使用dynamic 定义的变量是可以随时修改的,这和var定义的变量不同 var变量只要赋值后 类型就定了 不能修改了,而dynamic定义的变量是可以修改的,这个我们最好别用,代码阅读起来很不方便,在dart中也提供了很多种数据类型 比如String

Another option is to explicitly declare the type that would be inferred:

String name = 'Bob';

这是说我们可以显式声明将要推断的类型

在dart中声明一个变量是使用final或者const

Final and const
If you never intend to change a variable, use final or const, either instead of var or in addition to a type. A final variable can be set only once; a const variable is a compile-time constant. (Const variables are implicitly final.) A final top-level or class variable is initialized the first time it’s used

翻译:

如果您从未打算更改一个变量,请使用final或const代替var或类型。最后一个变量只能设置一次;const变量是编译时常量。(Const变量隐式为final。)最后一个顶层变量或类变量在第一次使用时初始化

例子:

final name = 'Bob'; // Without a type annotation
final String nickname = 'Bobby';

使用final修饰的变量 定义不能使用var 而要使用dart给的数据类型

Use const for variables that you want to be compile-time constants. If the const variable is at the class level, mark it static const. Where you declare the variable, set the value to a compile-time constant such as a number or string literal, a const variable, or the result of an arithmetic operation on constant numbers:

翻译:

对于希望成为编译时常量的变量,请使用const。如果const变量位于类级别,则将其标记为静态const。在声明变量时,将值设置为编译时常量,如数字或字符串文字、const变量或对常量进行算术运算的结果:

例子:

const bar = 1000000; // Unit of pressure (dynes/cm2)
const double atm = 1.01325 * bar; // Standard atmosphere

说明:

The const keyword isn’t just for declaring constant variables. You can also use it to create constant values, as well as to declare constructors that create constant values. Any variable can have a constant value​

​

翻译:

const关键字不仅仅用于声明常量变量。您还可以使用它来创建常量值,以及声明创建常量值的构造函数。任何变量都可以有一个常量

例子:

var foo = const [];
final bar = const [];
const baz = []; // Equivalent to `const []`

最后讲下final和constant的区别:

Instance variables can be final but not const. Final instance variables must be initialized before the constructor body starts — at the variable declaration, by a constructor parameter, or in the constructor’s initializer list

上面是官网给我们的解释:

翻译:

实例变量可以是final,但不能是const。最终实例变量必须在构造函数主体开始之前初始化——在变量声明处,由构造函数参数初始化,或者在构造函数的初始化器列表中初始化

这个要到学习对象时候才能用到 在这记录下

猜你喜欢

转载自blog.csdn.net/coderinchina/article/details/91412952