Dart-类和对象

94 阅读3分钟

面向对象编程(OOP)的三个基本特征是:封装、继承、多态

封装: 封装是对象和类概念的主要特性。封装,把客观事物封装成抽象的类,并且把自己的部分属性和方法提供给其他对象调用, 而一部分属性和方法则隐藏。
继承: 面向对象编程 (OOP) 语言的一个主要功能就是“继承”。继承是指这样一种能力:它可以使用现有类的功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。
多态: 允许将子类类型的指针赋值给父类类型的指针, 同一个函数调用会有不同的执行效果 。

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

一个类通常由属性和方法组成。
void main(){    
    List list = new List();
    list.isEmpty;
    list.add('香蕉');
    list.add('香蕉1');

    Map map = new Map();
    map["username"] = "张三";
    map.addAll({"age":20});
    map.isEmpty;

    Object a = 123;
    Object v = true;
    print(a);
    print(v);
}

定义类、使用类

Dart是一门使用类和单继承的面向对象语言,所有的对象都是类的实例,并且所有的类都是Object的子类
class Person{
  String name = "张三";
  int age = 23;
  void getInfo(){
      // print("$name----$age");
      print("${this.name}----${this.age}"); // 推荐使用这种写法
  }
  void setInfo(int age){
    this.age = age;
  }

}
void main(){
    // 实例化
    var p1 = Person();
    print(p1.name);
    p1.getInfo();

    Person p2 = Person();
    print(p2.name);

    p1.setInfo(28);
    p1.getInfo();
}

自定义类的默认构造函数

class Person{
    String name = '张三';
    int age = 20; 
    // 默认构造函数
    Person(){
        print('这是构造函数里面的内容,这个方法在实例化的时候触发');
    }
    void printInfo(){   
        print("${this.name}----${this.age}");
    }
}

class Person{
    String name;
    int age; 
    // 默认构造函数
    Person(String name,int age){
        this.name=name;
        this.age=age;
    }
    void printInfo(){   
        print("${this.name}----${this.age}");
    }
}

class Person{
  String name;
  int age; 
  // 默认构造函数的简写
  Person(this.name,this.age);
  void printInfo(){   
    print("${this.name}----${this.age}");
  }
}

void main(){
 
    Person p1 = new Person('张三',20);
    p1.printInfo();

    Person p2 = new Person('李四',25);
    p2.printInfo();

}

自定义类的命名构造函数

dart里面构造函数可以写多个
class Person{
    String name;
    int age; 
    // 默认构造函数的简写
    Person(this.name,this.age);
  
    Person.now(){
        print('我是命名构造函数');
    }

    Person.setInfo(String name,int age){
        this.name = name;
        this.age = age;
    }

    void printInfo(){   
      print("${this.name}----${this.age}");
    }
}

void main(){
    var d = new DateTime.now(); // 实例化DateTime调用它的命名构造函数
    print(d);
    
    Person p1 = Person('张三', 20); // 默认实例化类的时候调用的是默认构造函数
    Person p1 = Person.now(); // 无参数的命名构造函数
    Person p1 = Person.setInfo('李四',30); // 有参数的命名构造函数
    p1.printInfo(); 

}

私有方法和私有属性

Dart和其他面向对象语言不一样,Dart中没有public、private、protected这些访问修饰符合但是我们可以使用" _ "把一个属性或者方法定义成私有(必须把类封装成一个单独的文件才能实现私有,如果在同一个文件中,即使class中使用" _ "修饰了属性,外部依然可以访问该属性)。

// 定义个Animal的类(此处定义的Animal类必须封装到一个单独的文件中才能实现name的私有化)
class Animal{
  String _name; // 私有属性
  int age; 
  // 默认构造函数的简写
  Animal(this._name,this.age);

  printInfo(){   
    print("${this._name}----${this.age}");
  }

  String getName(){ 
    return this._name;
  }
  
  // 定义一个私有方法
  void _run(){
    print('这是一个私有方法');
  }

  execRun(){
    this._run();  // 类里面方法的相互调用
  }
}

void main(){
    Animal a = Animal('小狗', 3);
    print(a.getName()); // 通过Animal的getName()方法访问私有属性
    a.execRun();   // 间接的调用私有方法
}

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

class Rect{
    int height;
    int width;
 
    getArea(){
        return this.height*this.width;
    } 
}

class Rect{
    num height;
    num width; 
  
    Rect(this.height,this.width);
    area(){
        return this.height*this.width;
    }
}

void main(){
    Rect r = (10,4);
    print("面积:${r.area()}");   
}

class Rect{
    num height;
    num width;   
    Rect(this.height,this.width);
    get area{
        return this.height*this.width;
    }
}

void main(){
    Rect r = Rect(10,2);
    print("面积:${r.area}"); // 注意调用直接通过访问属性的方式访问area
}

class Rect{
    num height;
    num width; 
    // 默认构造函数
    Rect(this.height,this.width);
    // getter方法
    get area{
        return this.height*this.width;
    }
    // setter方法
    set areaHeight(value){
        this.height=value;
    }
}

void main(){
    Rect r = Rect(10,4);
    print("面积:${r.area()}"); // 面积:40 
    r.areaHeight = 6;
    print(r.area); // 面积:24

}

类中的初始化列表

Dart中我们也可以在构造函数体运行之前初始化实例变量
class Rect{
    int height;
    int width;
    
    Rect():height=2,width=10{
        print("${this.height}---${this.width}"); // 输出20,在执行构造函数之前,宽高已经被赋值
    }
    getArea(){
        return this.height*this.width;
    }
}

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