Programming in Dart: Classes

Jun 28 2022 · Dart 2.17, Flutter 3.0, DartPad

Part 1: Understand Classes

03. Challenge: Create a Custom Class

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 02. Create a Class Next episode: 04. Write a Constructor

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Now that you have an idea on how to create a class, it’s time to put your new skills to the test. I want your to define an RPG Character class. The class name should be RPGCharacter. It should have a character name as string. Finally, create three instance variables for strength, dexterity and constitution. These should be of type int.

class RPGCharacter {

}
String name = '';
int strength = 0;
int dexterity = 0; 
int constitution = 0;
import 'dart:math';
void rollStats() {
    strength = Random().nextInt(16) + 3;
    dexterity = Random().nextInt(16) + 3;
    constitution = Random().nextInt(16) + 3;
}
void printStats() {
    print('$name has $strength strength, $dexterity dexterity, and $constitution constitution');
}
var fizBoz = RPGCharacter();
fizBoz.name = 'FizBoz';
fizBoz.rollStats();
fizBoz.printStats();
fizBoz.printStats();
fizBoz.rollStats();
int strength = Random().nextInt(16) + 3;
int dexterity = Random().nextInt(16) + 3; 
int constitution = Random().nextInt(16) + 3;
void main() {
  var fizBoz = RPGCharacter();
  fizBoz.name = 'FizBoz';
  fizBoz.printStats();
}