dart-lang / dart-lang/language
About the Dart lang assignment of class private member variables and method
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
Dart lang is great!I very like the Dart lang! But the Dart currently only has library-private declarations,**Strongly recommended that assignment of private member variables should be restrict to the current class by default!!!Dart should has class-private declarations,a name prefixed with an underscore (e.g. _age) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an implementation detail。Or, in order to be compatible with the old version, it is strongly recommended to add double underscores (e.g. _ _age) as class-private declarations,In no case should an instance call or modify a private variable or call a private method,Private is absolutely private and can't be accessed anywhere except itself!!!**
Most of the time, we need to hide some sensitive information and methods, make them private, only allow to assign and call in the class, and only allow public variables and methods in the instance; just like Python "_ _". This is very important!!!
I know that big companies like Google don't expect to improve on this proposal. If so, more's the pity!
Example code,file name test.dart:
```dart
main() {
var student = Student(1);
student.name = "Peter";
print("private _age is ${student._age}"); //Must No access,this`s private member variables
student._age =500; //Must No assignment
student.age =600;
print("now age is ${student.age}"); //now age is 0
student.say();
//pub method call private method
//Private method say hello!
student._say(); //Must No call,this`s private member method
}
class Student {
int id = -1;
String name;
int _age = -1;
Student(this.id, {this.name});
int get age => _age;
int set age(v) {
if (v > 18 && v < 180){
_age = v;
}else {
_age = 0;
}
}
//Private method
void _say() {
print("Private method say hello!");
}
void say(){
print("pub method call private method");
this._say();
}
}
```kotlin code
class Greeter() {
private var a =1
fun greet() {
println("Hello")
}
private fun test(){
println("test")
}
}
fun main() {
var d = Greeter()
d.greet()
d.a = 12
d.test()
}
//Cannot access 'a': it is private in 'Greeter'
//Cannot access 'test': it is private in 'Greeter'
Contributor guide
Assessment
This issue has not been assessed yet.