1 / 33

The Class as a Container

The Class as a Container. Possibly a Meta-Container. Objective. Provide an overview to help you read code in the book Not enough depth to author programs We will start with the details next week. new. "Class" Everybody Should Have Some . Foo.java. Foo.class. public class Foo {

trudy
Télécharger la présentation

The Class as a Container

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. The Class as a Container Possibly a Meta-Container

  2. Objective • Provide an overview to help you read code in the book • Not enough depth to author programs • We will start with the details next week

  3. new "Class" Everybody Should Have Some Foo.java Foo.class public class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } static void main(String[] args){ System.out.println(Foo.getK()); Foo f = new Foo(); System.out.println(f.getI()); } } javac static f new Foo() C:> java Foo

  4. OK, So I Cheated Foo.java public class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } static void main(String[] args){ System.out.println(Foo.getK()); Foo f = new Foo(); System.out.println(f.getI()); } } public class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } public static void main(String[] args){ System.out.println(Foo.getK()); Foo f = new Foo(); System.out.println(f.getI()); } } public class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } public static void main(String[] args){ System.out.println(Integer.toString(Foo.getK())); Foo f = new Foo(); System.out.println(f.getI()); } } public class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } public static void main(String[] args){ System.out.println(Integer.toString(Foo.getK())); Foo f = new Foo(); System.out.println("k = " + f.getI()); } } public class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } public static void main(String[] args){ System.out.println(Integer.toString(Foo.getK())); Foo f = new Foo(); System.out.println("" + f.getI()); } }

  5. Breaking Up is Not Hard to Do Foo.java class Fe { int i = 4; int getI() { return i; } } class Fi { static int k = 2; static int getK() { return k; } } public class Foo { public static void main(String[] args){ System.out.println("" + Foo.getK()); Foo f = new Foo(); System.out.println("" + f.getI()); } } class Fe { int i = 4; int getI() { return i; } } class Fi { static int k = 2; static int getK() { return k; } } public class Foo { public static void main(String[] args){ System.out.println("" + Foo.getK()); Foo f = new Foo(); System.out.println("" + f.getI()); } } class Fe { int i = 4; int getI() { return i; } } class Fi { static int k = 2; static int getK() { return k; } } public class Foo { public static void main(String[] args){ System.out.println("" + Fi.getK()); Fe f = new Fe(); System.out.println("" + f.getI()); } } class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } public static void main(String[] args){ System.out.println("" + Foo.getK()); Foo f = new Foo(); System.out.println("" + f.getI()); } } class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } } public class Foo { public static void main(String[] args){ System.out.println("" + Foo.getK()); Foo f = new Foo(); System.out.println("" + f.getI()); } } class Fe { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } } public class Foo { public static void main(String[] args){ System.out.println("" + Foo.getK()); Foo f = new Foo(); System.out.println("" + f.getI()); } }

  6. Breaking Up is Not Hard to Do Fe.class new Fi.class Foo.class Foo.java class Fe { int i = 4; int getI() { return i; } } class Fi { static int k = 2; static int getK() { return k; } } public class Foo { public static void main(String[] args){ System.out.println("" + Fi.getK()); Fe f = new Fe(); System.out.println("" + f.getI()); } } javac

  7. Breaking Up is Not Hard to Do Fe.class new Fi.class Foo.class Fe.java class Fe { int i = 4; int getI() { return I; } } javac Fi.java class Fi { static int k = 2; static int getK() { return k; } } javac Foo.java public class Foo { public static void main(String[] args){ System.out.println("" + Fi.getK()); Fe f = new Fe(); System.out.println("" + f.getI()); } } javac

  8. Examples in the Thinking in Java class Foo { int i = 4; int getI() { return i; } static int k = 2; static int getK() { return k; } public static void main(String[] args){ System.out.println("" + Foo.getK()); Foo f = new Foo(); System.out.println("" + f.getI()); } } class Foo { // . . . // Code to illustrate behavior // . . . public static void main(String[] args){ Foo f = new Foo(); // Demonstrate f's behavior here } }

  9. Coding Style • Capitalize the first letter of a class name • The first letter of embedded words are capitalized • MyVeryOwnClass • The same for most all other names except lowercase the first letter • myVeryOwnOther

  10. Comments in the Code // following "//" everything is a comment /* stuff between the slash asterisk on this line is comment stuff and the asterisk slash on this line */

  11. Embedded Documentationand javadoc /** @tagargument(s) @tagargument(s) @tagargument(s) */ /** <ol> <li> One <li> Two </ol> */ Preceding a variable, class or method definition.

  12. Embedded Documentation • @see: referring to other classes • Class tags • @version • @author • @since • Method tags • @param • @return • @throws • @deprecated

  13. What a Class Looks Like The class keyword says this is the beginning of a class definition class Foo { // the stuff that really defines a class goes here } The name of the class Foo The beginning of the class definition The end of the class definition

  14. What a Class Looks Like The public keyword may appear, it says this class can be accessed by any other class. public class Foo2 extends Foo1 implements That { // the stuff that really defines a class goes here } Yes there are other keywords that may appear along with class. The implements keyword may appear, it says this class meets a specification defined in a thing called an interface named That. The extends keyword may appear, it says that this class is just like the class Foo1 with the stuff of class Foo2 added.

  15. What a Class Looks Like The order doesn't matter! File Fe.java File Fe.java public class Fe { // Fe stuff goes here } class Fi { // Fi stuff goes here } class Fo { // Fo stuff goes here } class Fum { // Fum stuff goes here } class Fum { // Fum stuff goes here } class Fo { // Fo stuff goes here } class Fi { // Fi stuff goes here } Public class Fe { // Fe stuff goes here } =

  16. What a Class Looks Like What can you expect to find inside a class? • More Classes • Comments and Embedded Documentation • Fields Containing (Variables) • Primitive Values • References to Objects • Methods, Arguments and Return Values (Subroutines) • Initializers and Constructors (Chap. 4)

  17. What a Class Looks Like Sometimes classes can be inside other classes but we will discuss that later. File Fe.java public class Fe { // Fe stuff goes here class Fi { // Fi stuff goes here class Fo { // Fo stuff goes here class Fum { // Fum stuff goes here } } } }

  18. What a Class Looks Like Import Statements Precede any Classes File Fe.java import java.util.*; public class Fe { // Fe stuff goes here } class Fi { // Fi stuff goes here } class Fo { // Fo stuff goes here } class Fum { // Fum stuff goes here } What is an import statement and why do I care?

  19. What a Class Looks Like Fields (attributes or variables) File Fe.java import java.util.*; public class Fe { boolean theEarthIsFlat = false; int i = 4; int j, k; float f = 8.7f; String s = new String("Hello"); Date d = new Date(); }

  20. What a Class Looks Like Fields (attributes or variables) File Fe.java import java.util.*; public class Fe { boolean theEarthIsFlat; float f; int i ,j, k; String s; Date d; Fe() { theEarthIsFlat = false; i = 4; f = 8.7f; s = new String("Hello"); d = new Date(); } }

  21. What a Class Looks Like All the Primitives boolean b; // true, false char c; // 16 bit Unicode 0 to +2+16-1 byte by; // 8 bit integer -2-7 to +2+7-1 short s; // 16 bit integer -2-15 to +2+15-1 int i; // 32 bit integer -2-31 to +2+31-1 long l; // 64 bit integer -2-63 to +2+63-1 float f; // 32 bit floating point IEEE754 Standard double d; // 64 bit floating point IEEE754 Standard

  22. Wrapper Classes for the Primitives Wrapper ClassPrimitive Boolean b = new Boolean(true); // boolean Character c = new Character(‘c’); // char Byte by = new Byte(20); // byte Short s = new Short(500); // short Integer i = new Integer(50000); // int Long l = new Long(1000000000); // long Float f = new Float(23.45f); // float Double d = new Double(234.567f); // double

  23. What a Class Looks Like All the Classes String s; // String a string of Unicode characters // s is a reference to a String object Date d; // Date a date/time object // d is a reference to a Date object • About 1500 other classes included in the JDK from Sun, • Plus all the classes you and your friends define, and • All the classes available from third party providers.

  24. Creating and Referencing Objects Using String as an Example String s; // s is now a String reference // set to null s = new String(“Hello”); // A String object is created // and s is set to reference // the new object OR String s = new String(“Hello”); // All in one line OR String s = “Hello”; // A special shorthand // allowed for String only. System.out.println(s); // Using the reference to // print the object.

  25. Java has Arrays A Simple Example Now (a whole chapter later) int[] k; // Makes k a reference to an array of ints int k[]; // Equivalent to int[] k; int k[] = {10, 20, 30, 40}; System.out.println("k[0] = " + k[0]); System.out.println("k[1] = " + k[1]); System.out.println("k[2] = " + k[2]); System.out.println("k[3] = " + k[3]); outputs k[0] = 10 k[1] = 20 k[2] = 30 k[3] = 40

  26. Java has Arrays Lets Make a Complete Program Out Of It File: SimpleArrayThing.java public class SimpleArrayThing { public static void main(String[] args) { } } int k[] = {10, 20, 30, 40}; System.out.println("k[0] = " + k[0]); System.out.println("k[1] = " + k[1]); System.out.println("k[2] = " + k[2]); System.out.println("k[3] = " + k[3]); outputs k[0] = 10 k[1] = 20 k[2] = 30 k[3] = 40

  27. Java has Arrays How About Objects File: SimpleArrayThing.java public class SimpleArrayThing { public static void main(String[] args) { } } String[] k = {"ten", "twenty", "thirty", "forty"}; System.out.println("k[0] = " + k[0]); System.out.println("k[1] = " + k[1]); System.out.println("k[2] = " + k[2]); System.out.println("k[3] = " + k[3]); outputs k[0] = ten k[1] = twenty k[2] = thirty k[3] = forty

  28. A Static Place for Simple Testing File: MyClassName.java // import here if needed public class MyClassName { public static void main(String[] args) { } } // // This is where you try simple declarations // and executable statements. // // Then print the results. System.out.println("variable = " + variable);

  29. The Staff of Hotel JavaCleans up abandoned Objects String s; s = "Hello"; s = "Goodbye";

  30. Scoping References and Object Lifetimes /* Stuff between these things are comments */ { int i = 42; /* only i available */ { int j = 100; String s = new String("Hi There"); /* i, j, and s are available */ } /* only i available */ /* j and s out of scope */ /* What happens to "Hi There"? */ }

  31. Summary • Java Programming Centers Around Classes • Classes contain data and methods • Static members have a single identity • The "new" keyword produces a new Object of the Class

  32. Exercises • Enter the first Foo class and compile and run it • Modify Foo to have separate Fe and Fi classes compile and run. • Make Foo, Fe and Fi into separate files, compile and run. • Do exercise 9 page 131. (see also page 128)

  33. End of Content

More Related