首页 \ 问答 \ 在Swift中重写方法或属性时的多态性(Polymorphism when overriding methods or properties in Swift)

在Swift中重写方法或属性时的多态性(Polymorphism when overriding methods or properties in Swift)

我想通过编译器确保CarViewController仅在车辆属性中接收Car

鉴于以下快速示例代码:

class Vehicle {
    func doSomething(){}
}

class Car: Vehicle {
    func doCarThings(){}
}

class VehicleViewController : UIViewController {
    var vehicle : Vehicle!;

    override func viewDidLoad() {
        super.viewDidLoad();
        vehicle.doSomething();
    }
}

class CarViewController:VehicleViewController {
    var vehicle: Car!

    override func viewDidLoad() {
        super.viewDidLoad();
        vehicle.doCarThings();
    }
}

我收到以下错误: Cannot override mutable property 'vehicle' of type 'Vehicle!' with covariant type 'Car!' Cannot override mutable property 'vehicle' of type 'Vehicle!' with covariant type 'Car!'

我尝试了一种基于泛型的方法:

class Vehicle {
    func doSomething(){}
}

class Car: Vehicle {
    func doCarThings(){}
}

class VehicleViewController<T:Vehicle> : UIViewController {
    var vehicle : T!;

    override func viewDidLoad() {
        super.viewDidLoad();
        vehicle.doSomething();
    }
}

class CarViewController:VehicleViewController<Car> {
    override func viewDidLoad() {
        super.viewDidLoad();
        vehicle.doCarThings();
    }
}

这是正确的,但在故事板类中使用泛型导致错误(因为它们被编译为objective-c)。

我怎样才能做到这一点,而不使用泛型?

谢谢!


I want to ensure by compiler that CarViewController only receives a Car in the vehicle property.

Given the following swift example code:

class Vehicle {
    func doSomething(){}
}

class Car: Vehicle {
    func doCarThings(){}
}

class VehicleViewController : UIViewController {
    var vehicle : Vehicle!;

    override func viewDidLoad() {
        super.viewDidLoad();
        vehicle.doSomething();
    }
}

class CarViewController:VehicleViewController {
    var vehicle: Car!

    override func viewDidLoad() {
        super.viewDidLoad();
        vehicle.doCarThings();
    }
}

I get the following error: Cannot override mutable property 'vehicle' of type 'Vehicle!' with covariant type 'Car!'

I tried with a generics-based approach:

class Vehicle {
    func doSomething(){}
}

class Car: Vehicle {
    func doCarThings(){}
}

class VehicleViewController<T:Vehicle> : UIViewController {
    var vehicle : T!;

    override func viewDidLoad() {
        super.viewDidLoad();
        vehicle.doSomething();
    }
}

class CarViewController:VehicleViewController<Car> {
    override func viewDidLoad() {
        super.viewDidLoad();
        vehicle.doCarThings();
    }
}

It is correct but using generics in storyboard classes results in errors (since they get compiled to objective-c).

How can I do this without using generics?

Thanks!


原文:https://stackoverflow.com/questions/43937762
更新时间:2023-12-06 09:12

最满意答案

因此,根据我们的讨论,我们发现:

Cesium.EntityCesium.Entity的一个数组。

Cesium.Entityposition字段是一个Cesium.PositionProperty ,它是一个接口,它有一个方法getValue ,接受一个Cesium.JulianDate作为它的第一个参数。 所以,把所有这些放在一起,你需要的唯一改变是:

peakPosition = peaks[i].position;

peakPosition = peaks[i].position.getValue(new Cesium.JulianDate());

So, per our discussion we have discovered:

peaks is an array of Cesium.Entitys.

and

the position field of a Cesium.Entity is a Cesium.PositionProperty, which is an interface that has a method getValue, accepting a Cesium.JulianDate as its first parameter. So, putting all that together, the only change you need is:

peakPosition = peaks[i].position;

to

peakPosition = peaks[i].position.getValue(new Cesium.JulianDate());

相关问答

