美文网首页
Dart中的对象、类

Dart中的对象、类

作者: 二猪哥 | 来源:发表于2019-09-25 09:48 被阅读0次

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

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

Dart所有的东西都是对象,所有的对象都继承自Object类。

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

一个类通常由属性和方法组成。

自定义类

class Person{
  String name="张三";
  int age=23;
  
  void getInfo(){
      print("${this.name}----${this.age}");
  }

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

  //实例化
  // var p1=new Person();
  // print(p1.name);
  // p1.getInfo();

  Person p1=new Person();
  // print(p1.name);

  p1.setInfo(28);
  p1.getInfo();   //张三----28
  
}

默认构造函数

默认构造函数无参数
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();  //张三----20

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

}

命名构造函数

dart里面构造函数可以写多个(默认构造函数只有一个,命名构造函数可有多个);

默认实例化类的时候调用的是 默认构造函数;如:

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

命名构造函数;如:

var d=new DateTime.now();   //实例化DateTime调用它的命名构造函数
print(d);

代码实现如下:

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(){
  Person p1=new Person.now();   //命名构造函数

  Person p2=new Person.setInfo('李四',30);
  p2.printInfo();  //李四----30
}

把类抽成一个文件.dart

创建一个Person.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}");
  }
}

在main方法调用;首先引入头文件“ import 'lib/Person.dart'; ”

import 'lib/Person.dart';

void main(){

  Person p1=new Person.setInfo('李四1',30);
  p1.printInfo();   //李四1----30

}

Dart中的私有方法和私有属性

Dart和其他面向对象语言不一样,Data中没有 public private protected这些访问修饰符合;但是我们可以使用 _ 把一个属性或者方法定义成私有。
代码示例:

Animal.dart文件

class Animal{
  String _name;   //私有属性
  int age; 
  
  Animal(this._name,this.age); //默认构造函数
 
  void printInfo(){   
    print("${this._name}----${this.age}");
  }

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

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

main方法调用


import 'lib/Animal.dart';

void main(){
 
  Animal a=new Animal('小狗', 3);

  print(a.getName());  //小狗

  a.execRun();   //间接的调用私有方法 
}

getter和setter修饰词的用法

  • 通过get计算矩形的面积
class Rect{
  num height;
  num width;   
  Rect(this.height,this.width);
  get area{
    return this.height*this.width;
  }
}

void main(){
  Rect r=new Rect(10,2);
  print("面积:${r.area}");      //注意调用直接通过访问属性的方式访问area
}
  • set方法
class Rect{
  num height;
  num width; 
  
  Rect(this.height,this.width);
  get area{
    return this.height * this.width;
  }
  set areaHeight(value){
    this.height=value;
  }
}

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

  print(r.area);

}

类中的初始化列表

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()); 
   
}

相关文章

  • Dart语法 -- [06 - 类和对象]

    Dart是一个面向对象的语言,面向对象中非常重要的概念就是类,类产生了对象。 1.1. 类的定义 在Dart中,定...

  • Dart中的对象、类

    面向对象编程(OOP)的三个基本特征是:封装、继承、多态 封装:封装是对象和类概念的主要特性。封装,把客观事物封装...

  • Flutter 代码规范整理

    Dart 语法简介 Flutter是使用Dart语言开发的。Dart语言是基于类的纯面向对象语言。Dart 中的所...

  • Flutter(六) Dart语言基础-面向对象特性

    Dart是一个面向对象的语言,面向对象中非常重要的概念就是类,类产生了对象。今天,我们就具体来学习Dart 中重要...

  • flutter【7】dart语言--类

    类 dart 是纯面向对象语言,每个对象都是某个类的实例,并且所有类都是继承自 Object。 dart中继承使用...

  • Dart学习笔记

    Dart语法预览 重要概念: 所有能够使用变量引用的都是对象, 每个对象都是一个类的实例。在 Dart 中 甚至连...

  • Dart 语法(二)

    类和对象 使用class关键字声明类 使用new、构造函数创建一个对象 所有对象继承自Object Dart中默认...

  • 2019-09-20: 九: Flutter之Dart第六节(类

    九: Flutter之Dart第六节(类和对象)? Dart 是一个面向对象的语言、面向对象中非常重要的概念就是类...

  • 8.Dart-对象属性, 方法, get, set

    /** * Dart所有的东西都是对象, 所有的对象都是继承自Object类 * Dart是一门实用类和单继承的面...

  • 【Dart】Dart类与对象

    类与对象 类 继承 抽象类 接口 混入 泛型 枚举类类-简介 构造器 (构造函数) 默认构造函数与类同名的函数,在...

网友评论

      本文标题:Dart中的对象、类

      本文链接:https://www.haomeiwen.com/subject/whipsctx.html