1 / 58

Calling Methods in Java

Learn how to declare and call methods in Java, including changing variable values and object references. See examples of method signatures and arrays.

nicka
Download Presentation

Calling Methods in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 4 Using Methods

  2. CCE JMU CHINA

  3. CCE JMU CHINA

  4. CCE JMU CHINA

  5. CCE JMU CHINA

  6. CCE JMU CHINA

  7. Calling Method CCE JMU CHINA

  8. Worker Method CCE JMU CHINA

  9. Declaring Methods • Worker and calling methods have the same syntax structure. CCE JMU CHINA

  10. CCE JMU CHINA

  11. CCE JMU CHINA

  12. CCE JMU CHINA

  13. CCE JMU CHINA

  14. CCE JMU CHINA

  15. CCE JMU CHINA

  16. CCE JMU CHINA

  17. CCE JMU CHINA

  18. **参见程序运行 CCE JMU CHINA

  19. PassTest.java • public class PassTest { • // changeInt(int)方法用于改变int数据值 • public static void changeInt(int value) { • value = 55; • } CCE JMU CHINA

  20. // changeObjectRef(MyDate)方法用于改变MyDate引用变量 public static void changeObjectRef(MyDate ref) { ref = new MyDate(10, 10, 2008); // changeObjectAttr(MyDate) 方法用于改变引用对象的变量 CCE JMU CHINA

  21. public static void changeObjectAttr(MyDate ref) { • ref.setDay(17); • } • } • public static void main(String args[]) { • MyDate date; • int val; • // 赋int val变量值11 CCE JMU CHINA

  22. val = 11; • // 单向传递val值,调用changeInt(int)方法 • changeInt(val); • // 观测val的当前值 CCE JMU CHINA

  23. System.out.println("Int value is: " + val); • // 为date引用赋值,date引用对象MyDate(7, 7, 2007) • date = new MyDate(7, 7, 2007); • //单向传递date值,调用changeObjectRef(MyDate)方法 • changeObjectRef(date); CCE JMU CHINA

  24. // 观测date引用值 • date.print(); • // 调用changeObjectAttr(MyDate)方法, • //通过date改变所引用对象的变量 • // 观测date引用值 • date.print(); • } • } CCE JMU CHINA

  25. 运行结果: ---------- java ---------- • Int value is: 11 • MyDate: 7-7-2007 • MyDate: 17-7-2007 • Normal Termination • Output completed (0 sec consumed). CCE JMU CHINA

  26. CCE JMU CHINA

  27. CCE JMU CHINA

  28. **参见程序运行 CCE JMU CHINA

  29. CCE JMU CHINA

  30. Method Signature • method signature is unique • method name +argument list CCE JMU CHINA

  31. CCE JMU CHINA

  32. CCE JMU CHINA

  33. CCE JMU CHINA

  34. CCE JMU CHINA

  35. CCE JMU CHINA

  36. CCE JMU CHINA

  37. CCE JMU CHINA

  38. CCE JMU CHINA

  39. CCE JMU CHINA

  40. CCE JMU CHINA

  41. CCE JMU CHINA

  42. CCE JMU CHINA

  43. CCE JMU CHINA

  44. CCE JMU CHINA

  45. CCE JMU CHINA

  46. CCE JMU CHINA

  47. Array

  48. CCE JMU CHINA

  49. public class Point { private int x; private int y; public String toString() { return ("[" + x + "," + y + "]"); } } CCE JMU CHINA

  50. CCE JMU CHINA

More Related