swift重写

本文详细介绍了Swift语言中子类如何通过重写机制提供自定义的实现,包括方法、属性等,以及如何正确使用override关键字确保代码的健壮性和清晰性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A subclass can provide its own custom implementation of an instance method, type method, instance property, type property, or subscript that it would otherwise inherit from a superclass. This is known as overriding.
子类可以提供实例方法,类型方法,实例属性,类型属性,附属脚本的自定义实现,它与从父类继承不同。这被叫做继承。
To override a characteristic that would otherwise be inherited, you prefix your overriding definition with the override keyword. Doing so clarifies that you intend to provide an override and have not provided a matching definition by mistake. Overriding by accident can cause unexpected behavior, and any overrides without the override keyword are diagnosed as an error when your code is compiled.
为了重写特性而不是继承需要前置overriding关键字。这样澄清是为了提供的是重写,而不会报错。
The override keyword also prompts the Swift compiler to check that your overriding class’s superclass (or one of its parents) has a declaration that matches the one you provided for the override. This check ensures that your overriding definition is correct.
重写关键字也在提示swift编译器你重写类的父类或者某一个父类有匹配的。这种检查确保你的重写定义是正确的。
访问父类的属性,方法,附属脚本
When you provide a method, property, or subscript override for a subclass, it is sometimes useful to use the existing superclass implementation as part of your override. For example, you can refine the behavior of that existing implementation, or store a modified value in an existing inherited variable.
当你提供了重写方法,有时候利用已经实现父类的方法实现又是非常有用的。例如,你可以改善已经存在的实现,或者在一个已经存在的变量进行修改值。
重写方法
You can override an inherited instance or type method to provide a tailored or alternative implementation of the method within your subclass.
你可以重写一个继承来的实例方法或者类型方法来定制或者替换方法的实现在子类。
重写属性
You can override an inherited instance or type property to provide your own custom getter and setter for that property, or to add property observers to enable the overriding property to observe when the underlying property value changes.
你可以重写继承来的实例或者类型属性,为属性提供自定义的setter和getter方法,或者增加属性观察观察属性变化。
Overriding Property Getters and Setters
重写属性的setter和getter方法
You can provide a custom getter (and setter, if appropriate) to override any inherited property, regardless of whether the inherited property is implemented as a stored or computed property at source. The stored or computed nature of an inherited property is not known by a subclass—it only knows that the inherited property has a certain name and type. You must always state both the name and the type of the property you are overriding, to enable the compiler to check that your override matches a superclass property with the same name and type.
不管其实属性是存储属性还是计算属性,你都可以提供自定义的get(或者set)来重写继承来的属性。继承来的存储或者计算属性在起始的本来面目在子类并不可知。子类仅仅知道继承属性的的名字和类型。你必须让父类和子类的属性名字和类型一致才可以。
You can present an inherited read-only property as a read-write property by providing both a getter and a setter in your subclass property override. You cannot, however, present an inherited read-write property as a read-only property.
在重写的时候,你可以把继承来的只读属性作为读写属性,通过在子类里边提供set和get方法。然而,你不可以把继承来的读写属性重写成只读属性。
这里写图片描述
如果你setter方法来重写,你必须提供getter方法来重写。在getter方法中,如果你不想要修改继承来的属性值。你可以简单传递及传递继承来的值super.someProperty。
调用顺序
遇到super就走父类的get方法。
注释1
这里写图片描述
注释2
这里写图片描述
记住不能在set或者get方法里直接写成self调用,因为这样会一直调用陷入死循环。
禁止重写
想要把重写给禁止掉,那么可以在方法或者属性前面添加关键字final。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值