Skip to content

Commit db0b787

Browse files
authored
More work on classes and functions
1 parent 151c9cc commit db0b787

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

lessons/functions-in-classes.md

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ In the example above, this line below might look a little familiar.
8282
```java
8383
Elevator elevator = new Elevator();
8484
```
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.
8787
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.
8888

89-
### Objects & Instances
89+
### Objects
9090
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.
9191
```java
9292
// Counter.java
@@ -109,6 +109,48 @@ public class Main {
109109
}
110110
}
111111
```
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.
112113

113114
## 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+
class Counter {
123+
private int count = 0;
124+
125+
public void incrementCount() {
126+
count += 1;
127+
}
128+
129+
public void decrementCount() {
130+
count -= 1;
131+
}
132+
133+
public int getCount() {
134+
return count;
135+
}
136+
}
137+
138+
// Main.java
139+
public class Main {
140+
public static void main(String[] args) {
141+
Counter counter1 = new Counter();
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.

lessons/structure.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
layout: default
3+
title: Structure
4+
---
5+
# Java Structure
6+
7+
> # WIP
8+
9+
Yap yap, packages, imports, classes, enums, robot code?

0 commit comments

Comments
 (0)