编辑代码

class Shape {
    //名称
    var name : String?
    //边数
    var sides : Int?
    //左上角的位置坐标
    var origin : CGPoint?
    //线条颜色
    var lineColor :UIColor = UIColor.red 
    //填充颜色
    var fillColor :UIColor = UIColor.green
    //线条宽度
    var lineWidth :CGFloat = 2
    //构造器函数init
    init(name : String,sides :Int,origin :CGPoint) {
        self.name = name 
        self.sides = sides 
        self.origin = origin
    }
    //自定义方法 sayHello 
    func sayhello() {
        print("Shape is \(name!),sides \(sides!), and originCord is(\(origin!.x),\(origin!.y)) ")
    }

}


//
// ViewController.swift
// ShapeDemo 
//
// Created by Zhifeng Chen on 2020/8/4.
// Copyright  2020 Zhifeng Chen. All rights reserved.
//
import UIkit
class Shape {
    //名称
    var name: String?
    //边数
    var sides : Int?
    //左上角的位置坐标
    var origin : CGPoint?
    //线条颜色
    var lineColor : UIColor = UIColor.red
    //填充颜色 
    var fillColor : UIColor = UIColor.green
    //线条宽度
    var lineWidth : CGFloat = 2
    //构造器函数init
    init(name :String, sides :Int,origin:CGPoint) {
        self.name = name 
        self.sides = sides 
        self.origin = origin
    }
    //自定义方法 
    sayhello func sayHello() {
        print("Shape is \(name!),sides \(sides!), and originCord is(\(origin!.x),\(origin!-y)) ")
    }
}
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        //此处调用Shape类,建立一个对象(实例)myShaple
        let myShape = Shape(name:"BaseShape", sides: 0,origin: CGPoint(x:0,y:0))
        myShapesayHello()
    }
}

//
// ViewController.swift
// DrawMusic 
//
// Created by Zhifeng Chen on 2020/8/4. 
// Copyright 2020 Zhifeng Chen. All rights reserved.
//
import UIKit
class Shape {
    //名称
    var name : String?
    //边数
    var sides : Int?
    //左上角的位置坐标
    var origin : CGPoint?
    //线条颜色
    var lineColor : UIColor = UIColor.red
    //填充颜色
    var fillColor : UIColor = UIColor.green
    //线条宽度
    var lineWidth : CGFloat = 2
    //构造器函数init
    init(name : String, sides : Int, origin : CGPoint) {
        self.namem = name 
        self.sides = sides 
        self.origin = origin
    }
    //自定义方法sayHello 
    func sayhello() {
        print("Shape:\(name!),sides \(sides!), origin (\(origin!.x),\(origin!.y)) ")
    }
}
class CzfView : UIView {
    //成员变量(属性)shape,其类型为Shape 
    var shape : shape?
    //重载UIView的draw方法
    override func draw(_ rect:cGRect) {
        //判断shape变量是否为空值nil 
        guard let se shape else {
            return
        }
        //不为空,则调用shape这个实例的方法 
        s.sayHello()
    }
}
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from anib.
        //此处调用Shape类,建立一个对象(实例)myShape
        let myShape = Shape(name:"BaseShape",sides:0,origin:CGPoint(x:0, y:0))
        //此处建立了一个CzfView 的实例 myview
        let width = self.view.frame.size.width 
        let height = self.view.frame.size.height
        let myView = CzfView(frame: CGRect(x: 0, y: 0, width: widtht, height: height))
        //赋值给myView中的成员变量(属性)shape 
        myView.shape = myShape
        //显示myView
        self.view.addSubview(myView)
    }
}