7. Dart - Classes (Like Structs in Golang)

Defining a class:

class User {

    String username = "todd";

    int age = 30;


    void printmyinfo() {

        print('$username is $age years old');

    }

}


Defining a variable as the class:

User person = User();


Calling class property:

print(person.username);

or

print('${person.username}')


Calling class method:

person.printmyinfo();


Changing property of class:

person.username = "mary";


Defining a class with a constructor (which edits property of the class when initiating with a value):

class User {

    String username;

    int age;


    void printmyinfo() {

        print('$username is $age years old');

    }


    User(this.username, this.age);

    // this is a constructor for the variables inside this class

}


Defining a variable as the class with constructor parameters:

User person = User("amy",50);

Comments

Popular posts from this blog

2. FreeCodeCamp - Dynamic Programming - Learn to Solve Algorithmic Problems & Coding Challenges

20. Data Analytics - Analyze Data to Answer Questions - Week 1

3. Algorithms - Selection Sort