Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialNuttakit Luengwitayakorn
1,626 PointsMy code doesn't work
help me please.
class Button {
var width: Double
var height: Double
init(width:Double, height:Double){
self.width = width
self.height = height
}
}
class RoundButton: Button{
var cornerRadius: Double = 5.0
}
let rounded: Button = RoundButton()
2 Answers
Michael Liendo
15,326 PointsTo answer your first question: It's due to inheritance. If you don't specify rounded
to be a subclass of Button
, then rounded
gets created without knowing anything at all about the button
class. That's means it can't have access to it's methods and properties.
As for your second question: Exactly, because the cornerRadius
instance isn't set in the init method.
Michael Liendo
15,326 PointsHello! Since your object "rounded" is of type button, it gets initialized using the same init method as a button would. Therefore, you're missing the width and height parameters.
instead of: let rounded: Button = RoundButton() it should be: let rounded: Button = RoundButton(width:someDouble, height:someDouble)
Hope that helps and upvotes are always welcome :) Happy Coding!
Ron Gootman
24,409 PointsThank you for your answer Michael!
2 questions:
why do you used "rounded: Button = RoundButton" instead of simply using "rounded = RoundButton" ?
I get why the "Button" parameters are added (width:someDouble, height:someDouble). But we also added the "cornerRadius" parameter to the RoundButton Class. So why is it not included when creating that instance? sort of RoundButton(width:someDouble, height:someDouble, cornerRadius:someDouble) Is it because the "cornerRadius" is not included in the init?