Introduction To Dart Classes And Objects. How New Operator Is Used For Objects.
- Posted on March 16, 2024
- Dart
- By MmantraTech
- 205 Views
In the world of programming, understanding classes and objects is fundamental, and Dart makes no exception. Dart, a modern, object-oriented programming language developed by Google, provides a robust framework for creating classes and objects, essential components for building scalable and maintainable applications.
-505zbvpkQA.png)
Video link
What are Classes and Objects?
In Dart, a class is a blueprint for creating objects. It defines the properties (attributes) and behaviors (methods) that objects of that class will have. Think of a class as a template or a cookie cutter, while objects are the actual cookies produced using that template.
For example, let's consider a class called Person
, which might have properties like name
, age
, and gender
, along with methods like greet()
or calculateAge()
. Each person in our program would be an object instantiated from the Person
class.
Creating Classes in Dart
In Dart, defining a class is straightforward. Here's a simple example:
class Person {
String name; int age; // Constructor Person(this.name, this.age);
// Method to greet
void greet() { print("Hello, my name is $name and I am $age years old."); }
}
In the above code, we've defined a Person
class with two properties: name
and age
. We've also included a constructor to initialize these properties when creating a new Person
object, and a greet()
method to display a greeting message.
Creating Objects in Dart
Once we've defined a class, we can create objects from it using the new
keyword:
void main() {
// Creating a Person object
var person1 = Person("Alice", 30);
// Calling the greet method
person1.greet();
}
In this example, we've created a Person
object named person1
with the name "Alice" and age 30, and then we've called the greet()
method on it to display a greeting message.
Everthing in one umbrella with examples
//Classes and objects --DART PART 3
// Blue print of real life
//We need a data type that can define real life objects structure
// data + Functions collection
//purpose : so that I can reuse that data type/structure as per requirement
class Calculator {
// Curly baraced : Scope of the class : Body of the class
int a = 10; //Global variables in terms of functions
void sum() {
print(a+2);
}
void divide() {
print(a/2);
}
}
void main() {
//How to create object of the class;
//Why object : to access class data we need objects
//What are objects:
//Ans : Objects are actually copy/snapshot of class
//so that we can access class operations.
//Objects are memory location in ram for each object
//New keyowrd : is used to assign memory to that object
// Calculator ob1 = new Calculator();
// Calculator ob1 = Calculator();
var ob1 = Calculator();
ob1.a=100;
ob1.sum();
ob1.divide();
/*
-Number
-- int
-- double
*/
//int and String are pre built keywords
//Contain one value at a time
int age = 56;
//print("age=${age}");
// print('age=${age}');
double percentage = 90.56;
String name = "Techmalasi";
String movie = 'Captain America';
//Contcate variables with strings
// print("The movie name=${movie}");
//Is Ankit a Topper of the class.
// Yes OR No
bool isTopper = true; //1
bool isRainny = false; //0
// print(isTopper);
//Collection in Dart
//What are you list of fav movies?
//Array : collection of something
/*
- Collection
-- List : an Array
-- Set : an Array without duplicate values
-- Map : It is just like JSON DATA - Key value pairs
*/
// Loose Type : Typed not checked
// List movies =["DDLJ",45.78,true,"Captain America","Sholay","Iron Man",30];
//Strict Type : Type checked
// List <String>movies =["DDLJ","Captain America","Sholay","Iron Man"];
// List movies =<String>["DDLJ","Captain America","Sholay","Iron Man"];
//
//
//
// print(movies);
//Duplicates allowed
// List numbers=[3,5,7,3,45,3];
// print(numbers);
// Set numbers ={3,5,7,3,8,3,45.67}; //Map/Set
// print(numbers);
// Set <int>numbers ={3,5,7,3,8,3}; //Map/Set
// print(numbers);
// Set p1 = Set();
// p1.add("5");
//
// print(p1);
//MAP
// Map student={
// "name" :"Techmalasi", //String
// "age" : 40, // int
// "percentage":90.45 // double
//
// };
//
// Map <String,dynamic>student={
// "name" :"Techmalasi", //String
// "age" : 40, // int
// "percentage":90.45 // double
//
// };
// print(student);
//variable type
var age2 = 45.67;
var name2 = "kamal";
var movies = ['a', "ddlj"];
//As per input type , Data Tpye is defined
}
Conclusion
Understanding classes and objects is essential for anyone learning Dart or any object-oriented programming language. With classes, you can create reusable and organized code, making your applications easier to maintain and extend. In Dart, creating classes and objects is simple yet powerful, allowing you to build complex applications with ease. So dive in, explore, and start building amazing things with Dart!
Write a Response