Home  >  Article  >  Web Front-end  >  Google Dart programming syntax and basic types learning tutorial_Basic knowledge

Google Dart programming syntax and basic types learning tutorial_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:12:211098browse

1. Variable declaration

How to define variables

Copy code The code is as follows:

var name = 'Bob';

Initial value of variable

Copy code The code is as follows:

int lineCount;
assert(lineCount == null ); // Variables (even numbers) are initially null.

You can use var or specify the type directly.
final, a variable defined as final, the value cannot be changed

Copy code The code is as follows:

final name = 'Bob'; // Or: final String name = 'Bob';
name = 'Alice'; // ERROR

2. Basic type

String

Strings can use single quotes or double quotes.

Copy code The code is as follows:

var s1 = 'Single quotes work well for string literals. ';
var s2 = "Double quotes work just as well.";


In a string, you can apply the value directly, ${expression}, if it's just a variable , you can remove {}
Copy code The code is as follows:

var s = 'string interpolation ';
assert('Dart has $s, which is very handy.' ==
'Dart has string interpolation, which is very handy.');
assert('That deserves all caps. $ {s.toUpperCase()} is very handy!' ==
'That deserves all caps. STRING INTERPOLATION is very handy!');


Multi-line string, will Considered the default splice.
Copy code The code is as follows:

var s = 'String ''concatenation'
" works even over line breaks.";
assert(s == 'String concatenation works even over line breaks.');

If you want to use a multi-line string, you can do this, use '''

to copy the code The code is as follows:

var s1 = '''
You can create
multi-line strings like this one.
''';


Create one Do not consider escaped strings
Copy code The code is as follows:

var s = @" In a raw string, even n isn't special.";

StringBuffer, very similar to that in .net.

Copy code The code is as follows:

var sb = new StringBuffer();

sb.add("Use a StringBuffer ");
sb.addAll(["for ", "efficient ", "string ", "creation "]);
sb.add("if you are ").add("building lots of strings.");

var fullString = sb.toString();

Numbers

There are mainly two types, int and double, they both inherit the num type

Conversion between numbers and strings

Copy code The code is as follows:

// String -> int
var one = Math.parseInt("1");
assert(one == 1);

// String -> double
var onePointOne = Math.parseDouble("1.1");
assert(onePointOne == 1.1);

// int -> String
var oneAsString = 1.toString();
assert(oneAsString == "1");

// double -> String
var piAsString = 3.14159.toStringAsFixed(2);
assert(piAsString == "3.14");



Boolean type

bool, different from js, as long as it is not true, it is false.

Lists (can be used as an array)

Copy code The code is as follows:

var list = [1,2,3]; / /Instantiate a list
list.add(4); //Add an element 4

You can use for, for...in, foreach() to traverse a list.

Copy code The code is as follows:

var list = [1,2,3];
for (final x in list) {
print(x);
}


or

Copy code The code is as follows:

var list = [1,2,3];
list.forEach((element) => print(element));

Maps (dictionary type)

Copy code The code is as follows:

var gifts = {                                                                                              // Keys Values
"first" : "partridge",
"second" : "turtledoves",
"fifth" : "golden rings"};
gifts["third"] = "apple"; //Add a


Use foreach to traverse


Copy code The code is as follows:
var gifts = {
"first" : "partridge",
"second": "turtledoves",
"fifth" : "golden rings"};
gifts.forEach((k,v) => print( '$k : $v'));

getKeys() and getValues() methods


Copy code The code is as follows:
var gifts = {"first": "partridge", "second": "turtledoves"};
var values ​​= gifts.getValues();
//Print partridge and turtledoves, but not necessarily in that order.

values.forEach((v) => print(v));

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn