- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在学习本教程:https://www.raywenderlich.com/160728/object-oriented-programming-swift
...并尝试对代码进行一些个人调整,看看我是否能够自己理解一些核心概念。
我最终得到以下代码:
class Instrument {
var brand: String
var model: String
init(brand: String, model: String) {
self.brand = brand
self.model = model
}
func tune() {
print("\(model) tuned !")
}
func play() {
print("\(model) plays an improvised melody")
}
func perform() {
print(tune())
print(play())
}
}
这个子类:
class Piano: Instrument {
var hasPedals: Bool
init(hasPedals: Bool ) {
self.hasPedals = hasPedals
super.init(brand: brand, model: model)
}
}
此时,我收到以下错误消息:
Playground execution failed:
error: Object oriented programming.playground:28:41: error: use of 'self' in property access 'model' before super.init initializes self super.init(brand: brand, model: model) ^
error: Object oriented programming.playground:28:27: error: use of 'self' in property access 'brand' before super.init initializes self super.init(brand: brand, model: model)
我觉得这是一个非常明显的错误,但我不知道如何解决。
编辑:非常感谢你们提供 super 干净的答案。现在对我来说很有意义!
如果我理解正确,即使您在子类中使用自定义 init 方法,您也需要初始化所有存储的属性(来自父类(super class)和子类),对吧?
最佳答案
您正在调用父类(super class)的 init
方法来初始化您的子类的一部分。然而,在你初始化它之前,你传递了一个未初始化的变量。只需将这些变量添加到 Piano
的参数中即可:
init(hasPedals: Bool, brand: String, model: String)
关于swift - 在 super.init 初始化 self super.init 之前,在属性访问 'self' 中使用 'model'(品牌 : brand, 模型:模型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46510969/
如果有人问过这个问题,我深表歉意,但如果用户缩小他们的显示,我会尝试将我的导航栏居中。 现在,我将导航栏项 float 到右侧,将品牌 float 到左侧以进行大型显示。当显示缩小时,它们保留在右侧和
是否可以在不修改/从源代码构建的情况下更改 Eclipse 品牌?喜欢: 更改启动画面 更改启动器/exe 名称和图标 换盒子 更改应用程序窗口标题 在从 eclipse 网站下载的 Eclipse
我曾尝试在没有 YouTube 品牌的情况下嵌入视频,因为它会将我的流量发送到 YouTube。我设置了 modestbranding=1 和 showinfo=0 但当我将光标悬停在播放器上时,我仍
我是 JSF 的新手,不知道如何在我的 Bootsfaces 导航栏中为我的图像添加链接。 图像文件夹中。 最佳答案 brandImg 只是要显示的图像的 URL。它可以是相对的也
我以前使用 Xcode 6.1.1,今天我安装了 Xcode 7.3 beta 3,它给出了一些警告,其中之一是“Brand Assets”,如下图所示。 我将我的图标添加到 AppIcon,一切正常
我正在使用 这会删除右侧底部的“Youtube” Logo 。并且还删除了悬停时出现的“标题栏”。 但这个问题是,当我将鼠标悬停在视频上时,然后在右侧底部“Youtube”缩略图/文本后面出现,当我
这是我的想法(原谅 Django 格式): class VehicleMake(Model): name = CharField(max_length=50) class VehicleMod
几天前我开始学习图像识别,我想做一个需要识别 Android 中不同品牌 Logo 的项目。 例如:如果我在 Android 设备中拍摄 Nike 标志的照片,那么它需要显示“Nike”。 低计算时间
我正在使用 mysql 查询返回适合 php 中某些汽车的产品数组,如下所示; array { [0]=> array(3) { ["sku"]=> string(16) "123a "
只是想让你知道,到今天为止我对 Bootstrap 还是个新手。我以前有使用 HTML/CSS 的经验,所以我决定尝试学习一些新东西,然后我直接进入了 Bootstrap 4 的深层次,跳过了 3(我
我正在尝试在我正在构建的网站上收紧 HTML5。导航和 Logo 需要在顶部栏中,我包括一个 slider 、引号和一些按钮。我不确定 header 是否真的应该包含引述或按钮。 如果不是,我真的需要
我创建了一个 Blazor WASM 项目。 它正在使用身份验证,并为此导航到身份区域中它自己的服务器端页面。 我试图搭建这些页面以更改导航栏上的标题,但我无法弄清楚。 它当前显示项目名称(即 Bla
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
有人知道是否有办法为 Azure AD B2C 发送的电子邮件添加品牌吗? 本页https://learn.microsoft.com/en-us/azure/active-directory-b2c
谁知道Mifare 1K card中写入和读取数据的代码是什么? ??我们使用 C#。 最佳答案 来自 DreamInCode , 查看 Goethals 的评论: "Pretty hard to f
我找到了很多解决这个主题的帖子,但是我的代码仍然遇到问题: 我有 first.png 是黑白的,second.png 是彩色的。我需要在悬停时为当前品牌着色。可以找到示例:h
我正在尝试使用 React Bootstrap 在我的 React 应用程序上创建一个与导航栏一起使用的 Logo ,并且我使用了 documentation as a guide .但是,当使用lo
我需要使用自定义产品名称构建基于 android chromium 的自定义浏览器。 我找到了配置文件(src/chrome/app/theme/chromium/BRANDING)并更改了 PROD
我想知道如何将品牌应用到 CRM 2011 网站? 我正在考虑添加一个客户横幅来代替放置在每个页面顶部的当前 Microsoft Dynamics CRM Logo (在顶部功能区上方的 div 中)
我有一个相当简单的查询,用于获取车辆属性,例如年份、品牌和型号。目前的数据看起来与此类似...: +------+------+-----------+-------+----------+-----
我是一名优秀的程序员,十分优秀!