|
| 1 | +# Classes |
| 2 | + |
| 3 | +### Object Oriented Programming |
| 4 | + |
| 5 | +Object-Oriented Programming (OOP) is a programming paradigm based on the concept of classes and objects. It provides a structured way to organize code, making it more modular, reusable, and easier to manage. OOP helps reduce errors by encapsulating data and behavior within classes, reusing code, and minimizing duplication. |
| 6 | + |
| 7 | +In the context of FTC, you might use OOP to create a custom DcMotor class that extends or enhances the built-in DcMotor functionality, allowing for additional features such as preset motor speeds, automated braking, or advanced control algorithms. |
| 8 | + |
| 9 | +For a generic example, if you had a Person class with two parameters and variables, name and age. If you wanted to do this exact thing, you would create the Person class, and then access that property later in the program. Example below |
| 10 | + |
| 11 | +```java |
| 12 | +class Person { |
| 13 | + String name; |
| 14 | + int age; |
| 15 | + |
| 16 | + // constructor |
| 17 | + public Person(String name, int age) { |
| 18 | + this.name = name; |
| 19 | + this.age = age; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// initialize the class |
| 24 | +Person bob = new Person("bob", 29); |
| 25 | +// access bob's name |
| 26 | +bob.name; |
| 27 | +// access his age |
| 28 | +bob.age; |
| 29 | +``` |
| 30 | + |
| 31 | +### State Based Class |
| 32 | + |
| 33 | +See [this](subsystem-control.md) |
| 34 | + |
| 35 | +State based classes and subsystems are a little different from the base level subsystem like above. These are commonly used for servos or other simple classes that have foreseeable states. For instance, my claw subsystem class, included completely below, uses states for the claws and for the flip servo. Then, when it calls the update function, it will move the servos to the correct position based on the state it is in. |
| 36 | + |
| 37 | +Unfortunately, you will not be able to use this code but use it as an **example** |
| 38 | + |
| 39 | +The parts of this code include: |
| 40 | + |
| 41 | +The enums, which control and operate the states |
| 42 | + |
| 43 | +Variables that contain the states |
| 44 | + |
| 45 | +The constructor (including my own initialize function) |
| 46 | + |
| 47 | +Functions that set the variables correctly (these are needed because it could be confusing to just set the variable differently in the program) |
| 48 | + |
| 49 | +The update function which will use a switch statement to control the claw and flip servo using my own commands |
| 50 | + |
| 51 | +### Private Variables/Functions |
| 52 | + |
| 53 | +Private variables are variables that can only be used inside the class they are defined within. This allows for more code security so you don't accidentally mess something up. For instance, if you have a class that is for calculating ticks per inch, you don't want to accidentally change the ticks per inch variable in the main program and instead make that var private. |
| 54 | + |
| 55 | +As for public variables, these are variables that can be used outside the class they are defined within. This allows for more flexibility in the code, but also makes it easier to mess up. For instance, if you have a class that is for calculating ticks per inch, you want to be able to access the variables used for calculating ticks per inch in the main program. |
| 56 | + |
| 57 | +### Static Variables/Functions |
| 58 | + |
| 59 | +Static variables are variables that can be shared and used in different classes at any time. A very good example of when you would use this is if you are using a class to store positions for a servo, you would want `static` variables for the positions so you can access them in the main program. This is also true for functions, if you have a function that is used in multiple classes, you would want to make it static so you can access it in any other program you want to. |
| 60 | + |
| 61 | +When using FTC Dash, the variables must be static, or they will not be able to be accessed in the dashboard. |
| 62 | + |
| 63 | +### Class Functions |
| 64 | + |
| 65 | +Functions inside classes are one of the most important parts of the class. These control the overall function of the class and allow you to call methods using the class. |
| 66 | + |
| 67 | +Example: |
| 68 | + |
| 69 | +```java |
| 70 | +class Person { |
| 71 | + String name; |
| 72 | + int age; |
| 73 | + |
| 74 | + // constructor |
| 75 | + public Person(String name, int age) { |
| 76 | + this.name = name; |
| 77 | + this.age = age; |
| 78 | + } |
| 79 | + |
| 80 | + // function to get the name |
| 81 | + public String getName() { |
| 82 | + return name; |
| 83 | + } |
| 84 | + |
| 85 | + // function to get the age |
| 86 | + public int getAge() { |
| 87 | + return age; |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +In this example, getName() returns a String of name, and getAge() returns an int of age. In this way, you could also have `void` functions that do not return anything, but instead do something behind the scenes. |
| 93 | + |
| 94 | +### Class Inheritance |
| 95 | + |
| 96 | +Inheritance is a way to create a new class based on an existing class. The new class inherits the properties and methods of the existing class, allowing for code reuse and organization. Inheritance is a key feature of object-oriented programming (OOP) and helps create a hierarchical structure of classes. |
| 97 | + |
| 98 | +In the context of FTC, you might create a base class for a robot, and an inherited class for the autonomous part of it. |
| 99 | + |
| 100 | +For example, you could create a base class called `Robot` that contains common properties and methods for all robots, such as `drive()`, `turn()`, and `stop()`. Then, you could create a subclass called `AutonomousRobot` that inherits from the `Robot` class and adds specific functionality for autonomous operations, such as `followLine()` or `avoidObstacle()`. Inheritance is denoted by the `extends` keyword. |
| 101 | + |
| 102 | +### Class Extension |
| 103 | + |
| 104 | +Class extension is a way to use all functions of one class in another class. This is useful when you are creating complex system architectures and want to use similar functions in multiple classes. This is approximately the same as class inheritance, but is more commonly used in FTC. |
| 105 | + |
| 106 | +Specifically in FTC, every OpMode must `extends OpMode or LinearOpMode` (preference to LinearOpMode). This allows you to use all the functions of the OpMode class in your own OpMode. This is required because the OpMode class contains all the functions needed to run the robot. |
| 107 | + |
| 108 | +### Class Initialization |
| 109 | + |
| 110 | +Class initialization allows you to create a new instance of the class. This is done using the `new` keyword. For example, if you have a class called `Person`, you can create a new instance of the class using `Person bob = new Person("bob", 29);`. This creates a new instance of the `Person` class and assigns it to the variable `bob`. |
| 111 | + |
| 112 | +You need to make sure that the class is initialized before you use it and that you are using the correct class instance when using multiple class instances. |
| 113 | + |
| 114 | +### Class Variables |
| 115 | + |
| 116 | +Class variables are variables that are shared in the instance of the class. This is most useful when you are using classes that contain HardwareDevices. It allows you to call specific motors or servos inside the class without having to create a new instance of the class. |
| 117 | + |
| 118 | +### Abstract Classes |
| 119 | + |
| 120 | +Abstract classes are classes that cannot be instantiated. This means that you cannot create a new instance of the class. This is useful when you want to create a base class that contains common properties and methods, but you do not want to create an instance of the class. Instead, you create a subclass that inherits from the abstract class and implements the abstract methods. |
| 121 | + |
| 122 | +For instance, if you have a class called `Person`, you could create an abstract class called `AbstractPerson` that contains common properties and methods, but cannot be instantiated. Then, you could create a subclass called `Student` that inherits from the `AbstractPerson` class and implements the abstract methods such as `eat` or `sleep`. |
| 123 | + |
| 124 | +#### Overriding Classes/Functions |
| 125 | + |
| 126 | +When you create an abstract class, the functions must be overridden in the subclass. This means that you must create a new implementation of the function in the subclass. This is useful when you want to create a base class that contains common properties and methods, but you want to create a new implementation of the function in the subclass. |
| 127 | + |
| 128 | +For instance, if you had a robot class, you could create a subclass called `AutonomousRobot` that overrides the `drive()` function to create a new (autonomous) implementation of the same function. |
| 129 | + |
| 130 | +### Class Example |
| 131 | + |
| 132 | +```java |
| 133 | +abstract class Person{ |
| 134 | + String name; |
| 135 | + int age; |
| 136 | + |
| 137 | + // constructor |
| 138 | + public Person(String name, int age) { |
| 139 | + this.name = name; |
| 140 | + this.age = age; |
| 141 | + } |
| 142 | + |
| 143 | + // abstract function |
| 144 | + public abstract void eat(); |
| 145 | + public abstract void sleep(); |
| 146 | + public abstract void calculateAge(); |
| 147 | +} |
| 148 | + |
| 149 | +class Student extends Person { |
| 150 | + String school; |
| 151 | + |
| 152 | + // Constructor |
| 153 | + public Student(String name, int age, String school) { |
| 154 | + super(name, age); // Calls the superclass (Person) constructor |
| 155 | + this.school = school; |
| 156 | + } |
| 157 | + |
| 158 | + // Implementing abstract methods |
| 159 | + @Override |
| 160 | + public void eat() { |
| 161 | + System.out.println(name + " is eating in the cafeteria."); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public void sleep() { |
| 166 | + System.out.println(name + " is sleeping after studying."); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void calculateAge() { |
| 171 | + System.out.println(name + " is " + age + " years old."); |
| 172 | + } |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +FTC example [here](external-hardware-classes.md) |
0 commit comments