news 2026/5/3 3:24:14

Android学Dart学习笔记第十五节 类

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Android学Dart学习笔记第十五节 类

文档描述

Dart is an object-oriented language with classes and mixin-based inheritance. Every object is an instance of a class, and all classes except Null descend from Object. Mixin-based inheritance means that although every class (except for the top class, Object?) has exactly one superclass, a class body can be reused in multiple class hierarchies. Extension methods are a way to add functionality to a class without changing the class or creating a subclass. Class modifiers allow you to control how libraries can subtype a class.

Dart是一种基于类和mixin的继承的面向对象语言。每个对象都是某个类的实例,而除Null之外的所有类都是object的后代
基于mixin的继承意味着尽管每个类(除了最上面的类,Object?)都只有一个超类,但一个类的主体可以在多个类层次中重用。
扩展方法是一种向类添加功能而不改变类或创建子类的方法。
类修饰符允许你控制库如何为一个类添加子类型。

信息量极大,暂时不理解没关系,继续看

Using class members 使用类成员

这部分和kt一样
使用点(.)来引用实例变量或方法:

varp=Point(2,2);// Get the value of y.assert(p.y==2);// Invoke distanceTo() on p.double distance=p.distanceTo(Point(4,4));

使用?.而不是.为了避免最左边的操作数为null时发生异常:

// If p is non-null, set a variable equal to its y value.vara=p?.y;

Using constructors

You can create an object using a constructor. Constructor names can be either ClassName or ClassName.identifier.

这点和java略有不同。 在下面例子中的三个函数都是构造函数。ClassName.identifier形式的构造函数,又叫命名构造函数,所有formJson是命名工厂构造函数

