@ Lesson 4 (Object) (Class) (Instance) (Method) (Constructor) Memory 1 UML 1
@ & 1 (Real World) (Software World)
@ &.. () ()
@ & 2 (Real World) (Software World) OOA/ Modeling Abstraction Instantiation
@ & 3 (Real World) (Software World) OOA/ Modeling Abstraction Instantiation class Account{ double money; getmoney(){} deposit(){} withdraw(){} } Account acc= new Account()
@ & 4 Real World (Software World) Abstraction Instantiation class Account{ double balance; getbalance(){} deposit(double a){} withdraw(double b){} }
@ 1.. Behavior Attribute
@ 2 Class Member field Method (Constructor) Method A Method B Field (data) Method (data)
@ Class Class [ classmodifier ] class ClassName //header { // body /* field */ } /* method */ classmodifier : public, friend, final, abstract
@ (1)
@ (2)
@
@ Instantiation Heap Account acc1=new Account(); Account acc2=new Account(); Account acc1=new Account(); Static Class Stack Reference variable Account acc1=new Account(); Instance Heap () ( ) acc3 acc2 acc1 (,)
@ Instantiation 2 Static Stack Heap Class car3 car2 car1 Car car1=new Car(); Car car2=new Car(); Car car3=new Car();
@ Method 200 argument return logic public int coffee( int money ) { //logic if( money>=1000 ) return 5; // 5 else if( money>=800 ) return 4; // 4 else - - - ; } int cups = vendingmachine.coffee( 800 ); 200 cups : 4
@ static & non static static method member method static field member field method field method method Class Heap (static) memory Heap Heap Stack
@ 3,
@ 4 : Car :,, :,, public class Car { /** * */ private int speed = 0; /** * -90 90 0 */ private int direction = 0; /** * Car default */ public Car(){} //constructor /** * 5Km/hr */ public void speedup() { speed+=5; }
@ 5 : Car :,, :,, /** 5Km/hr */ public void speeddown() { speed-=5; } /** * (Km/hr) * @return int */ public int curspeed(){ return speed; } /** *. * @param dir */ public void turndirect(int dir) { direction+=dir; } /** * (0:, -90, 90 ) * @return int */ public int curdirect(){ return speed; } }
@ javadoc javadoc use private ClassName.java /** *. * @param dir */ public void turndirect(int dir) { direction+=dir; } /** * (0:, -90, 90 ) * @return int */ public int curdirect(){ return speed; }
@ JVM 1 JVM Pc Register Frame Register Optop Register Vars Register Method Area (Static,Class) Heap Literal Pool Native Method Stack
@ JVM 2 Static(Class, Method) Stack Heap instance ClassLoad Account Static Variable field,methods AccountMain Static Variable field,methods Heap Literal Pool Methods byte code Main Thread Thread
@ JVM 3 Static Stack Heap Static, Class static, () ( ) acc3 acc2 acc1 (,) Stack reference, referencing Heap
@ reference pointer Static Stack () Heap a2 a1 Account a1=new Account(); Account a2=new Account();
@ 1 public class Accounts { private double money=500; // public Accounts ( double money ){ // this.money=money;} public double getmoney(){ // return money;} public void withdraw(double amount){ // if((amount>0)&&(money-amount>=0)){ money-=amount;}} public void deposit(double amount){ // if(amount>0){ money+=amount;}} }
@ 2 public class AccountMain { public static void main( String[] args) { Accounts acc1=new Accounts(1000); // acc1.deposit(3000); // acc1.deposit(2000); // acc1.withdraw(500);// System.out.println(acc1.getMoney());// } }
@ 3
@ Stack Heap Field money=0 money=0 Field money=0 money=500 acc1 0xa12 public class Account{ private double money=500; public Account(double money){ this.money=money;} ConstructorField 0xa12 money=0 money=1000 Reference Variable money=0 money=1000
@ JVM 4 frame 0xa12 Stack Heap Stack Heap amount acc1 this 0xa12 money= 1000+amount acc1 0xa12 money=4000 Accounts acc1=new Accounts(1000); acc1.deposit(3000); method stack amount, this, frame
@ Garbage Collection Static Stack Heap Static Stack Heap ( ) ( ) a4 a3 a2 a4 a1 a1 a2=null; // a2 Garbage Collection a3=null; //a3 Garbage Collection
@ Type Type Primitive Object, Reference Wrap Wraper class Student stu3 stu2 stu1
@ Airplane,, 1
@ Airplane,, 2
@ Airplane,, 3
@ Airplane,, 4
@ Airplane,, 5
@ Airplane,, 6
@ Airplane,, 7
@ Airplane,, 8
@ Airplane,, 9
@ Airplane,, 10
@ Airplane,, 11
@ Computor Systems Computer MotherBoard Monitor Body KeyBoard/Mouse VGACard CPU SoundCard
@ 1
@ 2
@ 3 public class ComputerMain { public static void main(string[] args) { Computer com=new Computer(); System.out.println(" : "+com.name); System.out.println(" : "+com.monitor.size); System.out.println("CPU : "+com.motherboard.cpu.speed); System.out.println (" : "+com.motherboard.soundcard.company); System.out.println("VGA : "+com.motherboard.vgacard.color); } }
@ Array
@ Array 0 1 2 3 4 indexes elements num stu
@ Array Declaration/Define stus nums ss a stu num
@ Array Initialization stus nums ss a stu num
@ Array- 1
@ Array-
@ Array-
@ Array- 1
@ Array-
@ Array-
@ Array-
@ Array- 5
@,, (,)