简体   繁体   中英

Swift Multiple Inheritance. I am trying to recreate this Java Code in Swift related to Multiple Inheritance

public class Shape {
     public static void main (String[] args) {
    

        Circle c = new Circle(5);
        System.out.println (c.getArea());
        c.setColour("Green");
        System.out.println (c.getColour());

  
    }
}

    interface Colour {
        String getColour();
        void setColour (String colour);
    }

    abstract class Shapes implements Colour {
        abstract double getArea();
        private String colour = "Red";

    public String getColour() {
        return colour;
    }
    public void setColour(String colour) {
        this.colour = colour;
    }

}
class Circle extends Shapes {
    private int radius;

    public Circle(int radius) {
    this.radius = radius;
}



    public double getArea() {
        return Math.PI * radius * radius;
    }
}

I have attempted a Swift Solution the code is below

import Cocoa

protocol Colour {
    var colour: String { get set }

    func getColour() -> (String)
    func setColour(colour: String)

}

class Shape: Colour {
    var colour: String = "Red"

    init(colour: String) {
        self.colour = colour
    }

    func getColour() -> (String) {
        return colour
    }

    func setColour(colour: String) {
        self.colour = colour
    }

    func getArea() -> Double {
        return 0.0
    }



}

class Circle: Shape {
    let radius: Double


    init(radius: Double) {
        self.radius = radius
    }

    override func getArea() -> Double {
        return radius * radius * Double.pi
    }


   }

I have written code to create an interface 'colour', a super class 'shape' and a subclass 'Circle'. The aim is to learn more about inheritance and multiple inheritance as a new programmer. The java code works as intended.

But i am getting an error " 'super.init' isn't called on all paths before returning from initializer" with the above Swift code. As I am newer to swift and multiple inheritance I am not sure how to correct this error.

How I can correct this?

Your error is related to the initialization of superclasses. Whenever you inherit the superclass, to initialize it you must write super.init() on the first line of the Circle class's initializer.

//Inside of Circle class

 init(radius: Double) {
//I've initialized shape using the color Red, but you can use any string.
        super.init(color: "Red")
        self.radius = radius
    }

Neither Swift nor Java support multiple inheritance.

C++ does, and a few others.

In Swift you can use protocols to accomplish very similar things, but conforming to a protcol is not the same thing as inheriting from a parent class.

Your problem is that your initializer needs to call its parent class' initializer:

init(radius: Double, color: String) {
    self.radius = radius
    super.init(color: color)
}

(Circle needs to call super.init, or in this case, init(colour:) from its shape superclass.)

Note that your circle shape's initializer should probably take a color as well as a radius:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM