Created
May 5, 2022 07:31
-
-
Save wesleybliss/55aa32ee30f72fc3ab3aeba112ec70f9 to your computer and use it in GitHub Desktop.
Learn Dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Slashy things are single-line comments, very useful | |
/* | |
* Slashy + star are multi-line comments | |
* | |
* This is a basic inheritence example, | |
* which is a core principle of OOP, or Object Oriented Programming | |
*/ | |
// Imports let you use things from other libraries (code) | |
// that you or others have written | |
// This math library comes with Dart | |
// Most languages have an STD (lol), or a Standard Library | |
import 'dart:math'; | |
// This is a list, which is the newer/cooler way of saying an Array | |
// This syntax uses "generics", which is a very difficult subject, but very powerful | |
// In this case, the List class is generic, so we need to tell it what type of list it is | |
// Here we're creating a list of Strings (text) | |
// The [square bracket] notation is used for lists/arrays, and Strings need to be quoted | |
List<String> meats = [ | |
'beef', | |
'pork', | |
'chicken', | |
]; | |
List<String> veggies = [ | |
'spinach', | |
'broccoli', | |
'carrot', | |
'lettuce', | |
'tomato', | |
]; | |
// Ignore this - just adding some more empty space to the output, easier to read | |
void log(String message) { | |
print('\n$message\n'); | |
} | |
// Functions can have a return type, i.e. they respond with data, or void which mean nothing | |
// Functions can take in arguments too | |
// In this case, we'll pick a random food from a given foodList | |
String getRandomFood(List<String> foodList) { | |
// First create a new instance of Dart's Random class | |
// It's good practice to declare things final if you know they won't change | |
final Random random = Random(); | |
// We define the maximum number we can use, which is the size of our | |
// food list + 1, since arrays start at zero | |
// (so given 10 items, .length would be 10, but we can only access 0-9) | |
final max = foodList.length - 1; | |
// Now grab the next available random integer (whole number) | |
// up to our maximum | |
// This will return a number between 0 and max | |
// This is known as the array's "index" | |
int randomIndex = random.nextInt(max); | |
// Now we send back the random food | |
return foodList[randomIndex]; | |
} | |
// All animals have some basic properties | |
// And can do some basic actions (functions) | |
class Animal { | |
// These are some properties of Animal | |
// They're final bc they can't change | |
final String name; | |
final String type; | |
final int arms; | |
final int legs; | |
// This is a constructor. It's the main function that gets called | |
// when you create a new instance of a class | |
Animal(this.name, this.type, this.arms, this.legs) { | |
log('HUZZAHHH, $name THE $type WAS BORN'); | |
} | |
void eat(String food) { | |
// When we write strings, we can use substitution by prefixing any variables with $ | |
log('$name ate some $food using his $arms arms, num num num num num num num'); | |
// can also write print('hello ' + 'world'); but that's more verbose | |
} | |
void sleep(int hours) { | |
log('$name is sleaping for the next $hours hours'); | |
} | |
void shit() { | |
final Random random = Random(); | |
final shitSize = random.nextInt(5) + 1; | |
log('$name took a shit that was a whopping $shitSize kg! His $legs legs almost fell off'); | |
} | |
void fuck() { | |
log('$name just got jiggy, allll right giggity'); | |
// Random chance he knocked her up | |
final Random random = Random(); | |
final oneOrZero = random.nextInt(1); | |
if (oneOrZero > 0) { | |
log('Oh noo, $name just got his hoe preggers'); | |
} | |
} | |
} | |
// Now let's create a more specific being by inheriting from Animal | |
// Dog inherits all of Animal's properties & functions | |
class Dog extends Animal { | |
// Dog constructor also needs a name, | |
// but we already know how many arms/legs it has | |
// By calling super() we call the parent (Animal) class's constructor | |
Dog(String name) : super(name, 'dog', 0, 4); | |
// Here we'll override the eat function & change the message | |
@override | |
void eat(String food) { | |
log('$name ate some $food, num num woof slobber'); | |
} | |
void bark() { | |
log('WOOF WOOF WOOF'); | |
} | |
void barkInChinese() { | |
log('WAN WAN WAN'); | |
} | |
} | |
// We can keep inheriting indefinitely | |
class GermanShephard extends Dog { | |
GermanShephard(String name) : super(name); | |
void attack() { | |
log('Grrrrr arrrrr crunch $name attacked someone'); | |
} | |
} | |
// Most apps have some kind of main/primary entrypoint | |
// This function never returns anything (void), but the computer knows to run it first | |
void main() { | |
// Let's create a dag | |
final shep = GermanShephard('Adolf Barkendeutsch'); | |
shep.eat(getRandomFood(meats)); | |
shep.sleep(1); | |
shep.bark(); | |
shep.shit(); | |
shep.barkInChinese(); | |
shep.attack(); | |
// Let's make shep eat 2 foods | |
final meat = getRandomFood(meats); | |
final vegg = getRandomFood(veggies); | |
// ...and combine them into 1 string | |
final shepFood = '$meat and $vegg'; | |
// ...and feed him | |
shep.eat(shepFood); | |
log('-----------------------------'); | |
// Let's create a more generic animal | |
final spider = Animal('Charlotte', 'spider', 8, 8); | |
spider.eat('butterfly'); | |
spider.sleep(12); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment