-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects.dart
More file actions
84 lines (68 loc) · 1.73 KB
/
objects.dart
File metadata and controls
84 lines (68 loc) · 1.73 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import 'dart:io';
//1. A class that implements an interface
//define an interface
abstract class Animal{
String makeSound();
}
//define a class that implements the animal interface
class Dog implements Animal{
@override
makeSound(){
return 'woof';
}
}
//2. A class that overrides an inherited method
class Vehicle{
String brand;
int year;
//Constructor
Vehicle(this.brand, this.year);
void display(){
print('Brand: $brand, Year: $year');
}
}
//create a subclass of Vehicle and inherit from it.
//A class that overrides an inherited method
class Car extends Vehicle{
int numofDoors;
//constructor
Car(String brand, int year, this.numofDoors): super(brand, year);
//display function
@override
display(){
super.display();
print('Number of doors: $numofDoors');
}
}
//create another subclass of vehicle and inherit from Vehicle.
//A class that overrides an inherited method
class Motorcycle extends Vehicle{
String type;
//constructor
Motorcycle(String brand, int year, this.type): super(brand, year);
//display function
@override
display(){
super.display();
print('type: $type');
}
}
// Method that demonstrates the use of a loop
void printMultipleTimes(String text, int times) {
for (var i = 0; i < times; i++) {
print(text);
}
}
void main(){
//create an instance of the objects
//An instance of a class that is initialized with data.
Animal animal = Dog();
print(animal.makeSound());
Car car = Car('Toyota Camry', 2022, 4);
car.display();
Motorcycle motorcycle = Motorcycle('Honda', 2019, 'rollercoaster');
motorcycle.display();
// Demonstrating a method that uses a loop
print('\nPrinting "Hello, world!" 3 times:');
printMultipleTimes("Hello, world!", 3);
}