Swift 中函数的初始化和 class 继承如何工作
How does Initialization and class inheritance of function work in Swift
基于 JavaScript 代码中的示例,我想在 Swift 中应用相同的步骤。如果你能帮助我了解它如何工作以获得所有形状的面积。
试图用下面的代码解决它,但是它不起作用。
class Shape {
func getArea() {
}
}
class Square: Shape {
var a: Double
init(a: Double) {
self.a = a
super.init()
}
override func getArea() {
a * a
}
}
class Rectangle: Shape {
var a: Double
var b: Double
init(a: Double, b: Double) {
self.a = a
self.b = b
super.init()
}
override func getArea() {
a * b
}
}
class Circle: Shape {
var r: Double
init(r: Double) {
self.r = r
super.init()
}
override func getArea() {
r * r * Double.pi
}
}
let square = Square(a: 1.1)
let rectangle = Rectangle(a: 5.0, b: 9.1)
let circle = Circle(r: 2.3)
let shapes = [square, rectangle, circle]
func getTotalArea() -> Double {
let areas = shapes.map { [=12=].getArea() }
let sum = areas.reduce (1, +)
return sum
}
let shapesArea = getTotalArea()
print(shapesArea)
getArea
没有 return 任何东西。您必须定义 getArea() -> Double
。此外,Shape 应该是一个没有实现(没有大括号)的 func getArea() -> Double
协议。
另请注意,减少(求和)区域时,必须从0
开始,而不是从1
开始。
protocol Shape {
func getArea() -> Double
}
class Square: Shape {
var a: Double
init(a: Double) {
self.a = a
}
func getArea() -> Double {
a * a
}
}
class Rectangle: Shape {
var a: Double
var b: Double
init(a: Double, b: Double) {
self.a = a
self.b = b
}
func getArea() -> Double {
a * b
}
}
class Circle: Shape {
var r: Double
init(r: Double) {
self.r = r
}
func getArea() -> Double {
r * r * Double.pi
}
}
let square = Square(a: 1.1)
let rectangle = Rectangle(a: 5.0, b: 9.1)
let circle = Circle(r: 2.3)
let shapes: [Shape] = [square, rectangle, circle]
func getTotalArea() -> Double {
let areas = shapes.map { [=10=].getArea() }
let sum = areas.reduce(0, +)
return sum
}
let shapesArea = getTotalArea()
print(shapesArea)
一个更“敏捷”的解决方案是使用结构,使用它们自动生成的初始化,area
作为计算 属性:
protocol Shape {
var area: Double { get }
}
struct Square: Shape {
var a: Double
var area: Double {
a * a
}
}
struct Rectangle: Shape {
var a: Double
var b: Double
var area: Double {
a * b
}
}
struct Circle: Shape {
var r: Double
var area: Double {
r * r * .pi
}
}
let square = Square(a: 1.1)
let rectangle = Rectangle(a: 5.0, b: 9.1)
let circle = Circle(r: 2.3)
let shapes: [Shape] = [square, rectangle, circle]
func getTotalArea() -> Double {
return shapes
.map { [=11=].area }
.reduce(0, +)
}
let shapesArea = getTotalArea()
print(shapesArea)
基于 JavaScript 代码中的示例,我想在 Swift 中应用相同的步骤。如果你能帮助我了解它如何工作以获得所有形状的面积。
试图用下面的代码解决它,但是它不起作用。
class Shape {
func getArea() {
}
}
class Square: Shape {
var a: Double
init(a: Double) {
self.a = a
super.init()
}
override func getArea() {
a * a
}
}
class Rectangle: Shape {
var a: Double
var b: Double
init(a: Double, b: Double) {
self.a = a
self.b = b
super.init()
}
override func getArea() {
a * b
}
}
class Circle: Shape {
var r: Double
init(r: Double) {
self.r = r
super.init()
}
override func getArea() {
r * r * Double.pi
}
}
let square = Square(a: 1.1)
let rectangle = Rectangle(a: 5.0, b: 9.1)
let circle = Circle(r: 2.3)
let shapes = [square, rectangle, circle]
func getTotalArea() -> Double {
let areas = shapes.map { [=12=].getArea() }
let sum = areas.reduce (1, +)
return sum
}
let shapesArea = getTotalArea()
print(shapesArea)
getArea
没有 return 任何东西。您必须定义 getArea() -> Double
。此外,Shape 应该是一个没有实现(没有大括号)的 func getArea() -> Double
协议。
另请注意,减少(求和)区域时,必须从0
开始,而不是从1
开始。
protocol Shape {
func getArea() -> Double
}
class Square: Shape {
var a: Double
init(a: Double) {
self.a = a
}
func getArea() -> Double {
a * a
}
}
class Rectangle: Shape {
var a: Double
var b: Double
init(a: Double, b: Double) {
self.a = a
self.b = b
}
func getArea() -> Double {
a * b
}
}
class Circle: Shape {
var r: Double
init(r: Double) {
self.r = r
}
func getArea() -> Double {
r * r * Double.pi
}
}
let square = Square(a: 1.1)
let rectangle = Rectangle(a: 5.0, b: 9.1)
let circle = Circle(r: 2.3)
let shapes: [Shape] = [square, rectangle, circle]
func getTotalArea() -> Double {
let areas = shapes.map { [=10=].getArea() }
let sum = areas.reduce(0, +)
return sum
}
let shapesArea = getTotalArea()
print(shapesArea)
一个更“敏捷”的解决方案是使用结构,使用它们自动生成的初始化,area
作为计算 属性:
protocol Shape {
var area: Double { get }
}
struct Square: Shape {
var a: Double
var area: Double {
a * a
}
}
struct Rectangle: Shape {
var a: Double
var b: Double
var area: Double {
a * b
}
}
struct Circle: Shape {
var r: Double
var area: Double {
r * r * .pi
}
}
let square = Square(a: 1.1)
let rectangle = Rectangle(a: 5.0, b: 9.1)
let circle = Circle(r: 2.3)
let shapes: [Shape] = [square, rectangle, circle]
func getTotalArea() -> Double {
return shapes
.map { [=11=].area }
.reduce(0, +)
}
let shapesArea = getTotalArea()
print(shapesArea)