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 trial
Angjelin Malaj
5,983 PointsWrite a class that describe your favorite thing. It could be anything.
Your class must contain at least 2 private variables , a constructor that prints out " I am a constructor" and 1 method that prints out " I am a method"
I wrote a long code but I would like to know a short code which is more specific than what I did.
3 Answers
Angjelin Malaj
5,983 PointsThank you JCorum and foxyrev91!
foxyrev91
3,912 Pointspublic class Holiday {
private String destination;
private int daysDuration;
public Holiday(String destination, int daysDuration) {
this.destination = destination;
this.daysDuration = daysDuration;
System.out.println("I am a constructor");
}
public void exampleMethod() {
System.out.println("I am a method");
}
}
jcorum
71,830 PointsHere's an example for you:
class IceCreamCone {
var flavor: String
var toppings: [String]
init(flavor: String, toppings: [String]) {
self.flavor = flavor
self.toppings = toppings
print("I am a constructor")
}
func description() {
print("I am a method")
}
}
To create an IceCreamCone object you would do this:
let myCone = IceCreamCone(flavor: "raspberry", toppings: ["fudge", "marshmellow whip"])