-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19. object.dart
131 lines (106 loc) · 4.58 KB
/
19. object.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/// Objects in Dart
///
/// What are Objects?
///
/// In Dart, everything you interact with is an object.
/// Objects are instances of classes, and they represent real-world entities or concepts.
/// They are the building blocks of object-oriented programming (OOP).
///
/// Key Characteristics of Objects:
///
/// 1.Identity: Each object has a unique identity that distinguishes it from other objects, even if they have the same data.
/// 2.State: An object's state is defined by the values of its properties (fields).
/// 3.Behavior: An object's behavior is defined by the methods it can perform.
///
/// How Objects Relate to Classes:
///
/// * Class as a Blueprint: A class is like a blueprint or template. It defines the structure (fields) and behavior (methods) that objects of that class will have.
/// * Object as an Instance: An object is a specific instance of a class. When you create an object, you are essentially creating a concrete realization of the class's blueprint.
///
/// Creating Objects:
///
/// Objects are created using the `new` keyword (optional in Dart) followed by a call to a class's constructor.
///
/// Accessing Object Members:
///
/// You use the dot operator (`.`) to access an object's fields and methods.
///
/// Example: The `Dog` Class and Objects
///
/// Let's illustrate these concepts with a `Dog` class.
// Define the Dog class
class Dog {
// Fields (Properties)
String name;
String breed;
int age;
String? color; // Nullable field
// Constructor
Dog(this.name, this.breed, this.age, [this.color]);
// Named Constructor
Dog.puppy(this.name, this.breed, [this.color]) : age = 0;
// Methods (Behavior)
void bark() {
print('$name says Woof!');
}
void eat(String food) {
print('$name is eating $food.');
}
void describe() {
print('$name is a $age-year-old $breed.${color != null ? ' Its color is $color.' : ''}');
}
// Getter
String get dogInfo => 'Name: $name, Breed: $breed, Age: $age${color != null ? ', Color: $color' : ''}';
// Setter
set updateAge(int newAge) {
if (newAge >= 0) {
age = newAge;
} else {
print('Age cannot be negative.');
}
}
}
void main() {
// Creating Objects:
// 1. Creating a Dog object using the default constructor:
Dog dog1 = Dog('Buddy', 'Golden Retriever', 3, 'Golden');
print('Dog 1 created.');
// 2. Creating a Dog object using the named constructor:
Dog dog2 = Dog.puppy('Lucy', 'Poodle', 'White');
print('Dog 2 created.');
// Accessing Object Members:
// Accessing fields:
print('Dog 1\'s name: ${dog1.name}'); // Output: Dog 1's name: Buddy
print('Dog 2\'s breed: ${dog2.breed}'); // Output: Dog 2's breed: Poodle
// Calling methods:
dog1.bark(); // Output: Buddy says Woof!
dog2.eat('kibble'); // Output: Lucy is eating kibble.
dog1.describe(); // Output: Buddy is a 3-year-old Golden Retriever. Its color is Golden.
dog2.describe(); // Output: Lucy is a 0-year-old Poodle. Its color is White.
// Using the getter:
print(dog1.dogInfo); // Output: Name: Buddy, Breed: Golden Retriever, Age: 3, Color: Golden
print(dog2.dogInfo); // Output: Name: Lucy, Breed: Poodle, Age: 0, Color: White
// Using the setter:
dog1.updateAge = 4;
print(dog1.dogInfo); // Output: Name: Buddy, Breed: Golden Retriever, Age: 4, Color: Golden
dog2.updateAge = -2; // Output: Age cannot be negative.
print(dog2.dogInfo); // Output: Name: Lucy, Breed: Poodle, Age: 0, Color: White
// Objects are Unique:
// Even if two objects have the same data, they are still distinct objects.
Dog dog3 = Dog('Buddy', 'Golden Retriever', 4, 'Golden');
print(dog1 == dog3); // Output: false (They are different objects, even with the same data)
// Objects and Memory:
// Each object occupies a separate space in memory.
// When you create a new object, Dart allocates memory to store its data.
// Objects and Null:
// If a variable that is supposed to hold an object is not assigned an object, it will be null.
Dog? dog4;
print(dog4); // Output: null
// Objects and Dynamic Typing:
// Dart is dynamically typed, so you can assign objects of different classes to the same variable (if they share a common superclass or interface).
// However, it's generally good practice to use specific types for better code readability and maintainability.
// Objects and the `toString()` Method:
// Every object in Dart has a `toString()` method, which is used to get a string representation of the object.
// You can override this method in your classes to provide a custom string representation.
print(dog1.toString()); // Output: Instance of 'Dog' (default implementation)
}