Skip to content

Commit

Permalink
Add: Class code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
rosera committed Jan 17, 2022
1 parent 339931d commit 61ec0e0
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 9 deletions.
176 changes: 171 additions & 5 deletions dart/lab05/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,183 @@ Classes allow member access to specific code definitions.

Return to [Dart Essentials](https://github.com/rosera/flutter_workshop/tree/main/dart)

# Classes
# Create a Class

TBC
Dart uses Classes to represent an object.

#### Use Case:

* TBC
* You want to create a Class

#### Example:
#### [Example](https://github.com/rosera/flutter_workshop/blob/main/dart/lab05/solutions/hello-class.dart):
```dart
TBC
const numDays = 7;
class DaysLeftInWeek {
int currentDay = DateTime.now().weekday.toInt();
DaysLeftInWeek();
int howManyDaysLeft(){
return numDays - currentDay;
}
}
void main() {
var currentDay = DaysLeftInWeek();
print ('Today is day ${currentDay.currentDay}');
print ('We have ${currentDay.howManyDaysLeft()} day(s) left in the week');
}
```

# Use a Class constructor

Dart uses Classes to represent an object.

#### Use Case:

* You want to create a Class constructor

#### [Example](https://github.com/rosera/flutter_workshop/blob/main/dart/lab05/solutions/constructor-class.dart):
```dart
const numDays = 7;
class DaysLeftInWeek {
int currentDay = 0;
DaysLeftInWeek(){
currentDay = DateTime.now().weekday.toInt();
}
int howManyDaysLeft(){
return numDays - currentDay;
}
}
void main() {
var currentDay = DaysLeftInWeek();
print ('Today is day ${currentDay.currentDay}');
print ('We have ${currentDay.howManyDaysLeft()} day(s) left in the week');
}
```


# Extending a Class

Dart uses Classes to represent an object.

#### Use Case:

* You want to extend a Class

#### [Example](https://github.com/rosera/flutter_workshop/blob/main/dart/lab05/solutions/extend-class.dart):
```dart
class Media {
String title = "";
String type = "";
Media(){ type = "Class"; }
void setMediaTitle(String mediaTitle){ title = mediaTitle; }
String getMediaTitle(){ return title; }
String getMediaType(){ return type; }
}
class Book extends Media {
String author = "";
String isbn = "";
Book(){ type = "Subclass"; }
void setBookTitle(String bookTitle){ title = bookTitle; }
void setBookAuthor(String bookAuthor){ author = bookAuthor; }
void setBookISBN(String bookISBN){ isbn = bookISBN; }
String getBookTitle(){ return title; }
String getBookAuthor(){ return author; }
String getBookISBN(){ return isbn; }
}
void main() {
var myMedia = Media();
myMedia.setMediaTitle('Tron');
print ('Title: ${myMedia.getMediaTitle()}');
print ('Type: ${myMedia.getMediaType()}');
var myBook = Book();
myBook.setBookTitle("Jungle Book");
myBook.setBookAuthor("R Kipling");
print ('Title: ${myBook.getMediaTitle()}');
print ('Author: ${myBook.getBookAuthor()}');
print ('Type: ${myBook.getMediaType()}');
}
```


# Extending a Class with Mixins

Dart uses Classes to represent an object.

#### Use Case:

* You want to extend a Class with Mixins

#### [Example](https://github.com/rosera/flutter_workshop/blob/main/dart/lab05/solutions/mixins-class.dart):
```dart
abstract class SnickersOriginal {
bool hasHazelnut = true;
bool hasRice = false;
bool hasAlmond = false;
}
abstract class SnickersCrisp {
bool hasHazelnut = true;
bool hasRice = true;
bool hasAlmond = false;
}
class ChocolateBar {
bool hasChocolate = true;
}
class CandyBar extends ChocolateBar with SnickersOriginal {
List<String> ingredients = [];
CandyBar(){
if (hasChocolate){
ingredients.add('Chocolate');
}
if (hasHazelnut){
ingredients.add('Hazelnut');
}
if (hasRice){
ingredients.add('Hazelnut');
}
if (hasAlmond){
ingredients.add('Almonds');
}
}
List<String> getIngredients(){
return ingredients;
}
}
void main() {
var snickersOriginal = CandyBar();
print ('Ingredients:');
snickersOriginal.getIngredients().forEach((ingredient) => print(ingredient));
}
```

Return to [Dart Essentials](https://github.com/rosera/flutter_workshop/tree/main/dart)
21 changes: 21 additions & 0 deletions dart/lab05/solutions/constuctor-class.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

const numDays = 7;

class DaysLeftInWeek {
int currentDay = 0;

DaysLeftInWeek(){
currentDay = DateTime.now().weekday.toInt();
}

int howManyDaysLeft(){
return numDays - currentDay;
}
}

void main() {
var currentDay = DaysLeftInWeek();

print ('Today is day ${currentDay.currentDay}');
print ('We have ${currentDay.howManyDaysLeft()} day(s) left in the week');
}
47 changes: 47 additions & 0 deletions dart/lab05/solutions/extend-class.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Media {
String title = "";
String type = "";

Media(){ type = "Class"; }

void setMediaTitle(String mediaTitle){ title = mediaTitle; }

String getMediaTitle(){ return title; }

String getMediaType(){ return type; }
}

class Book extends Media {
String author = "";
String isbn = "";

Book(){ type = "Subclass"; }

void setBookTitle(String bookTitle){ title = bookTitle; }

void setBookAuthor(String bookAuthor){ author = bookAuthor; }

void setBookISBN(String bookISBN){ isbn = bookISBN; }

String getBookTitle(){ return title; }

String getBookAuthor(){ return author; }

String getBookISBN(){ return isbn; }
}

void main() {
var myMedia = Media();

myMedia.setMediaTitle('Tron');
print ('Title: ${myMedia.getMediaTitle()}');
print ('Type: ${myMedia.getMediaType()}');


var myBook = Book();
myBook.setBookTitle("Jungle Book");
myBook.setBookAuthor("R Kipling");
print ('Title: ${myBook.getMediaTitle()}');
print ('Author: ${myBook.getBookAuthor()}');
print ('Type: ${myBook.getMediaType()}');
}
15 changes: 11 additions & 4 deletions dart/lab05/solutions/hello-class.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
class FlutterDev {
FlutterDev() {
print('I am a Flutter Developer');
const numDays = 7;

class DaysLeftInWeek {
int currentDay = DateTime.now().weekday.toInt();

int howManyDaysLeft(){
return numDays - currentDay;
}
}

void main() {
var typeOfDeveloper = new FlutterDev();
var currentDay = DaysLeftInWeek();

print ('Today is day ${currentDay.currentDay}');
print ('We have ${currentDay.howManyDaysLeft()} day(s) left in the week');
}
44 changes: 44 additions & 0 deletions dart/lab05/solutions/mixins-class.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
abstract class SnickersOriginal {
bool hasHazelnut = true;
bool hasRice = false;
bool hasAlmond = false;
}

abstract class SnickersCrisp {
bool hasHazelnut = true;
bool hasRice = true;
bool hasAlmond = false;
}

class ChocolateBar {
bool hasChocolate = true;
}

class CandyBar extends ChocolateBar with SnickersOriginal {
List<String> ingredients = [];

CandyBar(){
if (hasChocolate){
ingredients.add('Chocolate');
}
if (hasHazelnut){
ingredients.add('Hazelnut');
}
if (hasRice){
ingredients.add('Hazelnut');
}
if (hasAlmond){
ingredients.add('Almonds');
}
}

List<String> getIngredients(){
return ingredients;
}
}

void main() {
var snickersOriginal = CandyBar();
print ('Ingredients:');
snickersOriginal.getIngredients().forEach((ingredient) => print(ingredient));
}

0 comments on commit 61ec0e0

Please sign in to comment.