简体   繁体   English

用于提取数学的类或结构(Swift)

[英]class or struct for extracting maths (Swift)

hi all i'm very new to programming and it's turning into a bit of a mission. 大家好,我对编程非常陌生,这正变成一项使命。

I'm trying to create a class with function to do some basic maths. 我正在尝试创建一个具有函数的类来进行一些基本的数学运算。 However I'm still learning how to extract the new value and maybe either pass in on within my class/func or pass into another. 但是,我仍在学习如何提取新值,并且可以在类/函数中传递或传递给另一个。

here is what I have so far 这是我到目前为止所拥有的

class fuel {
var rate = 3.7
var laps: Double = 13.0
var totalFuel: Double

init (rate: Double, laps: Double, totalFuel: Double){
    self.rate = rate
    self.laps = laps
    self.totalFuel = totalFuel

    }
func fuelMaths (fuel: Double) -> Double {
    self.totalFuel = (rate * Double(laps))
    print(self.totalFuel)

    }

} fuelMaths(self.totalFuel) } fuelMaths(self.totalFuel)

I'm getting an unresolved identifier error when I call "fuelMaths" or am I meaning to of created a struct? 调用“ fuelMaths”时出现无法解析的标识符错误,或者我的意思是创建结构吗?

Thanks in advance. 提前致谢。

Regarding class vs. struct, this seems like a struct is a better choice because it only contains a few stored properties which are all value types. 对于类与结构,这似乎是一个更好的选择,因为它只包含一些都是值类型的存储属性。

Even so, here are the issues: 即便如此,这里还是有问题:

1) Your fuelMaths instance method is not returning a value. 1)您的fuelMaths实例方法未返回值。 Your implementation should resemble this: 您的实现应类似于以下内容:

func fuelMaths(fuel:Double) -> Double {
    // "someValue" contains your result
    return someValue
}

2) The reason you are getting an unresolved identifier error is because you need to first declare an instance of fuel , and then call fuelMaths on that instance: 2)出现无法解析的标识符错误的原因是,您需要首先声明fuel的实例,然后在该实例上调用fuelMaths

// someRate, someLaps, someTotal, and someFuelValue previously defined

let myFuel = fuel(rate: someRate, laps: someLaps, totalFuel: someTotal)
let myReturnedValue = myFuel.fuelMaths(someFuelValue)

Finally, it appears that totalFuel is a good candidate for a computed property instead of a stored property. 最后,看来totalFuel是计算属性而不是存储属性的良好候选者。 This eliminates your need for the fuelMaths method. 这样就无需使用fuelMaths方法。 Just perform the calculations that are in fuelMaths inside the body of your computed property instead. 只需执行计算属性内部的fuelMaths中的计算即可。 It also means that your initializer only needs to provide two arguments for rate and laps : 这也意味着您的初始化程序只需要为ratelaps提供两个参数:

var totalFuel: Double {
    return rate * laps
}

init(rate:Double, laps:Double) { //... }

One more thing, the convention is to capitalize the names of your classes and structs. 还有一件事,约定是将类和结构的名称大写。

EDIT: 编辑:

The class version of your code might look like this: 您的代码的类版本可能如下所示:

class Fuel {
    var rate = 3.7  // default value tells compiler this is a Double
    var laps = 13.0
    var totalFuel: Double {
        return rate * laps
    }

    init(rate:Double, laps:Double) {
        self.rate = rate
        self.laps = laps
    }
}

The struct version of your code might look like this (notice that it doesn't require you to explicitly define an initializer, unlike a class): 代码的结构版本可能如下所示(注意,与类不同,它不需要您显式定义初始化程序):

struct Fuel {
    var rate = 3.7
    var laps = 13.0
    var totalFuel: Double {
        return rate * laps
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM