Содержание
- 2. Objectives After completing this lesson, you should be able to: Declare, instantiate, and initialize object reference
- 3. Topics Declaring, instantiating, and initializing objects Working with object references Using the String class Using the
- 4. Working with Objects: Introduction Objects are accessed via references. Objects are instantiated versions of their class.
- 5. Accessing Objects by Using a Reference The remote is like the reference used to access the
- 6. Shirt Class public class Shirt { public int shirtID = 0; // Default ID for the
- 7. Topics Declaring, instantiating, and initializing objects Working with object references Using the String class Using the
- 8. Working with Object Reference Variables Declaration: Classname identifier; Instantiation: new Classname(); Assignment: Object reference = new
- 9. Declaring and Initializing: Example Shirt myShirt; myShirt = new Shirt(); Declare a reference for the object.
- 10. Working with Object References Shirt myShirt = new Shirt(); int shirtId = myShirt.shirtId; myShirt.display(); Declare and
- 11. Working with Object References Shirt myShirt = new Shirt(); myShirt.display(); Create a Shirt object and get
- 12. Working with Object References Camera remote1 = new Camera(); Camera remote2 = remote1; remote1.play(); remote2.stop(); remote1
- 13. References to Different Objects Television Television remote Camcorder Camcorder remote
- 14. References to Different Object Types Shirt myShirt = new Shirt(); myShirt.display(); Trousers myTrousers = new Trousers();
- 15. References and Objects In Memory 10 0x034009 0x99f311 0x034009 shirtID price colorCode shirtID price colorCode int
- 16. Assigning a Reference to Another Reference 10 0x99f311 0x99f311 shirtID price colorCode myShirt = yourShirt; counter
- 17. Two References, One Object Shirt myShirt = new Shirt(); Shirt yourShirt = new Shirt(); myShirt =
- 18. Assigning a Reference to Another Reference 10 0x99f311 0x99f311 shirtID price colorCode myShirt.colorCode = 'R'; yourShirt.colorCode
- 19. Quiz Which of the following lines of code instantiates a Boat object and assigns it to
- 20. Topics Declaring, instantiating, and initializing objects Working with object references Using the String class Using the
- 21. String Class The String class supports some non-standard syntax A String object can be instantiated without
- 22. Concatenating Strings When you use a string literal in Java code, it is instantiated and becomes
- 23. Concatenating Strings 0x034009 Hello 0x034009 String myString = "Hello"; myString
- 24. Concatenating Strings 0x99f311 0x034009 String myString = "Hello"; myString = myString.concat(" World"); myString 0x99f311 "Hello World"
- 25. Concatenating Strings 0x74cd23 0x99f311 String myString = "Hello"; myString = myString.concat(" World"); myString = myString +
- 26. String Method Calls with Primitive Return Values A method call can return a single value of
- 27. String Method Calls with Object Return Values Method calls returning objects: String greet = " HOW
- 28. Method Calls Requiring Arguments Method calls may require passing one or more arguments: Pass a primitive
- 29. Topics Declaring, instantiating, and initializing objects Working with object references Using the String class Using the
- 30. Java API Documentation Consists of a set of webpages; Lists all the classes in the API
- 31. Java Platform SE 7 Documentation You can select All Classes or a particular package here. Depending
- 32. Java Platform SE 7 Documentation Scrolling down shows more description of the String class.
- 33. Java Platform SE 7: Method Summary The type of the parameter that must be passed into
- 34. Java Platform SE 7: Method Detail Click here to get the detailed description of the method.
- 35. System.out Methods To find details for System.out.println(), consider the following: System is a class (in java.lang).
- 36. Documentation on System.out.println() The field out on System is of type PrintStream. Some of the methods
- 37. Using the print() and println() Methods The println method: System.out.println(data_to_print); Example: System.out.print("Carpe diem "); System.out.println("Seize the
- 38. Topics Declaring, instantiating, and initializing objects Working with object references Using the String class Using the
- 39. StringBuilder Class StringBuilder provides a mutable alternative to String. StringBuilder: Is a normal class. Use new
- 40. StringBuilder Advantages over String for Concatenation (or Appending) String concatenation Costly in terms of creating new
- 41. StringBuilder: Declare and Instantiate 0x034009 "Hello" 0x034009 StringBuilder mySB = new StringBuilder("Hello"); mySB
- 42. StringBuilder Append 0x034009 "Hello World" 0x034009 StringBuilder mySB = new StringBuilder("Hello"); mySB.append(" World"); mySB
- 43. Quiz Which of the following statements are true? (Choose all that apply.) The dot (.) operator
- 44. Summary Objects are accessed via references: Objects are instantiated versions of their class. Objects consist of
- 46. Скачать презентацию