유디의 공간정보·개발일기
5. 0709_ 널체크(Null Check), ArrayList 본문
Ex5_class
Animal | Dog / Animal 상속 | Cat / Animal 상속 |
String name; int age; String mobile; public void standup() { } public void sitdown() { } public void run() { } |
public class Dog extends Animal public Dog() { } public Dog(String name, int age, String mobile) { this.name = name; this.age = age; this.mobile = mobile; } |
public class Cat extends Animal public String getName() { return super.name; } |
int type = 0 | type = 1; | type = 2; |
[Test1]
package com.lx;
import com.lx.animal.Animal;
import com.lx.animal.Cat;
import com.lx.animal.Dog;
public class Test1 {
public static void main(String[] args) {
int type = 0;
Animal animal = null;
if (animal == null) { // <-- NULL CHECK
System.out.println("animal 변수가 비어있어요.");
} else {
animal.sitdown();
}
// animal.sitdown();를 그냥 실행하면 비정상종료되므로
// --> java.lang.NullPointerException오류 발생하므로 null체크 필요하다.
// if문을 적어서 null값 확인한 후 함수를 실행하면 정상적으로 작동함
//Dog 클래스 import, public 붙이기
// 1. new Dog에 매개변수 안넣고 만들기
animal = new Dog();
type = 1; //animal.name/age/mobile
animal.name = "댕댕이1";
animal.age = 1;
animal.mobile = "010-0000-0000";
System.out.println("첫번째 강아지 이름은 : " + animal.name);
System.out.println("첫번째 강아지 나이는 : " + animal.age);
// 2. new Dog에 매개변수 넣어서 만들기
Dog dog2 = new Dog("댕댕이2", 2, "010-1234-5678");
System.out.println("두번째 강아지 이름은 : " + dog2.name);
System.out.println("두번째 강아지 나이는 : " + dog2.age);
// Cat 클래스 import, public 붙이기
animal = new Cat();
type = 2;
animal.name = "냥이";
animal.age = 2;
animal.mobile = "010-1000-1000";
System.out.println("고양이 이름은 : " + animal.name);
// cat1.getName();
// 1. type을 활용한 if문
if (type == 1) { // == : 왼쪽값과 오른쪽 값이 같은지 물어보는 것
System.out.println("Dog객체 생성됨");
} else if (type == 2) {
System.out.println("Cat객체 생성됨");
} else {
System.out.println("객체생성 안됨");
}
// 2. instanceof를 활용한 if문
// - type이라는 구분자를 별도로 일일히 만들 필요가 없다는 장점이 있다.
if (animal instanceof Dog) {
System.out.println("Dog객체 생성됨");
} else if (animal instanceof Cat) {
System.out.println("Cat객체 생성됨.");
} else {
System.out.println("객체생성 안됨");
}
}
public static int add(int a, int b) {
return a + b;
}
}
ArrayList 적용
[Test5]_[Test1]
import java.util.ArrayList;
public class Test1 {
public static void main(String[] args) {
String name1 = "홍길동1";
String name2 = "홍길동2";
String name3 = "홍길동3";
ArrayList<String> names = new ArrayList<String>();
names.add("홍길동1"); // 0번째
names.add("홍길동2"); // 1번째
names.add("홍길동3"); // 2번째
String out1 = names.get(0); //0번째의 값이 out1이란 변수명으로 들어감
System.out.println("첫번째 칸의 값 : " + out1); //out1 안에 들어있는 0번째 값이 나옴
System.out.println(" 칸의 갯수 : " + names.size()); //변수명.size() : 칸 갯수 계산
// 칸의 번지수를 일일이 넣는 대신 for문을 사용하면 총 칸 수만큼 값 출력 가능
for (int i = 0; i < names.size(); i++) {
String outName = names.get(i);
System.out.println("이름" + i + " : " + outName);
}
}
}
- 각각의 변수를 만들었던 방법 대신 리스트를 사용하는 것
- ArrayList<>에서 <>는 변수크기를 적는다.
- ArrayList는 0번지(0값)부터 시작한다. --> Index(인덱스)
- 변수명.add( )
--> 리스트의 각 칸에 넣을 값을 넣어주는 코드
--> 같은 크기인 것들끼리 넣어야 성능이 빠르고 일반적인 방식
'JAVA' 카테고리의 다른 글
7-1. 0713_ Interface, Abstract class (0) | 2021.10.24 |
---|---|
6. 0712_ try-catch절, Getter and Setter (0) | 2021.10.24 |
4. 0708_생성자함수, if문 (리뷰) (0) | 2021.10.23 |
3. 0707_review (0) | 2021.10.20 |
2. 0706_생성자(Constructor), Getter and Setter, 상속 (0) | 2021.10.20 |