Junior — Middle
Czy możesz wyjaśnić, co rozumiemy przez interfejs w programowaniu obiektowym?
sobes.tech AI
Odpowiedź od AI
W programowaniu obiektowym interfejs jest abstraktywnym typem, który definiuje zestaw metod, które klasa musi zaimplementować, ale nie zawiera ich konkretnej implementacji. Interfejs ustala kontrakt, który gwarantuje, że obiekty implementujące ten interfejs będą miały określony zestaw funkcji.
W Dart (język Flutter) interfejsy są implementowane przez klasy abstrakcyjne lub po prostu przez klasy, które inne klasy mogą implementować za pomocą słowa kluczowego implements.
Przykład:
abstract class Animal {
void makeSound();
}
class Dog implements Animal {
@override
void makeSound() {
print('Hau-hau');
}
}
Tutaj Animal jest interfejsem z metodą makeSound(), a klasa Dog implementuje ten interfejs, dostarczając konkretną implementację metody.