다형성
class Shape{
String name;
double area;
void draw() {
System.out.println("모양 그리기");
}
}
class Circle extends Shape{
@Override
void draw() {
System.out.println("원 그리기");
}
}
class Rectangle extends Shape{
@Override
void draw() {
System.out.println("네모 그리기");
}
}
public class Test {
public static void main(String[] args) {
Shape[] datas=new Shape[3];
datas[0]=new Shape();
datas[1]=new Circle();
datas[2]=new Rectangle();
for(Shape v:datas) {
v.draw();
}
}
}
상속
class Person{
String name;
}
class Student extends Person{
}
public class TEST {
public static void main(String[] args) {
Student student=new Student();
System.out.println(student.name);
}
}
캡슐화(은닉화)
class Book{
private int num;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
추상화
abstract class Pokemon{
abstract void hello();
}
class Pikachu extends Pokemon{
@Override
void hello() {
}
}
class Butterfree extends Pokemon{
@Override
void hello() {
}
}
public void main(String[] args) {
int a=10;
int b=11;
if(++a%2==0 || b++%2==0) {
System.out.println("로그A");
System.out.println("a: "+a);
System.out.println("b: "+b);
}
else {
System.out.println("로그B");
System.out.println("a: "+a);
System.out.println("b: "+b);
}
int c=a++*--b;
System.out.println("a: "+a);
System.out.println("b: "+b);
System.out.println("c: "+c);
}
코드의 답은?
a: 11
b: 12
a: 12
b: 11
c: 121
a | b | c | 결과 |
10 | 11 | ||
++a =>11 | 11 | ||
a%2 ==0(F) => 11 | 12 | ||
else => 11 | 12 | 로그 b a=11, b=12 | |
a++ * --b | 12* 11 | 121 | a=12 b=11 c=121 |
public static void main(String[] args) {
int[] datas= {8,2,1,9,7};
int max=datas[0];
int maxIndex=0;
for(int i=1;i<datas.length;i++) {
if(max<datas[i]) {
max=datas[i];
maxIndex=i;
}
}
System.out.println("max: "+max);
System.out.println("maxIndex: "+maxIndex);
}
최대값 찾기 알고리즘(n-1)
max | maxIndex | i | i<datas.length | max<datas[i] |
8 | 0 | 1 | T | F |
8 | 0 | 2 | T | F |
8 | 0 | 3 | T | T |
9 | 3 | 4 | T | T |
9 | 3 | T | T | |
9 | 3 | F | T |
max : 9 maxIndex : 3
public static void main(String[] args) {
ArrayList datas = new ArrayList();
for (int i = 1; i <= 5; i++) {
datas.add(i); // [1, 2, 3, 4, 5]
}
int total = 0;
for (int v : datas) {
total += v;
}
System.out.println("total: " + total); // total: 15
}
왜 문제가 발생할까??
저장할 데이터의 타입을 "강제" genelic제네릭필수!
답은 제네릭!!
ArrayList datas=new ArrayList<>();
오답) 배열로 생각
ArrayList datas는 객체
public class Test{
public static void main(String[] args) {
ArrayList<Integer> datas=new ArrayList<Integer>();;
datas.add(-5);
datas.add(-1);
datas.add(0);
datas.add(1);
datas.add(5);
System.out.println(datas);
for(int i=-1;i<5;i++) {
try {
System.out.println(10/datas.get(i));
} catch(Exception e) {
if(i<0) {
System.out.println("HELLO");
}
else {
System.out.println("JAVA");
}
}
}
}
}
<정답>
i | datas.get(i) | output | |
-1 | catch문(i<0) | hello | |
0 | -5 | 10/datas.get(i) | -2 |
1 | -1 | 10/datas.get(i) | -10 |
2 | 0 | catch문(i<0) | java |
3 | 1 | 10/datas.get(i) | 10 |
4 | 5 | 10/datas.get(i) | 2 |
class A {
int apple;
String banana;
void func(int num) {
num++;
this.apple++;
}
}
public class Test {
public static void main(String[] args) {
int apple;
A a=new A();
System.out.println(a.apple);
int num=123;
a.func(num);
System.out.println(num);
System.out.println(a.apple);
}
}
Q3. num의 값이 123으로 유지되는 이유는?
값에 의한 호출(call by value) 이기 때문에
Q4. 메인에서 num++
메서드 시그니쳐가 없기 때문에
String str="banana";
if(a.banana.equals(str)) {
System.out.println("확인 1");
}
else {
System.out.println("확인 2");
}
}
}
if(a.banana.equals(str)) { <= 오류가 나는 줄
왜 오류가날까??
메서드 수행주체가 주어가 없기 때문에 //stack tracing
a.banana가 주어가 없는 상태 (null값)
바나나에 값을 주면 해결
String banana="banana";
class B {
int b;
B(int b){
this.b=b;
}
}
class C extends B{
int c;
C(int c){
this.c=c;
}
}
Q8. C(int c) { <= 오류가 나는 이유
부모에게 기본 생성자가 없다
Q9. 부모에게 있는 생성자를 사용하면 된다
'리뷰' 카테고리의 다른 글
발표(버블배열) (0) | 2025.01.06 |
---|---|
비전공자를 위한 이해할 수 있는 IT지식 -1 (0) | 2025.01.05 |
T스토리 스킨 - 꾸미기 어렵다... (1) | 2024.12.27 |