YouTip LogoYouTip

Dart Classes And Objects

Classes are the core concept of object-oriented programming, and they are an abstract description of things in the real world. Objects are specific instances of classes. This chapter introduces class definition, constructors, member variables and methods, getter/setter, and static members in Dart. * * * ## Class Definition and Instantiation Classes are templates for creating objects, defining what properties (data) and behaviors (methods) an object has. ## Example // Define a User class class User { // Member variables (properties) String name =''; int age =0; // Member methods (behaviors) void introduce(){ print('Hello, I am $name, and I am $age years old this year.'); } bool isAdult(){ return age >=18; } } void main(){ // Instantiation: use the new keyword (optional in Dart 2.0+) var user1 = User(); user1.name='tutorial'; user1.age=10; // Access member variables and methods print('Username: ${user1.name}'); user1.introduce(); print('Is adult: ${user1.isAdult()}'); // Create another object, independent of each other var user2 = User(); user2.name='Xiao Ming'; user2.age=20; user2.introduce(); } Username: tutorial Hello, I'm tutorial, this year I'm 10 years old. Is adult: falseHello, I'm Xiao Ming, this year I'm 20 years old. > Starting from Dart 2.0, the new keyword is optional when instantiating. The community convention is to omit new to keep the code concise. All subsequent examples in this tutorial will omit new. * * * ## Constructors Constructors are special methods that are automatically called when creating an object, used to initialize the object's state. Dart provides various constructor forms, which is very flexible. ### Default Constructor If you don't define a constructor, Dart will automatically provide a parameterless default constructor. ### Regular Constructor ## Example class Student { String name; int age; // Regular constructor: same name as the class Student(String name,int age){ this.name= name; this.age= age; } void printInfo(){ print('Student: $name, Age: $age'); } } void main(){ var s1 = Student('tutorial',10); var s2 = Student('Xiao Ming',15); s1.printInfo(); s2.printInfo(); } Student: tutorial, age: 10Student: Xiao Ming, age: 15 ### Syntactic Sugar Constructor When constructor parameters are directly assigned to member variables, Dart provides a shorthand syntax. ## Example class Student { String name; int age; // Syntactic sugar: this.parameterName automatically assigns the parameter to the member variable with the same name // This approach is more concise than manually writing this.name = name Student(this.name,this.age); void printInfo(){ print('TUTORIAL Student: $name, Age: $age'); } } void main(){ var s = Student('tutorial',10); s.printInfo(); } TUTORIAL Student: tutorial, age: 10 ### Named Constructors A class can have multiple named constructors for different initialization logic. ## Example class Point { double x; double y; // Default constructor Point(this.x,this.y); // Named constructor: create origin Point.origin() : x =0, y =0; // Named constructor: create from a single value (x and y are the same) Point.diagonal(double value) : x = value, y = value; @override String toString()=>'Point($x, $y)'; } void main(){ var p1 = Point(3,4); var p2 = Point.origin(); var p3 = Point.diagonal(5); print('TUTORIAL Coordinates: $p1')
← Dart Interfaces And MixinsDart Collections β†’