Java의 입력/출력과 스트림
Alex Liu
Software Development Engineer
Animal 클래스는 일반적인 동물을 나타냅니다class Animal {
void makeSound() {
System.out.println("Animal sound");
}
public static void main(String[] args) {
Animal a = new Animal();
a.makeSound(); // Output: Animal sound
}
}
Animal sound
// Use `extends` to create a subclass of `Animal` named `Dog`
class Dog extends Animal {
void bark() {System.out.println("Bark");}
}
public static void main(String[] args) {
Dog d = new Dog(); // Create new instance of Dog object which extends Animal
d.makeSound();
d.bark();
}
Animal sound
Bark
class Cat extends Animal {
@Override // Use `@Override` to override the behavior of `.makeSound()`
void makeSound() {
System.out.println("Meow");
}
}
public static void main(String[] args) {
Cat c = new Cat(); // Create a new instance of Cat object which extends Animal
c.makeSound(); // Call the overrided method `makeSound()`
}
Meow
public class RecursionExample {
static void countdown(int n) {
// Base case
if (n == 0) return;
System.out.println(n);
// Recursive call
countdown(n - 1);
}
}
public static void main(String[] args) {
countdown(5);
}
0인 기저 사례에 도달하면 재귀 호출이 중지됩니다5
4
3
2
1
extends로 상속을 활성화합니다
@Override로 상속된 동작을 재정의합니다
Recursion은 문제를 더 작은 단계로 나눕니다
Java의 입력/출력과 스트림