You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lessons/functions-in-classes.md
+46-4Lines changed: 46 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -82,11 +82,11 @@ In the example above, this line below might look a little familiar.
82
82
```java
83
83
Elevator elevator =newElevator();
84
84
```
85
-
It's creating a variable! The data type of the variable here is not one of the one's you've learned before, but instead one we defined ourselves. `Elevator` is the class that the variable is. In order to make it's value the class it expects, we have to construct a new instance of the class.
86
-
Constructing the instance is done with teh`new` word and then the class name with parentheses. What's happening here is that we are calling the class's constructor. We didn't declare a custom constructor, so it's going to use the default functionality of purely creating a new instance of the class.
85
+
It's creating a variable! The data type of the variable here is not one of the one's you've learned before, but instead one we defined ourselves. `Elevator` is the class that the variable is. In order to make it's value the class it expects, we have to construct a new instance of the class, aka a object.
86
+
Constructing the object is done with the`new` word and then the class name with parentheses. What's happening here is that we are calling the class's constructor. We didn't declare a custom constructor, so it's going to use the default functionality of purely creating a new instance of the class.
87
87
You may also notice that there are 2 other classes that we construct: `DigitalInput` and `Encoder`. We didn't create those classes, however, they are provided to us via libraries. We'll discuss libraries later. In their constructing, you can see them accepting arguments.
88
88
89
-
### Objects & Instances
89
+
### Objects
90
90
When one constructs a class, they are merely creating a new instance of the class. Let's look at another example to see what I mean.
91
91
```java
92
92
// Counter.java
@@ -109,6 +109,48 @@ public class Main {
109
109
}
110
110
}
111
111
```
112
+
The `counter1` and `counter2` objects are diffrent instances of the counter class, and they each have their own memebers and functions. Although they are the same internally, the values of the variables within them are diffrent.
112
113
113
114
## Le Dot
114
-
Explain what the dot does, or smthn idk. I gotta keep workin on this.
115
+
We've been using this syntax in the examples here but I just haven't been saying anything about it yet.
116
+
117
+
The dot lets us get memebers and functions in objects. For example, we can access the `count` member (aka variable) of the `counter1` object by doing: `counter1.count`. We can then use it like any other variable, assigning values or reading its value. However this only works if the memeber was set to `public`. In cases where we don't want this functionality, we would mark it as `private`.
118
+
You've also seen it when you do `System.out.println()`, you are calling the `println` function on the `out` object inside of the `System` object. We can make our own functions like so.
119
+
120
+
```java
121
+
// Counter.java
122
+
classCounter {
123
+
privateint count =0;
124
+
125
+
publicvoidincrementCount() {
126
+
count +=1;
127
+
}
128
+
129
+
publicvoiddecrementCount() {
130
+
count -=1;
131
+
}
132
+
133
+
publicintgetCount() {
134
+
return count;
135
+
}
136
+
}
137
+
138
+
// Main.java
139
+
publicclassMain {
140
+
publicstaticvoidmain(String[] args) {
141
+
Counter counter1 =newCounter();
142
+
143
+
counter1.increment();
144
+
System.out.println(counter1.getCount()); // 1
145
+
146
+
counter1.decrement();
147
+
System.out.println(counter1.getCount()); // 0
148
+
149
+
counter1.decrement();
150
+
System.out.println(counter1.getCount()); // -1
151
+
}
152
+
}
153
+
```
154
+
Although it might seem like having memebers like `count` private just makes the code more complicated, I'd argue that it helps to protect features from unintended minipulation, particularly when we get to state machines.
155
+
156
+
The next lesson will go into some details about how java strucutres things and we'll look at code that might resemble what our robots code might look like.
0 commit comments