class AoTeMan{
var name:String
init(name:String){
self.name = name
}
func action(otherName:String){
print("\(name)打\(otherName)")
}
}
class Monster {
var name:String
init(otherName:String){
self.name = otherName
}
}
let s1 = AoTeMan(name: "奥特曼")
let s2 = Monster(otherName: "小怪兽")
s1.action(s2.name)
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s6 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s7 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s8 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s9 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s10 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}
class PC {
func pcNumber() -> Int{
return Int(arc4random_uniform(100)) + 1
}
}
let b = PC()
let c = b.pcNumber()
var isP = true
while isP{
print("请输入1到100的随机数:",terminator:"")
let a = inputInt()
if a == c{
print("恭喜你回答正确")
isP = false
}else if a > c{
print("你的数字大了")
}else {
print("你的数字小了")
}
}
在做以上两个的时候还没有发现什么问题,毕竟是单一对象比较简单,但是在进行下段代码的时候就出现错误了。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #4f8187}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s7 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s8 {font: 18.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s9 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s10 {font-variant-ligatures: no-common-ligatures; color: #000000}
let a = 3.14
class minOvral {
var radius:Double
init(radius:Double){
self.radius = radius
}
func area() -> Double{
return a * radius * radius
}
func showRadius() -> Double{
return (radius + 3) * (radius + 3) * a
}
}
class MaxOvral {
func area(radius:Double) -> Double{
return (radius + 3) * (radius + 3) * a
}
func cf(radius:Double) -> Double{
return 2 * a * (radius + 3)
}
}
let minO = minOvral(radius: 3.0)
let maxO = MaxOvral()
print("圆环价格为:\((maxO.area(minO.radius) - minO.area()) * 3.5)")
print("周围栅栏价格:\(maxO.cf(minO.radius) * 1.5)")
对于两个对象我就觉得比较有难度了,当然现在解决了感觉比较简单,我没有创建对象就直接用类调用方法或者属性,开始以为创建对象只是赋值而已,如果设计到不赋值就不用创建对象,直接调用方法或者属性,在继续的观看了一下老师的代码发现对象也可以直接输入到另外一个对象的方法中,这点很重要,在以后多个对象的时候应该会占有很大的比重。记住一点对象调用方法和属性。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px 'Heiti SC Light'; color: #008400}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font: 14.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s3 {font: 14.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font: 14.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s7 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s8 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s9 {font-variant-ligatures: no-common-ligatures; color: #272ad8}
//0.发现类
//-在对问题的描述中找到名词和动词
//-名词会成为类或类中的属性,动词会成为类中的方法
//1.定义类
//-数据抽象(属性)
//-行为抽象(方法)
//-初始化方法
//访问修饰符
//-public
//-internal(内部的)-默认
//-private(私有)
class Circle {
// stored property
//-存储属性(保存和圆相关的数据)
var radius:Double
init(radius:Double){
self.radius = radius
}
//通常获得某个计算出的值的方法都可以设计成计算属性
// computational property
//计算属性(通过对存储属性做运算得到的属性)
var perimeter:Double{
//圆的周长是一个只读属性
//所以此处只有get{}没有set{}
get {return 2 * M_PI * radius}
}
var area: Double{
get {return radius * radius * M_PI}
}
}
注意类中的方法在没有参数的情况下,另一种情况
var area:Double{
get{return。。。。}
}格式
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s6 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s7 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s8 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s9 {font: 14.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s10 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s11 {font: 14.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}
init(){
answer = Int(arc4random_uniform(100)) + 1
counter = 0
hint = ""
}
//行为抽象
//判断
不用非要在init()里面写参数 可以直接赋初值
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px 'Heiti SC Light'; color: #008400}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font: 14.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font: 14.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s6 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s7 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s8 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s9 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}
import Foundation
class Point {
var x:Double
var y:Double
//被其它初始化方法所调用的初始化方法
//指派初始化方法/指派构造器
init(x:Double,y:Double){
self.x = x
self.y = y
}
convenience init(){
self.init(x:0,y:0)
}
convenience init(point:(Double,Double)){
self.init(x:point.0,y:point.1)
}
func distanceTo(otherPoint:Point) -> Double{
let a = x - otherPoint.x
let b = y - otherPoint.y
return sqrt(a * a + b * b)
}
func moveTo(x:Double,y:Double){
self.x = x
self.y = y
}
}
这是今天其中第一个一个重要的知识点,叫便利构造器convenience
如上第一个convenience就是在打酱油的调用最开始的init初始化方法,第二个convenience就是一个元组Point给x,y赋初值
有点生疏保留几位小数代码格式(如下代码)
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s5 {font-variant-ligatures: no-common-ligatures; color: #4f8187}
print(NSString(format: "%.2f", a.perimeter))
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s5 {font-variant-ligatures: no-common-ligatures; color: #272ad8}
var hp:Int{
get{
return _hp
}
set{
_hp = newValue > 0 ? newValue : 0
}
}
var mp:Int{
get{
return _mp
}
set{
_mp = newValue > 0 ? newValue : 0
}
}
init(name:String,hp:Int,mp:Int){
_name = name
_hp = hp
_mp = mp
}
第二个重用的知识点,get set方法 前者只能读取数据,后者可以改写,set用起来很方便,比如这里的血量和蓝量不能低于0就用一个三元运算来解决,漂亮优雅,高内聚,低耦合,
以下代码是产生随机变量的标准代码模式
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s5 {font-variant-ligatures: no-common-ligatures; color: #272ad8}
func randomInt(min:UInt32,max:UInt32) -> Int{
return Int(arc4random_uniform(max - min + 1) + min)
}
由UIView提供的类Canvas直接调用就是,不用再新建类了,这里就是建立对象c然后用c来发送消息
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #703daa}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}
let c = Canvas(frame:self.view.bounds)
c.backgroundColor = UIColor.clearColor()
self.view.addSubview(c)
第三个重要的知识点扩展类(extension),如果在某个特定的应用中你发现现有的某个类型少了某项功能,就可以通过这个方法来来添加这个功能。以下代码主要是在Point类中新建cgPoint这个方法来增加CGFloat单浮点这个功能。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #bb2ca2}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s3 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s4 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s5 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}
extension Point{
var cgPoint:CGPoint{
get {return CGPointMake(CGFloat(x), CGFloat(y))}
}
}
这或许是swift一大亮点。用问号实现两个不确定的值得转换,很精练,如果为空就是取默认值(即蓝色)如果不为空就去输入的那个颜色
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}
var color:UIColor?
(color ?? UIColor.blueColor()).set()
以下一大亮点就是弄了一个二维数组来实现三角形的不消失,即每产生一个一维数组pointArray然后用数组的append方法从最后的位置塞入newArray数组中,最后在用一个for循环来使二维数组中的每个值来实现draw()
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s5 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s6 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s7 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s8 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s9 {font: 14.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures; color: #008400}
class Canvas: UIView {
var pointArray:[Point] = []
var newArray:[Triangle] = []
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first{
let touchPoint = touch.locationInView(self)
pointArray.append(touchPoint.myPoint)
if pointArray.count == 3{
let t = Triangle(va: pointArray[0], vb: pointArray[1], vc: pointArray[2])
// t.color = UIColor.redColor()
t.color = UIColor.randomColor()
newArray.append(t)
setNeedsDisplay()//刷新界面
pointArray.removeAll()
}
}
}
override func drawRect(rect: CGRect) {
for t in newArray{
t.draw()
}
}
}
求最大公约数
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #bb2ca2}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}
func gcb(x:Int,_ y:Int) -> Int{
var a = x < y ? x : y
while a > 1{
if x % a == 0 && y % a == 0{
return a
}
a -= 1
}
return 1
}
以上代码虽然可以求出最大公约数 但是太low了 这里介绍一个方法(短除法)就是较大的数除以较小的数,如果除得尽较小数就是最大公约数,如果除不尽就取其余数,用较小数除以余数,如果除得尽,余数就是最大公约数,如果不,继续除直到除得尽,除得尽的那个余数为最大公约数,这里用了递归,比循环的效率也高很多,当然逼格也高很多。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s5 {font-variant-ligatures: no-common-ligatures; color: #272ad8}
func gcb(x:Int,_ y:Int) -> Int{
if x > y{
return gcb(y,x)
}
else if y % x != 0{
return gcb(y % x,x)
}
else{
return x
}
}
运算符重载(为自定义的类型定义运算符)简单的说就是个某个运算符号加个什么功能,使使用者更加容易懂。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px 'Heiti SC Light'; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #4f8187}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}span.s1 {font: 14.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s4 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font-variant-ligatures: no-common-ligatures; color: #31595d}
//运算符重载(为自定义的类型定义运算符)
func +(one:Fraction,two:Fraction) -> Fraction{
return one.add(two)
}
func -(one:Fraction,two:Fraction) -> Fraction{
return one.sub(two)
}
func *(one:Fraction,two:Fraction) -> Fraction{
return one.mul(two)
}
func /(one:Fraction,two:Fraction) -> Fraction{
return one.div(two)
}
开火车式的给予方法就是在后面.方法就行,这里我犯的一个错就是返回值没有弄成Fraction。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #4f8187}
func add(otherfraction:Fraction) -> Fraction{
return Fraction(num: _num * otherfraction._den + _den * otherfraction._num, den: _den * otherfraction._den)
}
func sub(otherfraction:Fraction) -> Fraction{
return Fraction(num: _num * otherfraction._den - _den * otherfraction._num, den: _den * otherfraction._den)
}
func mul(otherfraction:Fraction) -> Fraction{
return Fraction(num: _num * otherfraction._num , den: _den * otherfraction._den)
}
func div(otherfraction:Fraction) -> Fraction{
return Fraction(num: _num * otherfraction._den , den: _den * otherfraction._num)
}
这里有个小知识点可以屏蔽名字字符串中最后一个字
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s4 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s5 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s6 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}
let displayName = _name.substringToIndex(_name.endIndex.predecessor())
return displayName + "*"
在swift里面的值不能给空。除非自带?,这是swift一个特点。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #4f8187}span.s1 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font: 14.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s5 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s6 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}
//发牌
func deal() -> Card?{
if hasMoreCards{
return cardsArray.removeFirst()
}
return nil
}
这里有个小结,以后遇到要你打乱数组里面值得位置就用这个方法,一个循环然后给出另外一个数组里面的值,用元组进行两两交换位置,另外一个值随机变换,循环停止交换停止。这里我开始想到的用数组和字典来解决,就是用一个for循环把数组里面的值弄到字典里面去,利用字典的无序性打乱数组里面的值得位置 ,然后在利用一个for循环把字典里面的value赋给数组。但是老师说字典并不是无序的,是另外一种有序,只是不是我们正常的那种有序而已。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #4f8187}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}span.s1 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font: 14.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s4 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s5 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s6 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s7 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s8 {font-variant-ligatures: no-common-ligatures; color: #703daa}
//洗牌
func shuffle(){
reset()
for i in 0..<cardsArray.count{
let j = randomInt(0,max: UInt32(cardsArray.count - 1))
(cardsArray[i],cardsArray[j]) = (cardsArray[j],cardsArray[i])
}
}
这里是枚举 还有调用枚举的方法也是比较特殊,注意观看以下代码便是。
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}span.s1 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s5 {font: 14.0px 'Apple Color Emoji'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}
enum Suite:String{
case Spade = "♠️"
case Heart = "♥️"
case Club = "♣️"
case Diamond = "♦️"
}
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #bb2ca2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #4f8187}span.s4 {font-variant-ligatures: no-common-ligatures; color: #31595d}
let suites = [Suite.Spade,.Heart,.Club,.Diamond]
可以将子类型的对象赋值给父类型的变量(因为子类跟父类之间是IS-A关系)
如果要将父类型的变量转换成子类型需要用as运算符进行类型转换
如果能够确认父类型的变量中就是某种子类型的对象可以用as!进行转换
如果不确定父类型的变量中是哪种子类型可以用as?尝试转换
继承: 从已有的类创建新类的过程
提供继承信息的称为父类(超类/基类)
得到继承信息的称为子类(派生类/衍生类)
通常子类除了得到父类的继承信息还会增加一些自己特有的东西
所以子类的能力一定比父类更强大
继承的意义在于子类可以复用父类的代码并且增强系统现有的功能
在做面向对象的问题上,一定要事先理清思路再下手,不然越做越混乱,父类的确定,下面的属性,还有具体方法,一定要所有子类都满足,也就是所有子类取并集,而子类酒会继承父类的功能,甚至在继承父类的功能上做些改变。
// 父类有的方法子类可以重新实现 这个过程叫方法重写
// 需要在方法前添加override关键字
// 重写有时也被称为置换/覆盖/覆写
注意这里是重写,不要和重载搞混了。