企襴 る語雑碁 蠍一 覦 れ
DApp れ(, ICO )
ERC-20,ERC-712,ERC-1155-ERC-1400, Multi-Sig れ
- Introduction to Ethereum Smart Contracts and Practical Exercises
- DApp Development Practice (Voting, ICO, etc.)
- Hands-on Practice with ERC-20, ERC-721, ERC-1155, ERC-1400, Multi-Sig, and More
8. Method Requirements
protocol SomeProtocol {
static func someTypeMethod()
}
protocol RandomNumberGenerator {
func random() -> Double
}
class LinearCongruentialGenerator: RandomNumberGenerator {
var lastRandom = 42.0
let m = 139968.0
let a = 3877.0
let c = 29573.0
func random() -> Double {
lastRandom = ((lastRandom * a + c).truncatingRemainder(dividingBy:m))
return lastRandom / m
}
}
let generator = LinearCongruentialGenerator()
print("Here's a random number: (generator.random())")
// Prints "Here's a random number: 0.3746499199817101
print("And another one: (generator.random())")
// Prints "And another one: 0.729023776863283"
9. Method Requirements
mutating れ襯 語ろ伎れ 覲蟆 螳ロる 蟆
給.
mutating れ 螳 襷 .
protocol Togglable {
mutating func toggle()
}
enum OnOffSwitch: Togglable {
case off, on
mutating func toggle() {
switch self {
case .off:
self = .on
case .on:
self = .off
}
}
}
var lightSwitch = OnOffSwitch.off
lightSwitch.toggle()
print(lightSwitch) // lightSwitch is now equal to .on
13. Protocols as Types
protocol RandomNumberGenerator {
func random() -> Double
}
class LinearCongruentialGenerator: RandomNumberGenerator {
var lastRandom = 42.0
let m = 139968.0
let a = 3877.0
let c = 29573.0
func random() -> Double {
lastRandom = ((lastRandom * a + c).truncatingRemainder(dividingBy:m))
return lastRandom / m
}
}
class Dice {
let sides: Int
let generator: RandomNumberGenerator
init(sides: Int, generator: RandomNumberGenerator) {
self.sides = sides
self.generator = generator
}
func roll() -> Int {
return Int(generator.random() * Double(sides)) + 1
}
}
var d6 = Dice(sides: 6, generator: LinearCongruentialGenerator())
for _ in 1...5 {
rint("Random dice roll is (d6.roll())")
}
// Random dice roll is 3, Random dice roll is 5, Random dice roll is 4
// Random dice roll is 5, Random dice roll is 4