[英]Dart 2.0.0 Access to the variable of a class that extends another
I have these classes (as @KevinMoore showed here ): 我有这些类(如@KevinMoore 在此处所示 ):
import 'dart:math';
class Photo {
final double area;
// This constructor is library-private. So no other code can extend
// from this class.
Photo._(this.area);
// These factories aren't needed – but might be nice
factory Photo.rect(double width, double height) => new RectPhoto(width, height);
factory Photo.circle(double radius) => new CirclePhoto(radius);
}
class CirclePhoto extends Photo {
final double radius;
CirclePhoto(this.radius) : super._(pi * pow(radius, 2));
}
class RectPhoto extends Photo {
final double width, height;
RectPhoto(this.width, this.height): super._(width * height);
}
My question is: if I create a Photo
object in this way: Photo photo = new CirclePhoto(15.0, 10.0);
我的问题是:如果我以这种方式创建
Photo
对象: Photo photo = new CirclePhoto(15.0, 10.0);
, how can I get the radius
from photo
object? ,如何从
photo
对象获取radius
? Can I make the radius
variable private and get it with a getter? 我可以将
radius
变量设为私有,并用吸气剂将其获取吗?
Thank you. 谢谢。
You need a get
method : 您需要一个
get
方法:
class Rectangle {
num left, top, width, height;
Rectangle(this.left, this.top, this.width, this.height);
// Define two calculated properties: right and bottom.
num get right => left + width;
set right(num value) => left = value - width;
num get bottom => top + height;
set bottom(num value) => top = value - height;
}
void main() {
var rect = Rectangle(3, 4, 20, 15);
assert(rect.left == 3);
rect.right = 12;
assert(rect.left == -8);
}
Doc: https://www.dartlang.org/guides/language/language-tour 文件: https : //www.dartlang.org/guides/language/language-tour
You just need to cast the value to a CirclePhoto
to access the radius
value. 您只需要将该值转换为
CirclePhoto
即可访问radius
值。 A Photo
does not have a radius, so if you do: Photo
没有半径,因此,如果有:
Photo photo = new CirclePhoto(15.0);
print(photo.radius); // Compile-time error, Photo has no "radius"
you get an error, but if you do: 您会收到一个错误,但是如果这样做:
Photo photo = new CirclePhoto(15.0);
print((photo as CirclePhoto).radius);
it works. 有用。
This performs a down-cast from Photo
to CirclePhoto
. 这会执行从
Photo
到CirclePhoto
的向下 CirclePhoto
。 The static type system cannot tell that this is safe (some photos are not circle-photos), so it checks at run-time. 静态类型系统无法确定这是安全的(某些照片不是圆形照片),因此它会在运行时进行检查。 If the photo isn't actually a
CirclePhoto
, you get a run-time error. 如果照片实际上不是
CirclePhoto
,则会出现运行时错误。
Another option is to use type-check-based type promotion: 另一种选择是使用基于类型检查的类型提升:
Photo photo = new CirclePhoto(15.0);
if (photo is CirclePhoto) print(photo.radius);
This promotes the photo
variable to be a CirclePhoto
in the code guarded by the is
-check. 这在
is
-check保护的代码中将photo
变量提升为CirclePhoto
。 (Type promotion is fairly primitive, it basically needs to be a local variable that you are not assigning to, and the type you check must be a sub-type of the current type of the variable). (类型提升是相当原始的,它基本上需要是一个您没有分配给它的局部变量,并且您检查的类型必须是该变量当前类型的子类型)。
Making radius
private and adding a getter makes no difference . 将
radius
私有并添加吸气剂没有什么区别 。 You already have a getter names radius
on CirclePhoto
, the one introduced by your final field. 您已经在
CirclePhoto
上有了一个吸气剂名称的radius
,这是您的最终字段引入的。 There is no advantage in renaming the field to be private and adding another getter, it's pure overhead. 将字段重命名为私有字段并添加另一个getter没有好处,这纯粹是开销。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.