To create a class in Dart, you can use the class
keyword followed by the name of the class. Here's an example of a basic class:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Person { // Properties String name; int age; // Constructor Person(this.name, this.age); // Methods void speak() { print("Hello, my name is $name and I am $age years old."); } } |
Let's break down the components of this class:
- The Person class is declared using the class keyword followed by the class name.
- Inside the class, you can declare properties. In this example, we have name and age.
- The Person class also has a constructor. Constructors are used to create instances of the class. In this case, it takes name and age as parameters and assigns them to the corresponding properties.
- We've defined a method called speak() which prints a message using the print() function. It uses the properties name and age to display the information.
To use this class, you can create an instance of it like this:
1 2 3 4 |
void main() { Person person = Person("John", 25); person.speak(); } |
Running this code will create a Person
object with the name "John" and age 25 and call the speak()
method, resulting in the output: "Hello, my name is John and I am 25 years old."
What is a class in Dart?
In Dart, a class is a blueprint or a template for creating objects. It contains data members (variables or fields) and functions (methods) that operate on those data members. Classes in Dart are used to define the structure and behavior of objects. Objects are instances of classes, which means they are created based on the defined class and can have their own unique values for the data members. Classes also support inheritance, allowing one class to inherit the properties and methods of another class.
How to define a static property in a Dart class?
To define a static property in a Dart class, you can use the static
keyword. Here's an example:
1 2 3 |
class MyClass { static int myStaticProperty = 10; } |
In this example, myStaticProperty
is a static property in the MyClass
class. It can be accessed using the class name directly, without needing to create an instance of the class:
1
|
print(MyClass.myStaticProperty); // Output: 10
|
Static properties are shared among all instances of the class and can be accessed and modified by any instance or static method of the class.
How to achieve polymorphism in Dart classes?
To achieve polymorphism in Dart classes, you can follow these steps:
- Create a base class: Start by creating a base class that defines a common interface or behavior for your objects. This base class acts as a blueprint for derived classes.
1 2 3 4 5 |
class Shape { void draw() { print('Drawing a shape...'); } } |
- Create derived classes: Create derived classes that inherit from the base class and provide their own implementation of the common interface. Each derived class can override methods and add new methods or properties as needed.
1 2 3 4 5 6 7 8 9 10 11 |
class Circle extends Shape { void draw() { print('Drawing a circle...'); } } class Square extends Shape { void draw() { print('Drawing a square...'); } } |
- Use polymorphism: Use the base class type to create objects of derived classes, and then call the common method on these objects. The method implementation to execute will depend on the actual type of the object.
1 2 3 4 5 6 7 |
void main() { Shape circle = Circle(); circle.draw(); // Output: Drawing a circle... Shape square = Square(); square.draw(); // Output: Drawing a square... } |
In this example, even though the objects circle
and square
are declared with the type Shape
, the actual implementation of the draw()
method executed depends on the actual type of the object (i.e., Circle
or Square
). This behavior is known as polymorphism.