重点实验室网站建设的意义wordpress wp_list_pages 样式
重点实验室网站建设的意义,wordpress wp_list_pages 样式,建站网站源码,连云港专业网站制作公司序言
之前我们学习过函数#xff0c;那么一个类中有多少种方法呢#xff1f;这篇文章我们一起来学习
Instance methods
这是最常见的方法
对象的实例方法可以访问实例变量和this。
import dart:math;class Point {final double x;final double y;// Sets the x and y instanc…序言之前我们学习过函数那么一个类中有多少种方法呢这篇文章我们一起来学习Instance methods这是最常见的方法对象的实例方法可以访问实例变量和this。importdart:math;classPoint{finaldouble x;finaldouble y;// Sets the x and y instance variables// before the constructor body runs.Point(this.x,this.y);doubledistanceTo(Point other){vardxx-other.x;vardyy-other.y;returnsqrt(dx*dxdy*dy);}}Operators计算符他和方法有什么关系呀大多数运算符是具有特殊名称的实例方法。Dart允许您定义具有以下名称的运算符要声明运算符先使用内置的标识符运算符(operator)再使用正在定义的运算符。下面的例子定义了向量的加法、减法-和相等classVector{finalint x,y;Vector(this.x,this.y);Vectoroperator(Vector v)Vector(xv.x,yv.y);Vectoroperator-(Vector v)Vector(x-v.x,y-v.y);overridebooloperator(Object other)otherisVectorxother.xyother.y;overrideintgethashCodeObject.hash(x,y);}voidmain(){finalvVector(2,3);finalwVector(2,2);assert(vwVector(4,5));assert(v-wVector(0,1));}下面是num类中对于的定义Getters and setters这个也是我们学习过的内容了getter和setter是特殊的方法提供了对对象属性的读写访问。回想一下每个实例变量都有一个隐式的getter如果合适的话还会加上一个setter。你可以通过实现getter和setter来创建额外的属性使用get和set关键字/// A rectangle in a screen coordinate system,/// where the origin (0, 0) is in the top-left corner.classRectangle{double left,top,width,height;Rectangle(this.left,this.top,this.width,this.height);// Define two calculated properties: right and bottom.doublegetrightleftwidth;setright(double value)leftvalue-width;doublegetbottomtopheight;setbottom(double value)topvalue-height;}voidmain(){varrectRectangle(3,4,20,15);assert(rect.left3);rect.right12;assert(rect.left-8);}运算符如 需要先获取当前值然后进行计算和赋值。Dart 规范要求调用 getter 仅一次以避免副作用。Abstract methods实例方法、getter 和 setter 方法都可以是抽象的它们定义了一个接口但将具体实现留给其他类。抽象方法只能存在于抽象类或mixin中。abstractclassDoer{// Define instance variables and methods...voiddoSomething();// Define an abstract method.}classEffectiveDoerextendsDoer{voiddoSomething(){// Provide an implementation, so the method is not abstract here...}}