传递的参数不带任何参数// swift

  • 本文关键字:参数 任何 swift 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"

您的问题与Christian.baptism(Billy)

之类的行

您创建了一个Christian类的对象billy。您的函数baptism()不用您的设计而没有参数。(否则将是func baptism(arg1: Type1)

而不是致电Christian.baptism(Billy),您应该调用billy.baptism()

正如另一个用户指出的那样,您应该真正使您的类资本化,例如ChristianPresbyterian,但是这些对象的实例billyjohn应该是遵循标准面向对象的编码实践的小写。

编辑:阅读您的更新后,很明显您有几个问题。

var Mary: Presbyterian = Christian(modeOfBaptism: "Sprinkling")正在造成错误,因为您的基督徒的构造函数没有初始化器来参数modeOfBaptism

您还试图将Christian设置为Presbyterian。请记住,基于您的子类别,PresbyterianChristian,但Christian不能被强加于Presbyterian对象,因为Christian不一定是Presbyterian

我建议通过深入的初始化指南阅读,因为这些是您在进一步开发之前应该充分理解的一些基本概念。

因为神学院在教授编程何时?

无论如何,我有一个骨头可以挑选您的作业,这与神学无关。

您被指示将BaptistPresbyterian作为Christian的子类创建。有人告诉您将玛丽创建为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!

玛丽2是一个新对象。如果您像她的名字一样设置了关于长老会玛丽的财产,那么当您重新创建她时,这些财产将会丢失。不好!

可能是您的老师希望您改变长老会的洗礼方式:

Mary.modeOfBaptism = "Immersion"

,但这没有道理。现在,您有一个相信沉浸式的长老会,这显然是非敏感的,并且首先击败了定义长老会的目的!

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()

我已经为您重写了您的代码,现在应该正常工作

相关内容

最新更新