Dart 方法';设置年龄';isn';t为类定义';人';。飞镖

Dart 方法';设置年龄';isn';t为类定义';人';。飞镖,dart,Dart,我不熟悉dart,当我尝试将值设置为一个变量(这是一个私有变量)并且使用getters和setters 但我得到了这个错误: 未为类“Person”定义方法“set_age” 下面是我的代码的样子 class Person{ String firstName, lastName; int _pAge; double pSalary; // syntactic sugar Person(this.firstName, this.lastName, this.pSalary);

我不熟悉
dart
,当我尝试将值设置为一个变量(这是一个私有变量)并且使用
getters
setters

但我得到了这个错误:

未为类“Person”定义方法“set_age”

下面是我的代码的样子

class Person{
  String firstName, lastName;
  int _pAge;
  double pSalary;

//  syntactic sugar
  Person(this.firstName, this.lastName, this.pSalary);

//  Named constructor
  Person.origin(){
    firstName = "";
    lastName = "";
    _pAge = 0;
    pSalary = 0.0;
  }

  String fullName() => this.firstName + " " + this.lastName;

//  getters and setters for _pAge
  set set_age(int age){
    _pAge =  age;
  }

  int get get_age => _pAge;
}

main() {
  Person p1 = new Person("Jananath", "Banuka", 15000.00);
  Person p2 = new Person("Thilina", "Kalansooriya", 55000.00);

  p1.set_age(10); //this is where the error is coming from

  print(p1.fullName());
  print(p2.fullName());
}

您将setter称为方法。您应该更改setter的使用方式或将setter更改为方法:

class Person1 {
  int _pAge;

  set set_age(int age) =>_pAge = age;
  int get get_age => _pAge;
}

class Person2 {
  int _pAge;

  void set_age(int age) => _pAge = age;
  int get_age() => _pAge;
}

void main() {
  final p1 = Person1();
  p1.set_age = 10;
  print(p1.get_age);

  final p2 = Person2();
  p2.set_age(10);
  print(p2.get_age());
}

您将setter称为方法。您应该更改setter的使用方式或将setter更改为方法:

class Person1 {
  int _pAge;

  set set_age(int age) =>_pAge = age;
  int get get_age => _pAge;
}

class Person2 {
  int _pAge;

  void set_age(int age) => _pAge = age;
  int get_age() => _pAge;
}

void main() {
  final p1 = Person1();
  p1.set_age = 10;
  print(p1.get_age);

  final p2 = Person2();
  p2.set_age(10);
  print(p2.get_age());
}
p1.set_年龄=10岁; p1.设定年龄(10岁); p1.set_年龄=10岁; p1.设定年龄(10岁);
您不是在编写惯用的Dart,这可能就是您将setter视为函数的原因。setter是通过分配给它们来调用的,因此它不应该是
p1。set_age(10)
它应该是
p1.age=10

作为惯用的Dart,您的代码如下所示:

班级人员{
字符串firstName,lastName;
智力年龄;
双薪;
人员(this.firstName,this.lastName,this.salary):年龄=0;
个人来源()
:firstName=“”,lastName=“”,年龄=0,工资=0;
字符串get fullName=>“$firstName$lastName”;
}
main(){
人员p1=新人员(“Jananath”、“Banuka”,15000.00);
人员p2=新人员(“Thilina”、“Kalansooriya”,55000.00);
p1.年龄=10岁;
打印(p1.全名);
打印(p2.全名);
}
这使得
age
成为公共字段。不需要将私有字段隐藏在公共setter/getter后面,然后将其转发到该字段。您可以直接公开该字段

Dart有setter和getter,这正是因为这样以后就不会阻止您向set/get操作添加逻辑。你总是可以改变的

int年龄;

int\u年龄;
int get age=>\u age;
设置年龄(整数值){
日志(“设置年龄:$value”);
_年龄=价值;
}

如果需要的话,请稍后再讨论。

您不是在编写惯用的Dart,这可能就是您将setter视为函数的原因。setter是通过分配给它们来调用的,因此它不应该是
p1。set_age(10)
它应该是
p1.age=10

作为惯用的Dart,您的代码如下所示:

班级人员{
字符串firstName,lastName;
智力年龄;
双薪;
人员(this.firstName,this.lastName,this.salary):年龄=0;
个人来源()
:firstName=“”,lastName=“”,年龄=0,工资=0;
字符串get fullName=>“$firstName$lastName”;
}
main(){
人员p1=新人员(“Jananath”、“Banuka”,15000.00);
人员p2=新人员(“Thilina”、“Kalansooriya”,55000.00);
p1.年龄=10岁;
打印(p1.全名);
打印(p2.全名);
}
这使得
age
成为公共字段。不需要将私有字段隐藏在公共setter/getter后面,然后将其转发到该字段。您可以直接公开该字段

Dart有setter和getter,这正是因为这样以后就不会阻止您向set/get操作添加逻辑。你总是可以改变的

int年龄;

int\u年龄;
int get age=>\u age;
设置年龄(整数值){
日志(“设置年龄:$value”);
_年龄=价值;
}

如果需要的话,可以在以后的时间点。

设置年龄=10但是给像set\u something这样的名字不是聪明的想法
set\u age=10但是给像set\u这样的名字不是什么聪明的主意
  void set_age(int age){
    _pAge =  age;
  }