Dart is an object-oriented language

Dart is a utility class and single inheritance object-oriented language. All objects in DART are instances of classes. All classes are subclasses of Object and all classes have properties and methodsCopy the code

Defining a class

In Dart, we can define classes by the keyword class and the class name is usually capitalized by you. It's a big hump. If we have a function or a method. We start with lowercase letters; As we just said, classes are usually made up of attributes and methods. Let's implement a classCopy the code

Implement a simple class

Class PersonIno {// class attribute String name = 'Linyan '; int age = 30; // The class likes() {print(' like shopping '); Print (' my name is $name, this year $age'); Print (' my name is ${this.name}, this year ${this.age}'); }}Copy the code

Instantiate the class before using it

Void main() {// instantiate var p = new PersonIno(); // Call the class method p.intfo (); print(p.name); // We can also declare the type of p; PersonIno p1 = new PersonIno(); }Copy the code

The constructor in DART

Class PersonIno {// class attribute String name = 'Linyan '; int age = 30; PersonIno() {print(' I am the constructor and will be triggered when instantiated '); } // the class method likes() {print(' like shopping '); Print (' my name is $name, this year $age'); Print (' my name is ${this.name}, this year ${this.age}'); }}Copy the code

Use the constructor to initialize the value

Our current PersonIno class; Can only output a person's information and age if we need to output zhang, Li, 4 wang wu.... What about lots of people's information? At this point we can use the constructor.Copy the code
Void main() {// instantiate var p = new PersonIno(' lisi ', 20); p.info(); Var p1 = new PersonIno(' PersonIno ', 25); p1.info(); } class PersonIno {// attribute in class String name; int age; PersonIno(String name, int age) {this.name = name; this.age = age; Print (' my name is ${this.name}, this year ${this.age}'); }}Copy the code

Shorthand for the constructor in DART

PersonIno(this.name, this.age); PersonIno(String name, int age) {this.name = name; this.age = age; }Copy the code

Name the constructor.

Void main() {// instantiate var p = new PersonIno(' lisi ', 20); } class PersonIno {// attribute in class String name; int age; PersonIno(this.name, this.age); Personino.myfun () {print(' I am the named constructor '); }} We all know that the constructor fires by default when instantiated. Var p = new Personino.myfun (); var p = new Personino.myfun (); There can be only one constructor in a class. But there can be multiple constructorsCopy the code

Named constructors can also initialize values

Void main() {// instantiate var p = new Personino.myFun (' aftersound ', 22); p.info(); } class PersonIno {// attribute in class String name; int age; PersonIno(this.name, this.age); PersonIno.myFun(this.name, this.age); Print (' my name is ${this.name}, this year ${this.age}'); }}Copy the code

Pull the class out

In a real development, we might have many classes. This will make the file bigger and bigger. That's when we need to pull the class out so how do we pull the class out?Copy the code
Create a folder in the project's root directory: lib. place the classes in this folder with the same name as the class name: PersonIno import 'lib/ personino.dart '; Void main() {// instantiate var p = new Personino.myFun (' aftersound ', 22); p.info(); }Copy the code

Private methods and properties

Dart is not like other object-oriented languages. Dart does not have public private protected access modifiers but we can use _ to define a property or method as private. Note: If you add "_" before a property or method in the same file, it is still accessible [1] but if you add "_" before a property or method in a separate file, it is not accessible [2]Copy the code

For example [1]

Void main() {var p = new PersonInfo(' PersonInfo ', 18); print(p._age); // Output 18 is accessible} class PersonInfo {String name; int _age; PersonInfo(this.name, this._age) can still be accessed from the same file, though "_" is added to make it private; }Copy the code

Example [2] PersonInfo class file personinfo.dart in the lib directory

Class PersonIno {// attribute String _name; int age; PersonIno(this._name, this.age); Info () {// print(' my name is ${this._name}, this year ${this.age}'); }}Copy the code

At this point, an access error will appear

import 'lib/PersonIno.dart'; Void main() {var p = new PersonIno(' PersonIno ', 18); print(p._name); }Copy the code

Take the sum of two numbers

void main() { Sun c = new Sun(10, 20); print(c.backSum()); } class Sun { num a; num b; Sun(this.a, this.b); backSum() { return this.a + this.b; }}Copy the code

Use the getter to sum two numbers

void main() { Sun c = new Sun(10, 20); print(c.backSum); } class Sun {num a; num b; Sun(this.a, this.b); Get backSum{return this.a + this.b; }}Copy the code

Use of setter methods

void main() { Sun c = new Sun(10, 20); c.restateA = 100; Print (c ackSum); } class Sun {num a; num b; Sun(this.a, this.b); Get backSum {return this.a + this.b; }} set restateA(value) {this.a = value; }}Copy the code