news 2026/4/23 11:48:56

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/4/14 6:09:04

从MD5到国密SM4:中国自主加密算法崛起之路(深度内幕)

第一章&#xff1a;从MD5到国密SM4&#xff1a;中国加密演进的宏观图景 中国在信息安全领域的自主可控发展进程中&#xff0c;密码技术的演进扮演了关键角色。从早期广泛使用的国际算法如MD5、SHA-1&#xff0c;到如今全面推广具有自主知识产权的国密算法体系&#xff0c;尤其是…

作者头像 李华
网站建设 2026/4/18 11:03:24

LangChain实战快速入门笔记(五)--LangChain使用之Tools

LangChain实战快速入门笔记&#xff08;五&#xff09;–LangChain使用之Tools 文章目录LangChain实战快速入门笔记&#xff08;五&#xff09;--LangChain使用之Tools一、Tools概述1. 介绍2. Tool 的要素二、自定义工具1. 两种自定义方式第1种&#xff1a;使用tool装饰器&…

作者头像 李华
网站建设 2026/4/18 3:22:02

AI编程系列——git-worktree并行开发

什么是worktree Git Worktree允许从同一个Git仓库中检出多个分支到不同的目录中&#xff0c;每个worktree都有独立的工作目录&#xff0c;但共享相同的Git历史记录。 与传统git checkout的区别 最大区别在于 可以保存工作状态 不会因为切换导致其他的代码没了 worktree结合工…

作者头像 李华
网站建设 2026/4/21 18:35:25

【纤维协程调度优化指南】:掌握任务优先级分配的5大核心策略

第一章&#xff1a;纤维协程任务优先级调度的核心理念在现代高并发系统中&#xff0c;纤维&#xff08;Fiber&#xff09;作为一种轻量级的用户态线程&#xff0c;能够显著提升任务调度的灵活性与效率。与传统操作系统线程相比&#xff0c;纤维由运行时环境自行管理&#xff0c…

作者头像 李华
网站建设 2026/4/23 12:38:55

力扣题解析

今天这段代码实现了数组形式的整数加1虽然是简单题但是学会很有用处。题目&#xff1a;给定一个表示 大整数 的整数数组 digits&#xff0c;其中 digits[i] 是整数的第 i 位数字。这些数字按从左到右&#xff0c;从最高位到最低位排列。这个大整数不包含任何前导 0。将大整数加…

作者头像 李华
网站建设 2026/4/23 13:04:29

NVIDIA NIM 开发并部署 AI Agent(智能体)实战

基于 NVIDIA NIM 开发并部署 AI Agent&#xff08;智能体&#xff09;的完整案例&#xff0c;以企业级客服智能体为例&#xff0c;涵盖 Agent 核心逻辑开发、基于 NIM 的推理服务部署、Agent 服务封装与上线全流程。 案例背景 开发一个“金融产品咨询智能体”&#xff0c;具备以…

作者头像 李华