@@ -50,8 +50,51 @@ Vehicle is an abstract class (at least currently) declared with
50
50
[ open] ( https://kotlinlang.org/docs/reference/classes.html#inheritance ) to say that it's
51
51
open for inheritance.
52
52
53
- Initially, all of the methods in ` Vehicle ` were abstract.
53
+ Initially, all of the methods in ` Vehicle ` were abstract and were being implemented inside
54
+ of the base class that was inheriting it. However, after going back over
55
+ the ` open ` keyword in Kotlin, I realized you can actually implement code inside of the
56
+ abstract class and then just have the subclass call those methods with that logic. ` open `
57
+ provides the opportunity to override the method. I don't see myself changing the code
58
+ for incrementing the count of a vehicle so I put some code and logic into the ` Vehicle `
59
+ class.
54
60
55
- ## Player - inherits from Vehicle
61
+ ### companion object
62
+
63
+ I needed a static member inside of the ` Vehicle ` object, so I made a companion object which
64
+ is described inside of the [ til.md] ( https://github.com/csisl/kotlin-learning/blob/master/notes/til.md )
65
+ file in this repo. I should be able to see the available colors of a vehicle without needing
66
+ and instance of a vehicle. Inside of the companion object ` Colors ` is an array of strings,
67
+ each a possible color of the vehicle:
68
+
69
+ ```
70
+ companion object Colors {
71
+ val colors: Array<String> = arrayOf("red", "orange", "yellow", "green", "blue",
72
+ "purple", "pink", "black", "white", "silver")
73
+ }
74
+ ```
75
+
76
+ To get the colors array, I just need to call ` Vehicle.colors ` . Iterating through the array is
77
+ easy enough, following the same ` forEach ` logic metioned [ above] ( ###iterable-/-forEach ) .
78
+
79
+
80
+ ### Expression function
81
+
82
+ For functions that only return something in Kotlin, you can set that method equal to what
83
+ it should return.
84
+
85
+ ```
86
+ fun getCount(): Int = count // the same as returning count
87
+ ```
88
+
89
+ ## PunchBuggy - inherits from Vehicle
90
+
91
+ Initially, the punch buggy code had a lot of logic. There were methods that were
92
+ overriding the abstract methods that ` Vehicle ` defined. However, currently, it is
93
+ completely empty, just using the logic from the ` Vehicle ` superclass.
94
+
95
+ ## Player
96
+
97
+ A player can be made by passing a string to the constructor that is the username. Each
98
+ player has a ` muatable list ` of punch buggies and a ` score ` object. Pretty much everything
99
+ done in this class has been talked about above (mutable lists, forEach, constructors).
56
100
57
- ### Expression function
0 commit comments