Flutter Dart中的类 对象

Dart 基本特征

私有属性/私有方法

Flutter Dart中的类 对象_第1张图片

import 'test88.dart';

main() {
  var home = new MainHome();
  home.execRun(); //间接的调用私有方法
}




class MainHome {
  String _name = "张三";//私有属性
  int age = 10;

  main() {
    _run();
    print(_name);
  }

  void _run() {
    print("私有方法");
  }

  execRun() {
    this._run();
  }
}



get用法

  var rect = Rect(10, 2);
  var rect1 = Rect1(10, 2);
  print( "面积是= ${rect.area()}");
  print( "面积是= ${rect1.area}");//注意调用直接通过访问属性的方式访问area


  rect1.areaHeight=6;
  print( "面积是= ${rect1.area}");

class Rect {
  num height;
  num width;

  Rect(this.height, this.width);
  
  //方法
  area() {
    return this.height * this.width;
  }
}


class Rect1 {
  num height;
  num width;

  Rect1(this.height, this.width);

  //get用法
  get area {
    return this.height * this.width;
  }

  //方法
  set areaHeight(value) {
    this.height = value;
  }
}

构造函数体运行之前初始化实例变量
 

class Rect2 {
  num height;
  num width;

  //可以在构造函数体运行之前初始化实例变量
   Rect2()
      :height =3,
        width=2 {
    print("height =$height ---- width=$width");
  }

  //get用法
  get area {
    return this.height * this.width;
  }

  //方法
  set areaHeight(value) {
    this.height = value;
  }
}

你可能感兴趣的:(flutter,android)