Programming in Dart: Classes

Jun 28 2022 · Dart 2.17, Flutter 3.0, DartPad

Part 1: Understand Classes

05. Challenge: Define a Constructor

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: 04. Write a Constructor Next episode: 06. Utilize Initialization Lists

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.

Transcript: 05. Challenge: Define a Constructor

With your new knowledge of constructors, I think it’s time to give you another challenge. In your first challenge, you created an RPG character class. Your going to do it again, only this, I want to create a constructor that takes in the character’s name and the three stats.

For review, the RPGCharacter class should have a character name as string. Then create three instance variables for strength, dexterity and constitution. These should be of type int.

Last but not least, create another method called printStats that will print out the character name and stats. solution. Pause the video now and try it out.

To get started, open up DartPad. We’ll start off by defining our RPGCharacter class.

class RPGCharacter {

}

Now lets define our instance variables. This time, we won’t set an values.

String name;
int strength;
int dexterity; 
int constitution;

The name and stats may change so we’ll keep them as variables. Now let’s create our constructor.

RPGCharacter(this.name, this.strength, this.dexterity, this.constitution);

We can add carriage returns to our constructor to make it legible as opposed to writing it on one line.

  RPGCharacter(this.name, 
               this.strength, 
               this.dexterity, 
               this.constitution);

Now lets add our printStats method.

void printStats() {
    print('$name has $strength strength, $dexterity dexterity, and $constitution constitution');
}

Like our constructor, we can break this statement on to multiple lines.

    print('$name has $strength strength, '
          '$dexterity dexterity, and $constitution'
          ' constitution.');

Lets now create a new character. Add the following.

 var fizBoz = RPGCharacter('Fizboz', 13, 10, 17);
 fizBoz.printStats();

Run the program. Now you get your stats. But notice, the constructor is a little difficult to understand. Which one is dexterity? Which one is the constitution? Will, we can use named parameters. Just remember, that we must provide a default value. The name will be required.

RPGCharacter(this.name, { this.strength = 0, this.dexterity = 0, this.constitution = 0 });

Now let’s update our constructor.

var fizBoz = RPGCharacter('Fizboz', strength: 13, dexterity: 10, constitution: 17);

Run the program. It behaves exactly as before only this time, the code self documenting and that’s a good thing.