1 / 21

Instance Method Review - CIS 1068 Program Design and Abstraction

Instance Method Review - CIS 1068 Program Design and Abstraction. Zhen Jiang CIS Dept. Temple University 1050 Wachman Hall, Main Campus Email: zhen.jiang@temple.edu. Table of Contents. Introduction Declaration and call Access and scope. Introduction. Why? Why loop Why array

gmiley
Download Presentation

Instance Method Review - CIS 1068 Program Design and Abstraction

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. Instance Method Review - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University 1050 Wachman Hall, Main Campus Email: zhen.jiang@temple.edu

  2. Table of Contents • Introduction • Declaration and call • Access and scope

  3. Introduction • Why? • Why loop • Why array • Why method • Why object (the host of instance method)?

  4. Every thing under the control inside of object (which must be declared in the class, as a general template to cover all possible changes/statuses)

  5. Dog class state: String name, breed int age behavior: writeOutput ( ) getAgeInHumanYears ( )‏ Dog object balto state:“Balto” (name) 8 (age) “Siberian Husky” (breed) behavior:getAgeInHumanYears ( ) writeOutput ( ) Dog object scooby state:“Balto” (name) 8 (age) “Siberian Husky” (breed) behavior:getAgeInHumanYears ( ) writeOutput ( )

  6. Declaration and Call • No more static • How to use instance method m()? public class A { public void m() { … } }

  7. public class B { public void m() { … } public void m3() { m() A a; //a.m is not supported A b = new A (); b.m(); } } public class A { public void m() { … } public void m2() { m(); } }

  8. public class A { public void m() { … } public static void m2() { A a = new A(); a.m(); } }

  9. For comparison public class B { public void m() { … } public void m3() { m() A.m(); } } public class A { public static void m() { … } public void m2() { m(); } }

  10. For comparison public class B { public void m1(){ A a = new A(); a.m1(); A.m2();}public static void m2(){System.out.println("B's static m2"); B b2 = new B(); b2.m1();}public static void main(String args[]){ A a = new A(); B b = new B(); b.m1(); B.m2();} } public class A { public void m1(){System.out.println("A's m1"); m2();}public static void m2(){System.out.println("A's static m2");} } A's m1 A's static m2 A's static m2 B's static m2 A's m1 A's static m2 A's static m2

  11. Constructor • Name • No return • Not static • Related to the use of new • Overloading

  12. Mutator • Argument and parameter • Type (compatible & overloading) • Primitive type and reference type (array & object) • Accessor • Return • toString

  13. Constructor public class B { public void m3() { A a = new A(); A b = new A(2); } } public class A { public A() { … } public A(int x) { … } }

  14. Mutator and accessor public class B { public void m3() { A a = new A(); a.a(); a.A(); // but very confused! } } public class A { public A() { … } public void a() { … } public void A() { … } }

  15. When an object is printed or concatenated with a String, Java calls the object's toString method. System.out.println("p1 is " + p1); is equivalent to: System.out.println("p1 is " + p1.toString()); Note: Every class has a toString method.

  16. toString public class B { public void m3() { A a = new A(); System.out.println(a); } } public class A { private int x = 2; public A() { … } public String toString () { // no parameter! return “a” + x; } }

  17. Access and Scope • Attribute (property, data field, etc) • What is “this” • Parameter

  18. Attribute • what is this public class B { public void m3() { A a = new A(2); a.m(3); System.out.println(a); } } public class A { private int x; public A(int y) { x = y; } public void m(int x) { // int x; this.x = x; } public String toString(){ return x+””; } }

  19. Parameter public class B { public void m3() { A a = new A(2); A b = new A(3); a.m(b); } } public class A { public int x; public A(int y) { x = y; } public void n() { // another method // other than m } public void m(A a) { // x and a.x // n(); and a.n(); } }

  20. Parameter public class B { public void m3() { A a = new A(2); A b = new A(3); a.m(b); } } public class A { privateint x; public A(int y) { x = y; } public void n() { // another method // other than m } public intgetX(){ return x; } public void m(A a) { // x and a.getx() // n(); and a.n(); } }

  21. ArrayOperation.java vs. ArrayOperations.java http://www.cis.temple.edu/~jiang/ArrayOperation.java http://www.cis.temple.edu/~jiang/ArrayOperations.java

More Related