classPoint{varx;vary;// 1. 生成构造函数(Generative Constructor)// 最常见的构造函数,直接创建新实例Point(this.x,this.y);// 2. 命名构造函数(Named Constructor)// 只是语法糖,仍然是生成构造函数Point.origin():x=0,y=0;// 3. 工厂构造函数(Factory Constructor)// 不总是创建新实例,可以返回缓存实例或子类实例factoryPoint.fromJson(Map<String,int>json){returnPoint(json['x']!,json['y']!);}}

在dart中,new关键字是可选的,两者等价。

finalp=newPoint(1,2);varnewP=Point(1,2);

有一些类提供了常量构造函数,如下:

classImmutablePoint{finaldouble x,y;constImmutablePoint(this.x,this.y);}

要使用常量构造函数创建编译时常量,请将const关键字放在构造函数名称之前,如下

varc=constImmutablePoint(1,2);varc1=constImmutablePoint(1,2);print(c==c1);//true

如果不需要也可以直接创建

varc=ImmutablePoint(1,2);varc1=ImmutablePoint(1,2);print(c==c1);//false

如果一个字段被定义为常量,后面的cosnt是可以省略的

constc=ImmutablePoint(1,2);constc1=ImmutablePoint(1,2);print(c==c1);//true

你可以仔细看看上面3段的区别
下面的官方的例子:

// Lots of const keywords here.constpointAndLine=const{'point':const[constImmutablePoint(0,0)],'line':const[constImmutablePoint(1,10),constImmutablePoint(-2,11)],};

省略后

// Only one const, which establishes the constant context.constpointAndLine={'point':[ImmutablePoint(0,0)],'line':[ImmutablePoint(1,10),ImmutablePoint(-2,11)],};

如果常量构造函数在常量上下文之外(就是不是用的const a,而是var )并且在没有const的情况下调用,它会创建一个非常量对象(并且也不是 var a = const A())

Getting an object’s type

要在运行时获取对象的类型,您可以使用Object属性runtimeType,它返回一个Type对象。

constc=ImmutablePoint(1,2);print('The type of a is ${c.runtimeType}');//The type of a is ImmutablePoint

使用类型测试操作符(is, as, is!)而不是runtimeType来测试对象的类型。在正式环境中,obj is A 比ibj.runtimeType更稳定。

Instance variables

classPoint{double?x;// Declare instance variable x, initially null.double?y;// Declare y, initially null.double z=0;// Declare z, initially 0.}

声明为nullable类型的未初始化实例变量的值为null。非空实例变量必须在声明时初始化。

所有实例变量都会生成一个隐式getter方法。没有初始化器的非最终实例变量和后期最终实例变量也会生成一个隐式setter方法。
setter&gettter方法我们在函数那一章学习过,已经忘却的同学可以回顾下(函数)

classPoint{double?x;// Declare instance variable x, initially null.double?y;// Declare y, initially null.}voidmain(){varpoint=Point();point.x=4;// Use the setter method for x.assert(point.x==4);// Use the getter method for x.assert(point.y==null);// Values default to null.}

初始化一个声明的非晚期实例变量会在实例创建时设置其值,在构造函数及其初始化列表执行之前。因此,非late实例变量的初始化表达式(在=之后)无法访问this

double initialX=1.5;classPoint{// OK, can access declarations that do not depend on `this`:double?x=initialX;// ERROR, can't access `this` in non-`late` initializer:double?y=this.x;// OK, can access `this` in `late` initializer:late double?z=this.x;// OK, `this.x` and `this.y` are parameter declarations, not expressions:Point(this.x,this.y);}

late我们之前在学习变量时也学习过,忘却的同学点这里变量

实例变量可以是final,在这种情况下,它们必须只设置一次。在声明时初始化最终的、非晚期的实例变量,可以使用构造函数的参数,也可以使用构造函数的初始化列表:

classProfileMark{finalString name;finalDateTime start=DateTime.now();ProfileMark(this.name);ProfileMark.unnamed():name='';}

如果需要在构造函数体启动后给final实例变量赋值,可以使用下列选项之一:
使用工厂构造函数;
使用late final,但要小心:没有初始化方法的late final会给API添加一个setter。(更多细节点这个文档了解)

Implicit interfaces

每个类都隐式定义了一个接口,其中包含该类的所有实例成员及其实现的任何接口。如果您想创建一个支持类B的API而不继承B实现的类A,类A应该实现B接口。

类通过在implements子句中声明一个或多个接口,然后提供这些接口所需的api来实现这些接口。

// A person. The implicit interface contains greet().classPerson{// In the interface, but visible only in this library.finalString _name;// Not in the interface, since this is a constructor.Person(this._name);// In the interface.Stringgreet(String who)=>'Hello, $who. I am $_name.';}// An implementation of the Person interface.classImpostorimplementsPerson{Stringget_name=>'';Stringgreet(String who)=>'Hi $who. Do you know who I am?';}StringgreetBob(Person person)=>person.greet('Bob');voidmain(){print(greetBob(Person('Kathy')));print(greetBob(Impostor()));}

这里使我有点懵懵的,有点不太好接受。 我们努力吧。
正如文档所言,每个类都定义了一个隐藏了接口,我可以理解为class,也是一种接口,当当作接口使用时就只有结构是有效的,当作类对象使用时,方法的实现才有效果。

一个类同样可以实现多个接口

classPointimplementsComparable,Location{...}

类变量和方法

使用static关键字实现类范围的变量和方法。

Static variables 静态变量
classQueue{staticconstinitialCapacity=16;// ···}voidmain(){assert(Queue.initialCapacity==16);}

静态变量在使用之前不会初始化,java是类加载时初始化

Static methods
import'dart:math';classPoint{double x,y;Point(this.x,this.y);staticdoubledistanceBetween(Point a,Point b){vardx=a.x-b.x;vardy=a.y-b.y;returnsqrt(dx*dx+dy*dy);}}voidmain(){vara=Point(2,2);varb=Point(4,4);vardistance=Point.distanceBetween(a,b);assert(2.8<distance&&distance<2.9);print(distance);}

同样,因为初始化时机的不同,静态方法不可以调用实例变量或者实例方法。

** 考虑为常见或广泛使用的实用程序和功能使用顶级函数而不是静态方法。**

可以使用静态方法作为编译时常量。例如,可以将静态方法作为参数传递给常量构造函数。

classExample{finalint value;constExample(this.value);staticintstaticMethod()=>42;}voidmain(){// 传递静态方法作为常量构造函数的参数constinstance=Example(Example.staticMethod());print(instance.value);// 输出: 42}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/3 0:23:41

纤维协程异常处理实战(99%开发者忽略的关键细节)

第一章&#xff1a;纤维协程异常处理的核心挑战在现代高并发系统中&#xff0c;纤维&#xff08;Fiber&#xff09;作为一种轻量级线程模型&#xff0c;被广泛应用于协程调度。然而&#xff0c;其异常处理机制相较于传统线程更为复杂&#xff0c;主要源于执行上下文的动态切换与…

作者头像 李华
网站建设 2026/5/3 1:12:01

纤维协程超时配置避坑手册(资深架构师20年经验总结)

第一章&#xff1a;纤维协程超时配置的核心概念在现代高并发服务架构中&#xff0c;纤维协程&#xff08;Fiber Coroutine&#xff09;作为一种轻量级执行单元&#xff0c;广泛应用于提升系统吞吐量与资源利用率。超时配置是保障协程不无限阻塞、避免资源泄漏的关键机制。合理的…

作者头像 李华
网站建设 2026/5/3 2:54:53

【高并发系统设计必修课】:纤维协程调度模型全剖析

第一章&#xff1a;纤维协程的任务调度在现代高并发系统中&#xff0c;纤维协程&#xff08;Fiber Coroutine&#xff09;作为一种轻量级执行单元&#xff0c;显著提升了任务调度的效率与灵活性。与操作系统线程不同&#xff0c;纤维协程由用户态调度器管理&#xff0c;避免了内…

作者头像 李华
网站建设 2026/5/1 0:26:07

还在手动处理API响应?Symfony 8自动格式化功能全揭秘

第一章&#xff1a;Symfony 8 响应格式化的变革意义Symfony 8 在响应格式化机制上的演进&#xff0c;标志着框架在构建现代化 API 方面迈出了关键一步。通过内置更智能的序列化策略和内容协商机制&#xff0c;开发者能够以声明式方式定义响应结构&#xff0c;大幅减少手动处理 …

作者头像 李华
网站建设 2026/5/1 0:26:18

揭秘PHP 8.6性能瓶颈:如何利用新特性打造超强监控系统

第一章&#xff1a;PHP 8.6 的性能监控面板PHP 8.6 引入了内置的轻量级性能监控面板&#xff0c;开发者无需依赖第三方扩展即可实时查看脚本执行效率、内存使用和函数调用堆栈。该功能专为开发与调试环境设计&#xff0c;可通过配置快速启用&#xff0c;帮助定位性能瓶颈。启用…

作者头像 李华
网站建设 2026/5/1 0:26:15

IntelliJ IDEA 安装 pmd-idea-1.9.0.zip 插件详细教程

PMD-IDEA 是 IntelliJ IDEA 的一个代码检查插件&#xff0c;基于 PMD​ 这个静态代码分析工具做的。 1. 下载插件 安装包下载&#xff1a;https://pan.quark.cn/s/d10d1c36a788 下载完就是一个 zip 文件&#xff0c;别解压它&#xff0c;就留着原样。 2. 打开 IDEA 打开你的…

作者头像 李华