在Dart语法中,用关键字class表示类。

class Person {}
  • 私有和公有

Dart中公有和私有的界定是以文件界定的,并不是通过类界定的,如果不允许其他文件访问当前文件中的成员变量,或者不允许调用方法,可以定义成员变量或者方法的时候以下划线开始。

class Person {//内部变量String _id = "111";//外部变量String? name;//内部方法void _printId() => print("id is $_id");//外部方法void printName() => print("name is $name");
}

注意,如果是在其他文件中定义一个Person子类,是不能继承内部方法的。如果是在本文件中是可以继承的。

//本文件中是没问题的,编译器是有提示的。
class Teacher extends Person {Teacher(String id, int age) : super(id, age);@overridevoid _printId() {// TODO: implement _printIdsuper._printId();}}
//其他文件中只能继承外部方法
class Student extends Person {Student.initWith(String name, int age) : super.initWith(name, age);@overridevoid printName() {// TODO: implement printName}}
  • 构造方法
class Person {String? name;String _id = "111";late int age;//默认构造函数// Person(this._id, this.age);//命名构造函数Person.initWith(String name, int age) {this.name = name;this.age = age;}//带有初始化列表的构造函数Person(String id,int age):assert(age > 0) {this.age = age;this._id = id;}}
  • 类属性和类方法

Dart中类变量和类方法都用static关键字修饰,在调用的时候方法如下。

void _test() {Person.isa = "1111111111";Person.getMethList()
}class Person {String? name;String _id = "111";late int age;static String isa = "0";static void getMethList() {print("it is not object-c");}
}
  • 单例

Dart中,构造函数是没有return的,如果想要ruturn,需要在构造函数前面加上关键字factory,原始写法

class Application {static  Application? _shared;// 私有的命名构造函数,外界不能调用Application._init();// 使用factory关键字factory Application() {if (_shared == null) {_shared = Application._init();}return _shared!;}}

可以稍作优化,三行代码搞定单例。

class Application {static  Application? _shared;// 私有的命名构造函数,外界不能调用Application._init();factory Application() => _shared ??= Application._init();
}
  • 继承

Dart中是单继承,继承一个类后,可以通过@override来重写父类中的方法,也可以通过super来调用父类中的方法。构造函数是不能呗继承的。

class Animal extends Object {late double  height;late double  weight;void eat() => print("eat food"); void sleep() => print("sleep");
}class Dog extends Animal {@overridevoid eat() => print("eat shit");
}class Cat extends Animal {}
  • 接口(implements)

Dart中没有interface关键字,但每个类都是隐式的接口。当类被abstract修饰的时候(抽象类),是无法被实例化为对象的,只能当接口。

void _test() {//这里会报错CanAccompanyMaster acc = new CanAccompanyMaster();}//接口定义
abstract class CanAccompanyMaster {void dance() {print("dance");}void makeFun();}class MakeFood {void cook() {print("as delicious food");}
}//Dart中每个类都是一个隐式的接口
//接口中的方法需要被重新实现
class Pig extends Animal implements CanAccompanyMaster, MakeFood {@overridevoid cook() {// TODO: implement cook}@overridevoid dance() {// TODO: implement dance}@overridevoid makeFun() {// TODO: implement makeFun}}
  • 混入(mixins)

在面向对象的语音中,mixins是一个可以把自己的方法提供给其他类使用,并且可以不需要成为其他类子类的类。以非继承的方式来复用类中的代码。要使用mixins,用关键字with来复用类中的代码。


class Cat extends Animal {void catchMouse() => print("catch mouse");
}// The class 'Cat' can't be used as a mixin because it extends a class other than Object
// class Dog extends Animal with Cat {
//   @override
//   void eat() => print("eat shit");
// }class LitterCat {void catchMouse() => print("catch mouse");
}class Dog extends Animal with LitterCat {@overridevoid catchMouse() {// TODO: implement catchMousesuper.catchMouse();}
}

注意,如果是作为混合的类,不能有extends。如有有extends编译器会报错。

Flutter之Dart语法(二)面向对象相关推荐

  1. Flutter学习之Dart语法特性

    一.前言 第一天把Flutter环境搭建了,并简单实现第运行第一个Flutter项目,感觉很不错,一些基本操作和原生体验差不多.用Flutter框架写过App项目的开发者都知道,Flutter是一个使 ...

  2. Flutter 开发中最实用的 Dart 语法知识

    零.前言 都说 Flutter 是谷歌的新宠,这段时间有空,就学习了一下 Dart 语法.本篇文章将会详细全面的介绍 Dart 常用语法. 一.变量和常量 在 Drat 语言中,一切皆对象,对象的默认 ...

  3. Dart语法篇之面向对象继承和Mixins(六)

    简述: 上一篇文章中我们详细地介绍了Dart中的面向对象的基础,这一篇文章中我们继续探索Dart中面向对象的重点和难点(继承和mixins). mixins(混合)特性是很多语言中都是没有的.这篇文章 ...

  4. 【Flutter】Dart 面向对象 ( 类定义 | 类的继承 | 私有变量 | 可选参数 | 默认参数 | 初始化列表 )

    文章目录 一. Dart 面向对象 二. 类定义 三. 类的继承 四. 私有变量 五. 可选参数与默认参数 六. 初始化列表 七. 完整代码示例 八. 相关资源 一. Dart 面向对象 OOP 基本 ...

  5. Flutter基础篇(2)-- 老司机用一篇博客带你快速熟悉Dart语法

    版权声明:本文为博主原创文章,未经博主允许不得转载.https://www.jianshu.com/p/3d927a7bf020 转载请标明出处: https://www.jianshu.com/p/ ...

  6. Flutter的基础知识之Dart语法

    Flutter的基础知识之Dart语法 关于TextStyle的设置 import 'package:flutter/material.dart'; void main() => runApp( ...

  7. Flutter开发指南之理论篇:Dart语法04(库,异步,正则表达式)

    总目录 Flutter开发指南之理论篇:Dart语法01(数据类型,变量,函数) Flutter开发指南之理论篇:Dart语法02(运算符,循环,异常) Flutter开发指南之理论篇:Dart语法0 ...

  8. 【-Flutter/Dart 语法补遗-】 sync* 和 async* 、yield 和yield* 、async 和 await

    前言 类别 关键字 返回类型 搭档 多元素同步 sync* Iterable<T> yield.yield* 单元素异步 async Future<T> await 多元素异步 ...

  9. 自定义Flutter Lint插件实现自己的Dart语法规则(静态语法分析检测)

    摘要: 本文实现了一个自定义的语法检查插件,功能是:当新写一个dart类,如果类名中包含ViewModel,那么必须添加前缀HDW.在vscode中效果如下: 在网上搜索自定义Dart语法检查或自定义 ...

  10. java map集合的等号改为逗号_老司机用一篇博客带你快速熟悉Dart语法

    [前言]Dart语言是使用flutter框架开发时候必备的语言,flutter是一个跨平台的框架,一套代码就可以完美实现安卓和ios两个平台,适配也很不错,Dart语言很友好,和java很类似,学习成 ...

最新文章

  1. RHEL5上配置VNCSERVER
  2. VC++下命名管道编程的原理及实现
  3. myBatis之事务管理
  4. 从零写一个编译器(八):语义分析之构造符号表
  5. A quick presentation of the Visual Studio 2010 editions per role
  6. spring-boot-devtools热加载不起作用
  7. NFS服务的端口分配
  8. 互相关python程序_互相关(cross-correlation)及其在Python中的实现
  9. 如何更改字体隶书html,隶书转换
  10. vs2008 sp1
  11. 常用的邮箱服务器配置
  12. 安卓动画两种基本实现方式
  13. ufs 固态硬盘_单芯片SSDs / eMMC / UFS
  14. 基于 Tampermonkey 插件平台开发的淘宝直通车爬虫
  15. 老java程序员告诉你要不要选择外包,外包公司的好处你知道吗
  16. shell脚本实例-交叉证认
  17. hash:哈希表 哈希桶
  18. yolov2模型导入keras方法及问题
  19. python爬虫:案例四:新浪微指数
  20. splitter MP3 file

热门文章

  1. 图解TCP/IP 读书笔记(二)
  2. Bottom Sheets(底部动作条)使用介绍
  3. H 扫雷 / 手写哈希+bfs
  4. 云端服务器有哪些型号,有那些云端服务器
  5. IO和文件系统性能分析工具
  6. 瘦身打包Springboot(Maven)项目
  7. CentOS 开机出现错误信息:Could not apply the stored configuration for monitors
  8. 联想大客户技术支持培训学习笔记(PC硬件篇)
  9. 【N32G457】基于RT-Thread和N32G457的高阶魔方
  10. 【数据库】windows安装多个mysql