![]() ![]() 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. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |