728x90 JAVA126 [JAVA] ch05-22. 초기화블럭 2 public class Ex22 { public static void main(String[] args){ Product p1 = new Product(); Product p2 = new Product(); Product p3 = new Product(); System.out.printf("p1: %d\n",p1.serialNo); System.out.printf("p2: %d\n",p2.serialNo); System.out.printf("p3: %d\n",p3.serialNo); System.out.printf("Total: %d", Product.count); } } class Product{ static int count = 0; int serialNo; { serialNo = ++count; /.. JAVA/예제 2017. 8. 22. [JAVA] ch05-20. 초기화블럭 public class Ex20 { public static void main(String[] args){ Ex20 e = new Ex20(); System.out.println(); Ex20 e2 = new Ex20(3); } Ex20(){ System.out.println("Ex20()"); } Ex20(int count){ System.out.println("Ex20("+count+")"); } { // 생성자보다 초기화블럭의 우선순위가 높다. //바이트코드 로딩될때 실행// 쓸데없다(무쓸모) 왓스)에 다모아서 초기화 작업을 다 해줌 System.out.println("1: {}"); } { System.out.println("2: {}"); } } JAVA/예제 2017. 8. 22. [JAVA] ch05-19. 생성자 4 public class Ex19 { public static void main(String[] args){ Auto auto = new Auto(); Auto auto2 = new Auto(auto); char a = 0; } } class Auto{ String color; String gearType; int door; Auto(){ this("White", "auto", 4); } Auto(Auto auto){ color = auto.color; gearType = auto.gearType; door = auto.door; } Auto(String color, String gearType, int door){ this.color = color; this.gearType = gearType; this.. JAVA/예제 2017. 8. 22. [JAVA] ch05-18. 생성자 3 public class Ex18 { public static void main(String[] args){ Vehicle v1 = new Vehicle(); Vehicle v2 = new Vehicle("blue"); System.out.printf("v1: %s %s %d\n", v1.color, v1.gearType, v1.door); System.out.printf("v2: %s %s %d",v2.color, v2.gearType, v2.door); } } class Vehicle{ String color; String gearType; int door; //this JAVA/예제 2017. 8. 22. [JAVA] ch05-17. 생성자 2 public class Ex17 { public static void main(String[] args){ Car c1 = new Car(); c1.color = "White"; c1.gearType = "Auto"; c1.door = 4; Car c2 = new Car("Black", "Auto", 2); System.out.printf("c1: %s %s %d\n",c1.color, c1.gearType, c1.door); System.out.printf("c2: %s %s %d",c2.color, c2.gearType, c2.door); } } class Car{ String color; String gearType; int door; Car(){} Car(String c, String g, int.. JAVA/예제 2017. 8. 22. [JAVA] ch05-16. 생성자 public class Ex16 { Data1 d1 = new Data1(); Data2 d2 = new Data2(); Data2 d3 = new Data2(3); } class Data1{ int value; } class Data2{ int value; Data2(){ value = 100; } Data2(int x){ value = x; } } JAVA/예제 2017. 8. 22. [JAVA] ch05-15. Method (메서드) public class Ex15 { public static void main(String[] args){ Machine machine = new Machine(); System.out.printf("add(int a, int b): %d\n", machine.add(3,3)); System.out.printf("add(int a, long b): %d\n", machine.add(3,3L)); System.out.printf("add(long a, int b): %d\n", machine.add(3L,3)); } } class Machine{ // 하나의 메서드값에 여러 값의 파라미터 int add(int a, int b){ return a+b; } long add(int a, long b){ retu.. JAVA/예제 2017. 8. 22. [JAVA] ch05-14. Static method (스태틱 메서드) 2 public class Ex14 { int iv = 10; // iv(instance variable) static int cv = 20; //cv(class variable) int iv2 = cv; //static int cv2 = iv; //(X) static int cv2 = new Ex14().iv; //(O) static void sm1(){ // sm(static method) System.out.println(cv); //System.out.println(iv);//(X) System.out.println(new Ex14().iv);//(O) } void im1(){ //im(instance method) System.out.println(cv); System.out.println(iv);.. JAVA/예제 2017. 8. 22. [JAVA] ch05-13. Static method (스태틱 메서드) = 클래스메서드 public class Ex13 { public static void main(String[] args){ // 클래스메서드=스태틱 메서드(객체 생성없이 사용할 수 있는 메서드) System.out.println(Calculator.add(300,200)); System.out.println(Calculator.subtract(300,200)); System.out.println(Calculator.multiple(300,200)); System.out.println(Calculator.divide(300,200)); Calculator caculator = new Calculator(); caculator.a = 300; caculator.b = 200; System.out.println(caculat.. JAVA/예제 2017. 8. 22. [JAVA] ch05-10. 클래스와 메서드에서의 객체 호출 2 public class Ex10 { public static void main(String[] args){ Data d = new Data(); d.x =10; System.out.println("main():x = "+d.x); Ex10 e = new Ex10(); e.change(d); System.out.println("main():x = "+d.x); } void change(Data d){ d.x = 1000; System.out.println("change():x= "+d.x); } } //Data class in Ex09 JAVA/예제 2017. 8. 22. [JAVA] ch05-09. 클래스와 메서드에서의 객체 호출 public class Ex09 { public static void main(String[] args){ Data d = new Data(); d.x = 10; System.out.println("main():x="+d.x); Ex09 e = new Ex09(); e.change(d.x); System.out.println("main():x=" +d.x); } void change(int x){ x = 1000; System.out.println("challenge():x="+x); } } class Data{ int x; } JAVA/예제 2017. 8. 22. [JAVA] ch05-07. 메서드 호출 public class Ex07 { // 메서드에서 다른 메서드를 호출할 때. public static void main(String[] args){ MethodTest m = new MethodTest(); m.first(); } } class MethodTest{ void first(){ System.out.println("first."); second(); } void second(){ System.out.println("second."); third(); } void third(){ System.out.println("third."); } } JAVA/예제 2017. 8. 22. 이전 1 2 3 4 5 6 7 8 ··· 11 다음 728x90