Sobes.tech
Junior — Middle

Millised vahendid ja mehhanismid Dart toetavad objektorienteeritud programmeerimise põhimõtteid?

sobes.tech AI

Vastus AI-lt

Dart supports the core principles of object-oriented programming (OOP) through the following mechanisms:

  • Classes and objects: defining classes with fields and methods, creating instances.
  • Inheritance: classes can inherit from other classes, extending or overriding functionality.
  • Interfaces: in Dart, any class can act as an interface, and a class can implement multiple interfaces.
  • Abstract classes: for defining base classes with abstract methods.
  • Encapsulation: private members of a class are indicated with an underscore (_), which limits access outside the file.

Example:

abstract class Animal {
  void makeSound();
}

class Dog implements Animal {
  @override
  void makeSound() {
    print('Woof-woof');
  }
}

void main() {
  var dog = Dog();
  dog.makeSound(); // Prints: Woof-woof
}