Flutter学习-dart(6):dart类和对象

1. 面向对象编程(OOP)的三个基本特征

  • Dart所有的东西都是对象,所有的对象都继承自Object类
  • Dart是一门使用类和单继承的面向对象语言,所有的对象都是类的实例,并且所有的类都是Object的子类

1.1 封装

  • 封装是对象和类概念的主要特性。封装,把客观事物封装成抽象的类,并且把自己的部分属性和方法提供给其他对象调用, 而一部分属性和方法则隐藏

1.2 继承

  • 面向对象编程 (OOP) 语言的一个主要功能就是“继承”。继承是指这样一种能力:它可以使用现有类的功能,并在无需重新编写原来的类的情况下对这些功能进行扩展

1.3 多态

  • 允许将子类类型的指针赋值给父类类型的指针, 同一个函数调用会有不同的执行效果

2. 定义类并创建对象

2.1 example

class Person {
  String name ;
  int age;
  //默认构造函数
  Person(String name,int age){
     this.name=name;
     this.age=age;
 }
   //默认构造函数的简写
   Person(this.name,this.age);
  
    /*
    dart里面构造函数可以写多个
    */

  Person(this.name,this.age);
  
  Person.now(){
    print('我是命名构造函数');
  }

  Person.setInfo(String name,int age){
    this.name=name;
    this.age=age;
  }
  void getInfo() {
    // print("$name----$age");
    print("${this.name}----${this.age}");
  }

  void setInfo(int age) {
    this.age = age;
  }
}

void main() {
    Person p1 = new Person();
    p1.getInfo();
}

2.2 单独的类封装成一个模块

  1. 定义一个class, 编写属性及方法
  2. 在需要使用的文件中使用 import语句导入即可
  3. 如: import 'lib/Person.dart';

2.3 Dart中我们也可以在构造函数体运行之前初始化实例变量

class Rect{

  int height;
  int width;
  Rect():height=2,width=10{
    
    print("${this.height}---${this.width}");
  }
  getArea(){
    return this.height*this.width;
  } 
}

void main(){
  Rect r=new Rect();
  print(r.getArea());  //20
}

3. dart中私有方法和属性

  1. 私有属性/方法不能被直接调用, 但能被共有方法间接调用!
String _name;   //私有属性

void _run(){
    print('这是一个私有方法');
}

4. 类中的getter和setter修饰符的用法

class Rect {
  //声明私有属性
  int _height;
  int _width;

  Rect(this._height, this._width);

  //提供getter/setter
  int get width => _width;

  set width(int value) {
    this._width = value;
  }

  int get height => _height;

  set height(int value) {
    this._height = value;
  }

  //提供 获取面积的方法
  int get area => _width * _height;
}

void main() {
  Rect rect = new Rect(10, 20);
  print(rect.width); //20
  print(rect.height); //10
  print(rect.area); //200

  rect.width = 12;
  print(rect.area); //120
}

5. 类中的静态变量和方法

  • 使用static 关键字来实现类级别的变量和函数;
  • 静态方法不能访问非静态成员,非静态方法可以访问静态成员
class Person {
  static String name = '张三';
  int age=20;
  
  static void show() {
    print(name);
  }
  void printInfo(){  /*非静态方法可以访问静态成员以及非静态成员*/

      // print(name);  //访问静态属性
      // print(this.age);  //访问非静态属性
      show();   //调用静态方法
  }
  static void printUserInfo(){//静态方法
        print(name);   //静态属性
        show();        //静态方法
        //print(this.age);     //静态方法没法访问非静态的属性
        // this.printInfo();   //静态方法没法访问非静态的方法
        // printInfo();
  }
}

6. dart中对象操作符(js中没有, 记住!)

  • ? 条件运算符
  • as 类型转换
  • is 类型判断
  • .. 级联操作 (连缀) (记住)

示例:

  • 有以下person类
class Person {
  String name;
  num age;
  Person(this.name,this.age);
  void printInfo() {
    print("${this.name}---${this.age}");  
  }
}

6.1 p不为null, 则执行printInfo

Person p;
p?.printInfo();

6.2 把p1当成是person实例, 并调用printInfo

var p1;

p1='';

p1=new Person('张三1', 20);

// p1.printInfo();
(p1 as Person).printInfo();

6.3 用于判断p1是否是Person实例

print(p1 is Person) //true

6.4 用于连续访问对象的属性和方法的操作:

Person p1=new Person('张三1', 20);

p1.printInfo();

p1..name="李四"
    ..age=30
    ..printInfo();

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>