When Kotlin finds a comment like this, it will ignore the rest of the line, so it has no effect on how the program runs. This is the first time in this series that we’ve used a comment in our code.Ī section of text that starts with two forward slashes // is a comment. Using our Circle class as a function parameter By the way. An Empty Classįirst, let’s define an empty class - with no variables and no functions. Instead of jumping into all of this at once, let’s build up this class slowly… one step at a time… and look carefully at each part. Let’s create our very first class, a Circle, which will include these variables and functions: ![]() In Kotlin, we can put related variables and functions together using a class, which is a feature that we will use to create our new Circle type. So instead of the scrambled variables and functions above, it’ll look more like this: This way, instead of having separate variables and functions that hold a radius and a circumference, we can have a single variable that represents the circle itself. So in this chapter, we’ll create a new type called a Circle. And once we start introducing other shapes, like rectangles and triangles, it becomes even harder to keep things straight - for example, which functions give us the area of a circle, and which give us the area of a rectangle? This was manageable for such a simple example, but as we come up with more and more things that we want to know about the circle, such as its diameter, area, or position, it can become quite difficult to manage all of those different variables and functions. ![]() It all looked kind of scrambled, like this: And in chapter 2, we created a function to calculate the circumference from the radius. ![]() Putting Variables and Functions TogetherĪs you might recall, we started off this series creating variables to hold things pertaining to a circle, such as its radius and circumference. In this chapter, we’re going to start creating our very own types. Way back in chapter 1, we learned about a variety of types in Kotlin, such as Double, String, and Boolean.
0 Comments
Leave a Reply. |