Author : Li Juncai (jcLee95) : https://blog.csdn.net/qq_28550263
Email: [email protected]
Address of this article : https://blog.csdn.net/qq_28550263/article/details/131242679
[Introduction]: This article introduces the usage of accessors in the Dart language.
Table of contents
1 Overview
1.1 What is an accessor
Accessors are special methods in the Dart language that allow you to perform operations when accessing and modifying properties of an object. There are two main types of accessors: getters and setters.
1.2 Types of accessors
Functionally, accessors are divided into two functions: "fetch" and "storage", which correspond to two categories: getter and setter:
accessor type | describe |
---|---|
getter | Method for getting the value of an object's property. |
setter | Method for setting the value of an object property. |
1.3 Why accessors are needed
We can introduce why sometimes we need to use memory from the following aspects:
project | describe |
---|---|
data encapsulation | Accessors can help achieve data encapsulation, that is, hiding the properties and implementation details of an object, exposing only a set of public interfaces. In this way, external code cannot directly access and modify the internal state of the object, thereby protecting the integrity of the object. |
Validate and manipulate data | Accessors allow you to perform actions when getting or setting a property's value, such as verifying that the data is valid, performing calculations, or triggering other actions. |
2. Syntax format of Dart accessor
2.1 getter syntax
In a class, a getter method is declared using the get keyword, which does not receive any parameters:
Type get propertyName {
// 返回属性值}
Please refer to the example in section 2.2 setter syntax .
2.2 setter syntax
In a class, the getter method is declared using the set keyword and needs to receive a parameter as the set value:
set propertyName(Type value) {
// 设置属性值}
For example:
import 'package:dio/dio.dart';
import 'package:dio/io.dart';
class ApiService {
Dio _dio = Dio();
int _connectTimeout = 5000;
int _receiveTimeout = 3000;
bool _useProxy = false; // 默认不使用代理
String _proxyUrl = ""; // 代理 URL
/// 获取当前是否使用代理
bool get useProxy => _useProxy;
/// 设置是否使用代理
set useProxy(bool value) {
_useProxy = value;
}
/// 获取当前代理 URL
String get proxyUrl => _proxyUrl;
/// 设置代理 URL
set proxyUrl(String value) {
_proxyUrl = value;
}
// ...其它方法
}
3. Accessor and Access Control
Like other methods in dart, accessors can also be defined with public or private access, where you can compare the difference between private fields and public accessors:
project | describe |
---|---|
private field | In Dart, fields and methods _ starting are private by default. By combining private fields and public accessors, you can implement access control to object properties. |
public accessor | Public accessors refer to getter and setter methods _ without . With public accessors, you can control how external code can access and modify an object's properties. |
4. Accessor Advanced
4.1 Automatic accessor
Dart supports automatic accessors, which allow you to automatically generate getter and setter methods for a property without explicitly defining them. To use automatic accessors, simply declare the property as public (without _
starting with ).
For example:
class Person {
// 自动存取器
String name;
}
void main() {
var person = Person();
person.name = "Jack"; // 自动调用 setter
print(person.name); // 自动调用 getter
}
4.2 Accessors and inheritance
In inheritance, subclasses can override the accessors of the parent class. This allows you to modify or extend the behavior of parent class properties in subclasses. When overriding an accessor, you need to use @override
annotations and ensure that the overridden accessor has the same signature as the parent class.
For example:
class Animal {
String _name;
String get name => _name;
set name(String value) {
_name = value;
}
}
class Dog extends Animal {
set name(String value) {
if (value.isEmpty) {
throw ArgumentError("名字不能为空!");
}
super.name = value;
}
}
void main() {
var dog = Dog();
dog.name = "Trump"; // 调用子类的 setter
print(dog.name); // 调用父类的 getter
}
In the example above, Dog
the class inherits from Animal
the class and overrides name
the setters of the parent class. When setting the name for Dog
the object , the subclass's setter method is called, which checks whether the name is empty, and then calls the superclass's setter method. When getting the name of Dog
the object , the getter method of the parent class is called.