[2017/2/6] Project Java: CH 6. 객체지향 프로그래밍 I 코드

1.
class MyMath{
long a, b;

long add(){ return a + b; }
long subtract(){return a – b;}

static long add(long a, long b){return a + b;}
static long subtract(long a, long b){return a – b;}
}

public class Hello {
public static void main(String[] args) {
System.out.println(MyMath.add(200L, 100L));
System.out.println(MyMath.subtract(200L, 100L));

MyMath mm = new MyMath();
mm.a = 200L;
mm.b = 100L;
System.out.println(mm.add());
System.out.println(mm.subtract());
}
}

2. 메서드 오버로딩
public class Hello {
public static void main(String[] args) {
MyMath mm = new MyMath();
System.out.println(mm.add(3, 3));
System.out.println(mm.add(3L, 3L));

int[] a = {100, 200, 300};
System.out.println(mm.add(a));
}
}

class MyMath{
int add (int a, int b){
return a + b;
}

long add (long a, long b){
return a + b;
}

int add (int[] arr){
int result = 0;
for(int i : arr){
result += i;
}
return result;
}
}

3. String… args
public class Hello {
public static void main(String[] args) {
String[] strArr = {“100”, “200”, “300”};

System.out.println(concatenate(“-“, “100”, “200”,”300″));
System.out.println(concatenate(“-“, strArr));
System.out.println(concatenate(“,”, new String[]{“1”, “2”, “3”}));
System.out.println(“[“+concatenate(“,”, new String[0])+”]”);
}

static String concatenate(String delim, String… args){
String result = “”;

for(String str : args){
result += str + delim;

}

return result;
}
}
–> String…(한칸 띄어쓰기)args

4. 생성자
class Data1{
int value;
}

class Data2{
int value;

Data2(int x){
value = x;
}
}

public class Hello {
public static void main(String[] args) {
Data1 d1 = new Data1();
Data2 d2 = new Data2(2);
}
}

5. 생성자 간의 유기적인 연결
class Car{
String color;
String gearType;
int door;

Car(){
this(“white”, “auto”, 4);
}

Car(Car c){
color = c.color;
gearType = c.gearType;
door = c.door;
}

Car(String color){
this(color, “auto”, 4);
}

Car(String color, String gearType, int door){
this.color = color;
this.gearType = gearType;
this.door = door;
}
}

public class Hello {
public static void main(String[] args) {
Car c1 = new Car();
Car c3 = new Car(“white”);
Car c2 = new Car(“white”, “auto”, 4);
Car c4 = new Car(c2);

System.out.println(c1.color+”, “+c1.gearType+”, “+c1.door);
}
}
–> this: 인스턴스 자신을 가리키는 참조변수
this(): 생성자. 같은 클래스의 다른 생성자 호출할 때 사용

6. 초기화 블록
public class Hello {
static {
System.out.println(“클래스 초기화 블록”);
}

{
System.out.println(“인스턴스 초기화 블록”);
}

Hello(){
System.out.println(“생성자”);
}

public static void main(String[] args) {
System.out.println(“새로운 인스턴스 만듦요”);
Hello h1 = new Hello();

System.out.println(“인스턴스 또 하나 만들게요”);
Hello h2 = new Hello();
}
}
–>
클래스 초기화 블록
새로운 인스턴스 만듦요
인스턴스 초기화 블록
생성자
인스턴스 또 하나 만들게요
인스턴스 초기화 블록
생성자

7.
class Product{
static int count = 0;
int serialNo;

{
++count;
serialNo = count;
}

public Product(){}
}

public class Hello {
public static void main(String[] args) {
Product p1 = new Product();
Product p2 = new Product();
Product p3 = new Product();

System.out.println(p1.serialNo);
System.out.println(p2.serialNo);
System.out.println(p3.serialNo);
System.out.println(Product.count);
}
}

댓글 남기기