传递给没有参数的调用的参数// Swift

时间:2018-02-22 00:54:06

标签: swift

我目前正在研究一个专注于课程和子类的学校作业计划。

在这个节目中,有一个宗教基督徒(主要班级)有两个子班(浸信会和长老会)附属于那个主要班级。我的问题是我正在尝试按照说明创建与子类对齐的人,但我收到的错误表明:

  传递给不带参数的调用的

参数

我想知道如何解决这个问题。

此行发生错误:

var Mary: Presbyterian = Christian(modeOfBaptism: "Sprinkling")

我的代码:

import UIKit

/*
 Create a base class called Christian and initialize it with Boolean variables indicating at least two shared Christian beliefs such as the Trinity and the inspiration of Scripture and set their default values to true. Create an empty baptism method. Create a person called Peter who is a Christian. If you have assigned the default values correctly, simply assigning the class to Peter will attribute those beliefs without having to specify them at initialization time.

 Use the playground to call and display Peter's beliefs in the right column (i.e. display it without printing it to the console). */

class Christian {
    var trinity: Bool = true
    var inspirationOfScripture: Bool = true

    func baptism() {

    }
}

var peter = Christian() //<- Do I need anything else inside the parentheses?
peter.trinity

 /*Create two Christian subclasses for Baptist and Presbyterian believers and set a default string value for mode of baptism and set its default to immersion and sprinkling respectively.*/

class Baptist: Christian {
    var modeOfBaptism: String = "Immersion"
    var ageOfAccountability: Bool

    init (ageOfAccountability: Bool) {
        self.ageOfAccountability = true
    }

    override func baptism() {
        if modeOfBaptism == "Immersion".lowercased() && ageOfAccountability == true {
            print ("You will be baptized by Immersion")
        } else {
            print ("Not yet. Ask me about this later.")
        }
    }

}

class Presbyterian: Christian {
    var modeOfBaptism: String = "Sprinkling"

    override func baptism() {
        if modeOfBaptism == "Sprinkling".lowercased() {
            print ("You will be baptized by Sprinkling")
        } else {
            print ("You will not be baptized by Sprinkling")
        }
    }
}

 /*Create an age of accountability variable for the Baptist class and set up an initializer that defaults to true. This means that if we instantiate a new Baptist, he or she will inherit Christian beliefs on the Trinity, mode of baptism and will be assumed to be of the age of accountability without having do implement memberwise initialization. With an initializer for age of accountability, you can do memberwise initialization of that single variable at instantiation time.

 Override the baptism method for each denomination class that the method prints how a person would be baptized (immersion/sprinkling). In the Baptist class, create an if/else test that will only baptize if the person's age of accountability is true. If not, print something like, "Not yet. Ask me about this later."*/

 /*Create a Baptist called Billy and a Presbyterian called John. You should be able to instantiate them by using the default initial values. Display their beliefs on the Trinity (should be the same) and mode of baptism (should be different).*/

var Billy: Baptist = Baptist(ageOfAccountability: true)
Billy.trinity
Billy.modeOfBaptism

var John: Presbyterian = Presbyterian()
John.trinity
John.modeOfBaptism

 /*Baptize Billy so that when we call the method we see a sentence in the console that says something like, "You have been immersed."*/

 Christian.baptism(Billy)

 /*Create a child of Billy called Suzie and use the initializer to specify that her age of accountability is false. Try to baptize Suzie.*/

var Suzie: Baptist = Baptist(ageOfAccountability: false)
Baptist.baptism(Suzie)

 /*Make a new Presbyterian believer and call her Mary. Display Mary's belief about the mode of baptism in the right-hand column.*/

var Mary: Presbyterian = Christian(modeOfBaptism: "Sprinkling")
Mary.modeOfBaptism

 /*Now imagine that Mary joins a Reformed Baptist church (kind of a hybrid Baptist and Presbyterian church) and changes her beliefs to support immersion. Change Mary's baptism belief and display what she believes about the mode of baptism now.
 */

