Changes between Version 12 and Version 13 of ControlSystems/SoftwareTeam/Training/GettingStarted/IntroJava


Ignore:
Timestamp:
Oct 7, 2019, 11:43:49 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/Training/GettingStarted/IntroJava

    v12 v13  
    146146== Objects and Classes
    147147
    148 ...Coming soon...
     148Java is an object-oriented language; it doesn't limit you to the pre-defined types of data (integers, doubles, chars, etc.); it allows you to create new types of data.  New data types are called "classes".  A class can hold data and can contain functions (called methods) that act on that data.  Java comes with many pre-defined classes such as String and you can define new classes; for example:
     149{{{
     150    public class MyClass {
     151        int x = 5;
     152
     153        static void printX(void) {
     154            System.out.println("X="+x);
     155        }
     156    }
     157}}}
     158
     159NOTE: Class names start with an upper-case letter and must be in a file with the class name (e.g. MyClass.java)
     160
     161You can create and use new variables (objects) of the MyClass type:
     162{{{
     163   MyClass myObject;         // declare an object variable
     164
     165   myObject = new MyClass(); // create object and store in variable
     166   myObject.printX();        // invoke a method on the object
     167}}}
     168
     169=== Constructors
     170You create an object by calling a special method called the [http://tutorials.jenkov.com/java/constructors.html constructor].  In the example above, the constructor was MyClass() and is the default constructor that any class has.  You can create additional constructor methods for your classes that do things like initializing their data members or preparing hardware they are associated with for use.  For example, when creating an xbox object, you would call the constructor XboxController(0); to indicate that you want to use the first (0th) xbox controller.  Consider this example:
     171{{{
     172class Student {
     173    private String name;
     174    public int averageGrade;
     175
     176
     177    public Student(String n, int avg) {
     178        name = n;
     179        averageGrade = avg;
     180    }
     181
     182    public static void main(String[] args) {
     183        Student s = new Student("John", 9);
     184    }
     185}
     186}}}
     187
     188=== Inheritence
     189Classes can be extended so that you don't need to replicate lots of code.  This is particularly useful when there is a hierarchical relationship between classes.  For example, you can define a class Fruit and then extend it with sub-classes Orange, Apple, Banana.  Each of the sub-classes meets the "isa" test: an Apple isa Fruit.  When a class extends another class, it inherits all of the attributes and functions of the parent (super) class.  So for example consider the following classes:
     190{{{
     191   class Fruit {
     192       String color;
     193
     194       void eat(void) {
     195          ...munch munch...
     196       }
     197    }
     198
     199    class Orange extends Fruit {
     200       int numSegments;
     201    }
     202}}}
     203
     204You could create variables of these class types and use them in interesting ways:
     205{{{
     206    Fruit f;
     207    Orange o;
     208    Apple  a;
     209
     210    o = new Orange();
     211    a = new Apple();
     212    f = o;    // legit because o "isa" Fruit
     213    f = a;    // also legit because a "isa" Fruit
     214    f.eat();  // eat the apple
     215
     216    o = a;    // ERROR: a is not an Orange so can't be assigned to one
     217}}}
    149218
    150219== Princeton Java