更多
  • 严格的答案 :因为JS规范这样说 : 如果Type(x)是Number,那么 如果x是NaN,则返回false。 如果y是NaN,则返回false。 有用的答案 :浮点数的IEEE 754规范(所有语言都使用浮点数)表示NaN永远不会相等。 Strict answer: Because the JS spec says so: If Type(x) is Number, then If x is NaN, return false. If y is NaN, return false. Useful ans ...
  • 接受的答案是100%无疑是错误的 。 不是一半错误甚至略有错误。 当这个问题在搜索中出现时,我担心这个问题很长一段时间会混淆和误导程序员。 NaN旨在通过所有计算来传播,像病毒一样感染他们,所以如果你深入,复杂的计算中的某个地方遇到了NaN,那么你不会冒出一个看似合理的答案。 否则通过身份NaN / NaN应等于1,以及所有其他后果如(NaN / NaN)== 1,(NaN * 1)== NaN等。如果你想象你的计算在某个地方出错(舍入产生一个零分母,产生NaN)等,那么你可能会从你的计算中得到非常不正确( ...
  • 你忘了在减法中包装绝对值。 负数的sqrt是NaN。 编辑:我很蠢。 你可能想要做欧氏距离,即sqrt((x2-x1)^ 2 +(y2-y1)^ 2),平方而不是abs。 (如果你打算做出租车距离,如果你只能水平和垂直移动,也就是距离,那就是abs(x2-x1)+ abs(y2-y1)。) You forgot to wrap absolute value around the subtractions. sqrt of a negative number is NaN. EDIT: I am dumb. ...
  • 您不在e函数中返回值。 You don't return a value in your e function.
  • 如果使用左连接,如果df2.State中不存在df2.State值,则它将在代码中返回NaN,这表示不存在匹配。 我将检查状态列中的每个字符串之后是否有任何额外的空格,并确保所有值都是大写字母并转换为str 。 If you use a left join, if df2.State value does not exist in df1.State, it will return NaN in Code, meaning there is not a match. I will check if ther ...
  • 我认为应该在“城市”而不是“国家”字段上调用地图,如下所示: df['State'] = df['City'].map(state_dict) 但是,这有一个问题,它会覆盖任何不在您的字典中的城市的原始“国家”值,例如“芝加哥”。 解决这个问题的一个解决方案是下面的语法笨拙(但我相信是正确的)代码: df['State'] = df.apply(lambda x: state_dict[x['City']] if x['City'] in state_dict else x['State'], axis= ...
  • 因此,根据我们的讨论,我们发现: Cesium.Entity是Cesium.Entity的一个数组。 和 Cesium.Entity的position字段是一个Cesium.PositionProperty ,它是一个接口,它有一个方法getValue ,接受一个Cesium.JulianDate作为它的第一个参数。 所以,把所有这些放在一起,你需要的唯一改变是: peakPosition = peaks[i].position; 至 peakPosition = peaks[i].position.ge ...
  • getHours成员是一个函数而不是值。 因此,您将一个数字乘以一个方法并获得NaN 。 确保调用该方法 time1.setHours(time1.getHours() + (6)); The getHours member is a function not a value. Hence you're multiplying a number by a method and getting NaN. Make sure to invoke the method time1.setHours(time1. ...
  • 调用apply ,数据帧被强制转换为矩阵。 如果存在任何字符列,则将整个数据帧强制转换为字符矩阵,并将所有NaN转换为NA df_1 <- data.frame(a=c("a","b","c"), b=c(NaN, NaN,NaN)) df_2 <- data.frame(a=c(1,2,3), b=c(NaN, NaN,NaN)) as.matrix(df_1) a b [1,] "a" NA [2,] "b" NA [3,] "c" NA as.matrix(df_2) ...
  • 你可以修改Float#/使用细化(请记住Ruby中的大多数操作符只是方法) module RaiseExceptionOnNaN refine Float do def /(*) super.tap {|result| raise ZeroDivisionError if result.nan?} end end end 然后在您的模块/类中,使用该模块 class MyClass using RaiseExceptionOnNaN # Have a try ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。