Spaces:
Running
Running
class, constructor
Browse files
Week 4: Writing classes/03A Writing a class: file contents
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
A class is defined by the class identifier.
|
| 2 |
+
As stated earlier, all code in Java must be written inside classes.
|
| 3 |
+
However, not all the code that is written is directed at the objects that are created from the class.
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
In Java, the class name must be exactly the same as the file name.
|
| 7 |
+
Specifically, a file must have a public class defined by its name.
|
| 8 |
+
|
| 9 |
+
For example, the file Car.java must have a class definition of
|
| 10 |
+
public class Car
|
| 11 |
+
|
| 12 |
+
...and from the file OwnFineClass.java definition
|
| 13 |
+
public class OwnFineClass
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
You can write MORE THAN 1 CLASS in the SAME FILE, but ONLY 1 can be PUBLIC.
|
| 18 |
+
The others are initialized without the public attribute.
|
| 19 |
+
|
| 20 |
+
A class has different features. Features can be public or private.
|
| 21 |
+
When an object is created from a class, the client of the object has direct access to the public features.
|
| 22 |
+
The client in this context is the program code through which the entity is accessed
|
| 23 |
+
(it does not mean, for example, the program user or any other physical person).
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
====================================================================================================================================================
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
Attributes
|
| 38 |
+
Let's start by looking at the attributes of the class (note the two t's).
|
| 39 |
+
An attribute is a variable of an object.
|
| 40 |
+
They are therefore used to define the information content of the objects that make up the class.
|
| 41 |
+
Each object in a class has its own values for all the attributes defined in the class.
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
As an example, consider a class that models a student.
|
| 46 |
+
The class has three attributes: name, student number and credits:
|
| 47 |
+
|
| 48 |
+
public class Student {
|
| 49 |
+
// Attributes are usually defined at the start of the class
|
| 50 |
+
String name;
|
| 51 |
+
String studentId;
|
| 52 |
+
int studyPoints;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
ATTRIBUTES are thus defined inside the class, but OUTSIDE ANY METHODS.
|
| 59 |
+
Typically, attribute variables are assigned values in the CONSTRUCTOR based on PARAMETERS YOU PASS IN.
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
So let's write a constructor for the Student class:
|
| 65 |
+
public class Student {
|
| 66 |
+
// Attributes are usually defined at the start of the class
|
| 67 |
+
String name;
|
| 68 |
+
String studentId;
|
| 69 |
+
int studyPoints;
|
| 70 |
+
|
| 71 |
+
// Constructor
|
| 72 |
+
public Student(String n, String id, int sp) {
|
| 73 |
+
name = n;
|
| 74 |
+
studentId = id;
|
| 75 |
+
studyPoints = sp;
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
Now new student objects can be created using the new operator:
|
| 83 |
+
public class Testclass {
|
| 84 |
+
public static void main(String[] args) {
|
| 85 |
+
Student sally = new Student("Sally Student", "12345", 10);
|
| 86 |
+
Student sam = new Student("Sam Student", "99999", 35);
|
| 87 |
+
}
|
| 88 |
+
}
|
| 89 |
+
|