[Java] tutorial / class / inheritance

Onam Kwon·2022년 4월 29일
0

Java

목록 보기
1/7

Main.java

import java.util.Arrays;

class Spell {
    private String name;
    protected void setName(String name) {
        this.name = name;
    }
    public void ignite() {
        System.out.println(this.name + " has used ignite.");
    }
    public void flash() {
        System.out.println(this.name + " has used flash.");
    }
}

class Champion extends Spell{   // Champion class inherited Spell class.
    private String name;
    private String q;
    private String w;
    private String e;
    private String r;
    
    // Constructor.
    Champion(String name, String q, String w, String e, String r) {
        this.name = name;
        this.q = q;
        this.w = w;
        this.e = e;
        this.r = r;
        setName(name);  // this method came from Spell class.
    }
    
    // method.
    public void usesCombo() {
        System.out.println(this.name + " using combo:: ");
        System.out.println("q: "+ this.q + ", w: "+ this.w + ", e: "+ this.e + ", r: "+ this.r);
    }
}

public class Main {
	// static keyword makes available to use method without making an object.
    public static void main(String[] args) {
        System.out.println("Hello world!");
        int [] num = {1,2,3,4,5};
        char [] word = {'a', 'p', 'p', 'l', 'e'};
        System.out.println(Arrays.toString(num));
        System.out.println(word); // char array does not need to use Arrays.toString() method to print out itself.
        System.out.print(word.length + "\n");

        Champion talon = new Champion("talon", "Noxian Diplomacy", "Rake", "Assasin's path", "Ult");
        talon.usesCombo();
        talon.ignite(); // Champion object is using inherited method that came from Spell class.
        talon.flash(); // Champion object is using inherited method that came from Spell class.

    }
}

▼결과▼

  • 자바에서 class앞에 public이 붙어있으면 파일명과 동일함.
    • 우리는 public class Main에서 이 파일 이름이 Main.java인걸 알 수 있음.
  • method앞에 static키워드가 있는 경우 해당 메소드는 객체를 만들지 않아도 class.method()처럼 호출 가능.
  • 대부분의 문법은 C++과 비슷
profile
권오남 / Onam Kwon

0개의 댓글