Beyond the Single main() Method } Many classes can be written using only one method: main(), containing all of the code } Many classes will have more than one method } Some classes we write won’t have a main() method } Much code you have written includes more than method } You might only write a single main() method, but: Your code may use classes written by the instructor, like Oval , 1. Rectangle , etc., that don’t have their own main() methods Class #25: Your code may use built-in classes, like String , Scanner , etc., Writing Multiple Methods 2. that also don’t have their own main() methods } In each such case, you have used many other methods, like Software Design I (CS 120): D. Mathias setLocation() or length() } Now it’s time to create/write your own methods to do things 2 Software Design I (CS 120) Method Syntax Multiple Methods in a Class } Methods are placed inside of classes } We can write as many methods in a class as we like They are not placed inside of other methods: each is a separate code block } } Each will start with its own method declaration, and will Each can have its own variable declarations, with self-contained scope , which } contain a block of valid code means that the variables are not visible outside of the method } Each must be complete before another method starts May be public May have a non-void May have one or more instead return type (e.g., int) input parameters public class MethodRunner { public void sayHello() { System.out.println( "Hello!" ); } private void sayGoodbye() { System.out.println( "Goodbye!" ); } } 3 4 Software Design I (CS 120) Software Design I (CS 120) 1
private vs. public Methods private vs. public Method Access } Methods can be either public or private } If we make a method in a class C private , it can only be called from within the class C itself } This does not affect compiling the code at all } This does not affect what the code actually does at all } If we make a method in C public , it can be called: } Instead, it affects where we can legally access (i.e., call ) it from within the class C itself 1. public class MethodRunner by any other object of type C (created using the C() 2. { constructor) public void sayHello() A method with { public access System.out.println( "Hello!" ); } private void sayGoodbye() A method with { private access System.out.println( "Goodbye!" ); } } 5 6 Software Design I (CS 120) Software Design I (CS 120) public vs. private Method Access public vs. private Method Access } In this example, the public } A private method can’t be public class MethodRunner public class MethodRunner { { method can be called in any called in any other class, even if public void sayHello() public void sayHello() { { other class in the usual way: we create objects and try // code here // code here } } Instantiate object of class type 1. private void sayGoodbye() private void sayGoodbye() Call method we wish to use { { 2. // code here // code here } } } } sayGoodbye() is Since sayHello() public class Foo public class Bar private, and so we is public, we can { { can’t call it here public static void main( String[] args ) public static void main( String[] args ) call it here in { { the separate Foo MethodRunner runner = new MethodRunner(); MethodRunner runner = new MethodRunner(); This code results class runner.sayHello(); runner.sayGoodbye(); // ERROR!! in a compile error } } } } 7 8 Software Design I (CS 120) Software Design I (CS 120) 2
public vs. private Method Access Exercise } Inside a single class, it doesn’t } Download the code example for Class 25. public class MethodRunner matter whether methods are { } Open the code as Non BlueJ. public or private public void sayHello() { } Any other code can call every } Create a new Java class called MethodRunner3 // code here method in the same class } } Using MethodRunner as a template, write methods in } Since we are inside the same class, we don’t need an object your new class. It must include at least two methods, private void sayGoodbye() to call the method { each of which prints something to the screen. One } The compiler/JVM // code here should be public and the other private. } automatically looks for any methods we call this way } In Main.java, create a MethodRunner3 object and call your public void sayBoth() inside the class itself { methods. Observe what happens. sayHello(); sayGoodbye(); Here, we can directly } call both of the other } methods 9 10 Software Design I (CS 120) Software Design I (CS 120) Constructors Using Constructors } How do you call a DrawingGizmo } Oval o = new Oval(); constructor? Constructor } You’ve already done so } Random rand = new Random(); many times! <<constructor>> DrawingGizmo() } Use new } Constructor method } Window win = new Window(); <<update>> } Used to create an object void moveForward() These are calls to the } Has same name as class void turnClockwise() } String s = new String(); constructors for the void turnCounterclockwise() } Has no return type – this is Oval, Random, Window, void dontDraw() NOT the same as void } Car Giulia = new Car(); void draw() String, and Car classes. void delay2Sec() 11 12 Software Design I (CS 120) Software Design I (CS 120) 3
Multiple Constructors Multiple Constructors } Can have multiple DrawingGizmo constructors in a class: Car Giulia = new Car(); } Must have different Calls a constructor that takes no parameters. parameter lists <<constructor>> DrawingGizmo() } Java “knows” which one to DrawingGizmo(Color, Color) use based on the parameter list you provide <<update>> Car MC40 = new Car(“Mini”, “Cooper S”, 2004, in the call. void moveForward() void turnClockwise() 25000, 180, 190); void turnCounterclockwise() void dontDraw() Calls a constructor that takes six parameters: void draw() String, String, int, int, int, int void delay2Sec() 13 14 Software Design I (CS 120) Software Design I (CS 120) Multiple Constructors A constructor that takes no parameters. Car(){ … } A constructor that takes six parameters: String, String, int, int, int, int Car(String, String, int, int, int, int){ … } 15 Software Design I (CS 120) 4
Recommend
More recommend