Practice Questions on Advance dart
- Create a generic class
Pair
that holds two values of any type. Implement a method to swap the values. - Write a mixin
Logger
that provides a methodlog(String message)
. Create a classService
that uses this mixin and demonstrates logging messages. - Implement an extension on the
List
class that adds a methodsum()
to return the sum of all numeric elements in the list. - Create a class
User
with a@deprecated
annotation on a methodgetFullName()
. Add a new methodgetName()
and demonstrate how to use both methods. - Define a generic function
printList<T>
that takes a list of any type and prints each element. Show how to use this function with different data types. - Create a mixin
Flyer
with a methodfly()
. Implement a classBird
that uses this mixin and demonstrate its functionality. - Implement a simple annotation called
@Range
that takes two integers,min
andmax
. Use this annotation on a field in a class and show how to access it using reflection. - Define a class
Container<T>
that has a methodaddItem(T item)
. Show how to use this class with different types of items. - Create a class
Animal
with a methodspeak()
. Use a mixinWalker
that adds a methodwalk()
. Implement a classDog
that combines both and demonstrates both methods. - Write a program that uses an extension on the
String
class to add a methodisNumeric()
that checks if the string represents a number.
Answers and Explanations
Generic Class
Pair
class Pair<T, U> { T first; U second; Pair(this.first, this.second); void swap() { var temp = first; first = second; second = temp; } }
This class holds two values of any types
T
andU
, and provides a method to swap them.Mixin
Logger
mixin Logger { void log(String message) { print('Log: $message'); } } class Service with Logger { void performService() { log('Service is performed.'); } }
The
Logger
mixin provides a logging functionality that can be used in any class, such asService
.Extension on List
extension ListSum on List<num> { num sum() { return reduce((value, element) => value + element); } }
This extension adds a
sum()
method to lists containing numeric elements, allowing you to easily calculate their sum.Class
User
with@deprecated
class User { @deprecated String getFullName() { return 'John Doe'; } String getName() { return 'John'; } }
The
getFullName()
method is marked as deprecated, prompting users to usegetName()
instead.Generic Function
printList<T>
void printList<T>(List<T> list) { for (var item in list) { print(item); } }
This function takes a list of any type and prints each element, demonstrating the use of generics.
Mixin
Flyer
mixin Flyer { void fly() { print('Flying high!'); } } class Bird with Flyer {}
The
Flyer
mixin adds flying functionality to theBird
class, which can now fly.Annotation
@Range
class Range { final int min; final int max; const Range(this.min, this.max); } class Product { @Range(1, 100) int quantity; }
This annotation marks the
quantity
field, and you can access it using reflection to validate the range.Class
Container<T>
class Container<T> { List<T> items = []; void addItem(T item) { items.add(item); } }
The
Container
class can hold items of any type specified when instantiated, demonstrating the use of generics.Class
Animal
with MixinWalker
class Animal { void speak() { print('Animal sound'); } } mixin Walker { void walk() { print('Walking'); } } class Dog extends Animal with Walker { @override void speak() { print('Woof'); } }
The
Dog
class inherits fromAnimal
and uses theWalker
mixin to provide both walking and speaking functionalities.Extension on String for
isNumeric()
extension StringExtensions on String { bool isNumeric() { return double.tryParse(this) != null; } }
This extension method allows any string to check if it represents a numeric value, enhancing the string class functionality.
PLAY QUIZ