Mary.modeOfBaptism = "Immersion"

3 个答案:

答案 0 :(得分:1)

您的问题在于Christian.baptism(Billy)

等行

您已创建了一个类billy的{​​{1}}对象。根据您的设计,您的函数Christian不带任何参数。 (否则它将是baptism()

您应该拨打func baptism(arg1: Type1)

,而不是致电Christian.baptism(Billy)

正如另一位用户指出的那样,您应该真正利用您的类,例如billy.baptism()Christian,但这些对象的实例Presbyterian并且billy应该小写,以遵循标准的面向对象编码实践。

修改:阅读更新后,很明显您有几个问题。

john导致错误,因为Christian的构造函数没有带参数var Mary: Presbyterian = Christian(modeOfBaptism: "Sprinkling")的初始值设定项。

您还尝试将modeOfBaptism设置为Christian。请记住,根据您的子类,PresbyterianPresbyterianChristian无法强制转换为Christian对象,因为Presbyterian不一定是Christian {1}}。

我建议您阅读In-Depth Guide to Initialization,因为在进一步开发之前,您应该完全理解这些基本概念。

答案 1 :(得分:1)

从什么时候开始编程教学?

无论如何,我有一个可以选择你的任务的骨头,而且不是关于神学的。

您被指示创建BaptistPresbyterian作为Christian的子类。系统会告诉您将Mary设为Presbyterian。然后告诉你将玛丽改为改革宗浸信会。

问题在于,一旦您将Mary创建为Presbyterian,就无法在不创建新对象的情况下更改其类型:

var Mary = Presbyterian()  // Mary is forever a Presbyterian

你不能这样做:

Mary = Baptist(ageOfAccountability: false) // Mary is a Presbyterian, remember?

你只能这样做:

var Mary2 = Baptist(ageOfAccountability: false)  // this is a different Mary!

Mary2是一个新对象。如果你设置了关于玛丽长老会的属性,就像她的名字一样,当你重新创建她时,那些将会丢失。不好!<​​/ p>

可能是你的老师要你改变玛丽长老会的洗礼方式:

Mary.modeOfBaptism = "Immersion"

但这没有任何意义。现在你有一个相信Immersion的Presbyterian,这显然是非感性的,并且首先违背了定义长老会阶级的目的!

答案 2 :(得分:0)

import UIKit  
class Christian {
    var trinity: Bool = true
    var inspirationOfScripture: Bool = true

    func baptism() {

    }
}

var peter = Christian()
peter.trinity

class Baptist: Christian {
    var modeOfBaptism: String = "Immersion"
    var ageOfAccountability: Bool

    init (ageOfAccountability: Bool) {
        self.ageOfAccountability = true
    }

    override func baptism() {
        if modeOfBaptism == "Immersion".lowercased() && ageOfAccountability == true {
            print ("You will be baptized by Immersion")
        } else {
            print ("Not yet. Ask me about this later.")
        }
    }

}

class Presbyterian: Christian {
    var modeOfBaptism: String = "Sprinkling"

    override func baptism() {
        if modeOfBaptism == "Sprinkling".lowercased() {
            print ("You will be baptized by Sprinkling")
        } else {
            print ("You will not be baptized by Sprinkling")
        }
    }
}

var Billy: Baptist = Baptist(ageOfAccountability: true)
Billy.trinity
Billy.modeOfBaptism

var John: Presbyterian = Presbyterian()
John.trinity
John.modeOfBaptism

Billy.baptism()

var Suzie: Baptist = Baptist(ageOfAccountability: false)
Suzie.baptism()

var Mary: Christian = Presbyterian()
Mary.baptism()

 /*Now imagine that Mary joins a Reformed Baptist church (kind of a hybrid Baptist and Presbyterian church) and changes her beliefs to support immersion. Change Mary's baptism belief and display what she believes about the mode of baptism now.
 */

Mary = Baptist(ageOfAccountability: true)
Mary.baptism()

我已经为您重新编写了代码,现在应该可以正常工作