p.288 Figure 8.2 The GUI objects are created from classes.
p.288 Figure 8.2 A class is a template(contract, blueprint) for creating objects
p.289 Figure 8.3 A class is a construct that defines objects of the same type
It does not have a main method and therefore cannot be runp.2 Figure 8.4 Classes and objects can be represented using UML notations
────────────────────────────────── Class name ─────────────☼───────────────────── Data fields ────────────────────────────────── Constructors and methods ──────────────────────────────────constructor 建構子
ClassName(parameterName: parameterType)method 方法
methodName(parameterName: parameterType): returnType
參考 p.290 程式 Listing 8.1 TestCircle1.java
參考 p.291 程式 Listing 8.2 Circle1.java
Circle1 myCircle = new Circle1(5.0);一般而言,class檔案會分散在不同的檔案內,這時候需要考慮 package的觀念(以後解說)與修飾詞
p.292 Figure 8.5 The TV class models TV sets
參考 p.292 程式 Listing 8.3 TV.java
參考 p.293 程式 Listing 8.4 TestTV.java
// 宣告物件變數 ClassName objectRefVar; Circle myCircle; // 產生真正的物件,並用 myCircle 指到(reference) 到物件所在(in heap) myCircle = new Circle(); // 宣告與產生物件,合併為1個步驟 ClassName objectRefVar = new ClassName(); Circle myCircle = new Circle();
p.295 Note. An object reference variable and object ( 重要觀念 參考 Figure 8.6, Figure 8.8)
p.295 Note. Arrays are treated as objects in Java, created using the new operator
// 範例 objectRefVar.dataField myCircle.radius objectRefVar.method(arguments) myCircle.getArea()
p.296 Caution. 可以使用 Math.pow(3, 2.5) static vs. 不可以使用 Circle.getArea() NOT static
p.296 Note. create an object without explicitly assigning it to a variable new Circle()
class Student { String name; // name has default value null int age; // age has default value 0 boolean isScienceMajor; // isScienceMajor has default value false char gender; // c has default value '\u0000' }
null is a literal for a reference type, null表示變數沒有指到任何物件
class Test { public static void main(String[] args) { Student student = new Student(); System.out.println("name? " + student.name); System.out.println("age? " + student.age); System.out.println("isScienceMajor? " + student.isScienceMajor); System.out.println("gender? " + student.gender); } }Method 內的區域變數(Local Variable),並不會自動給予初值(initial value)
class Test { public static void main(String[] args) { int x; // x has no default value String y; // y has no default value System.out.println("x is " + x); System.out.println("y is " + y); } }
p.297 Caution. NullPointerException
Primitive type int i = 1 Object type Circle c = new Circle()
重要 p.297 Figure 8.6 variable of a primitive type vs variable of a reference type holds a reference
Primitive type assignment i = j
重要 p.297 Figure 8.7 Primitive variable j is copied to variable i.
Object type assignment c1 = c2
重要 p.298 Figure 8.8 Reference variable c2 is copied to variable c1.
p.298 Note. The object previously referenced by c1 is no longer useful and therefore is now known as garbage, garbage collection
p.298 Tip. assign null to a reference variable for the object, 表示物件不會用到了,Java VM 執行 garbage collection 時,會將該物件所佔用的記憶體回收,提供以後 需要配置記憶體時 使用
p.298 Figure 8.9 A Date object represents a specific date and time.
p.299 Figure 8.10 A Random object can be used to generate random values.
// 產生 [0 .. n) 之間的整數 nextInt(n: int)
參考 p.300 程式 Listing 8.5 TestFrame.java
參考 p.300 程式 Listing 8.6 GUIComponents.java
重要 p.302 Figure 8.12 Instance variables and Static variables
static int numberOfObjects; static int getNumberObjects() { return numberOfObjects; } final static double PI = 3.14159265358979323846;
參考 p.303 程式 Listing 8.7 Circle2.java
參考 p.303 程式 Listing 8.8 TestCircle2.java
class Circle2 { double radius; static int numberOfObjects = 0; // The number of the objects created
p.304 Tip. use class name to invoke a static method and access a static variable
ClassName.staticMethodName(arguments) ClassName.staticVariable
public class Foo { int i = 5; static int k = 2; public static void main(String[] args) { int j = i;// Wrong because i is an instance variable m1(); // Wrong because m1() is an instance method } public void m1() { // Correct since instance and static variables and methods // can be used in an instance method i = i + k + m2(i, k); } public static int m2(int i, int j) { return (int)(Math.pow(i, j)); } }
public class Foo { int i = 5; static int k = 2; public static void main(String[] args) { Foo foo = new Foo(); int j = foo.i;// foo.m1(); // } public void m1() { // Correct since instance and static variables and methods // can be used in an instance method i = i + k + m2(i, k); } public static int m2(int i, int j) { return (int)(Math.pow(i, j)); } }
p.306 Note. package packageName;
Visibility Modifierspackage packageName; // packageName 通常為網址倒過來 例如 tw.edu.scu.csim.hello
重要 p.306 Figure 8.13 default and private, public modifier
package p1; public class C1 { public int x; int y; private int z; public void m1() { } void m2() {} private void m3() {} }
package p1; public class C2 { void aMethod() { C1 o = new C1(); can access o.x; can access o.y; cannot access o.z; can invoke o.m1(); can invoke o.m2(); cannot invoke o.m3(); } }
package p2; public class C3 { void aMethod() { C1 o = new C1(); can access o.x; cannot access o.y; cannot access o.z; can invoke o.m1(); cannot invoke o.m2(); cannot invoke o.m3(); } }
重要 p.306 Figure 8.14 nonpublic class has package-access
package p1; class C1 { ... }
package p1; public class C2 { can access C1 }
package p2; public class C3 { cannot access C1; can access C2; }
重要 p.307 Figure 8.15 private members
p.307 Caution. public and private on local variables ERROR
p.307 Note. private constructor
What happen if public member ? using the private modifier. This is known as data field encapsulationp.208 Note. getter(or accessor) and setter (or mutator).
getterpublic returnType getPropertyName()If the returnType is boolean, getter
public boolean isPropertyName()setter
public void setPropertyName(dataType propertyValue)
p.308 Figure 8.16 The Circle class encapsulates circle properties and provides get/set and other methods
參考 p.308 程式 Listing 8.9 Circle3.java
參考 p.309 程式 Listing 8.10 TestCircle3.java
參考 p.310 程式 Listing 8.11 TestPassObject.java
p.311 Figure 8.17 n is passed to times, and the reference of myCircle is passed to c in the printAreas method.
passing methodsCircle[] circleArray = new Circle[10];To initialize the circleArray, you can use a for loop like this one:
for (int i = 0; i < circleArray.length; i++) { circleArray[i] = new Circle(); }An array of objects is actually an array of reference variables.
重要 p.312 Figure 8.18 In an array of objects, an element of the array contains a reference to an object
p.312 Note. When an array of objects is created using the new operator, each element in the array is a reference variable with a default value of null.
參考 p.312 程式 Listing 8.12 TotalArea.java