-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.dart
59 lines (49 loc) · 1.13 KB
/
classes.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Classes in Dart
* - Classes are used to create objects
* - Classes are defined using the keyword class
* - Classes can have state (attributes) and behavior (methods)
*/
void main() {
// create an object
Person person = new Person(); // new is optional
// access the attributes
person.name = "Muzakkir";
person.age = 24;
person.height = 5.11;
person.isMarried = true;
// access the behavior
person.printInfo();
// create another object
Book book = Book("The Great Gatsby", "F. Scott Fitzgerald", 218);
book.printInfo();
}
class Person {
String name;
int age;
double height;
bool isMarried;
Person(
{this.name = "Unknown",
this.age = 0,
this.height = 0.0,
this.isMarried = false});
void printInfo() {
print("Name: $name");
print("Age: $age");
print("Height: $height");
print("Is Married: $isMarried");
}
}
class Book {
String title;
String author;
int numPages;
// more readable
Book(this.title, this.author, this.numPages);
void printInfo() {
print("Title: $title");
print("Author: $author");
print("Number of Pages: $numPages");
}
}