[Java] tutorial / interface / implements

Onam Kwon·2022년 4월 30일
0

Java

목록 보기
2/7

Main.java

import java.util.Arrays;

// Parental class.
class Champion {
    // protected variables can be accessed in the same packages or inherited classes.
    protected String name;
    protected String q;
    protected String w;
    protected String e;
    protected 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;
    }
    public void ignite() {
        System.out.println(this.name + " has used ignite.");
    }
    public void flash() {
        System.out.println(this.name + " has used flash.");
    }
}

class AD extends Champion implements Champ {   // AD class inherited Champion class, Champ interface can ues declared methods.
    // Constructor.
    AD(String name, String q, String w, String e, String r) {
        // super() method calls Champion constructor in Champion class.
        super(name, q, w, e, r);
    }

    // methods.
    public void primaryDMG() {
        System.out.println(this.name + " primarily uses attack damage(AD).");
    }
    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);
    }
}

class AP extends Champion implements Champ {   // AP class inherited Champion class, Champ interface can ues declared methods.
    // Constructor.
    AP(String name, String q, String w, String e, String r) {
        // super() method calls Champion constructor in Champion class.
        super(name, q, w, e, r);
    }
    // methods.
    public void primaryDMG() {
        System.out.println(this.name + " primarily uses ability point(AP).");
    }
    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);
    }
}

// Displaying which champ primarily uses either AD or AP.
class PrimaryDamage {
    public void attack(Champ champ) {
        champ.primaryDMG();
    }
}

// Interface.
interface Champ {
    // This method is defined in AD or AP class.
    void primaryDMG();
}
public class Main {
    // static keyword makes available to use method without generating an object.
    public static void main(String[] args) {
        AD talon = new AD("Talon", "Noxian Diplomacy", "Rake", "Assasin's path", "Ult");
        AP leblanc = new AP("LeBlanc", "Sigil of Malice", "Distortion", "Ethereal Chains", "Ult");
        PrimaryDamage dmgCheck = new PrimaryDamage(); // Displaying which champ primarily uses either AD or AP.

        talon.usesCombo();
        talon.ignite(); // AD object is using inherited method that came from Spell class.
        talon.flash(); // AD object is using inherited method that came from Spell class.
        dmgCheck.attack(talon); // AD

        System.out.println();

        leblanc.usesCombo();
        leblanc.ignite(); // AP object is using inherited method that came from Spell class.
        leblanc.flash(); // AP object is using inherited method that came from Spell class.
        dmgCheck.attack(leblanc); // AP
    }
}

▼결과▼

  • 부모 클래스: Champion
  • 자식 클래스: AD and AP
  • 인터페이스: Champ
  • 인터페이스에 사용되는 클래스: PrimaryDamage
    • 만약 자식클래스가 AD and AP두개로 끝나는게 아닌 종류가 더 생긴다면 일일이 그에 따른 메소드를 추가해 줘야 하지만, Champ 인터페이스를 사용함으로서 PriamaryDamage 클래스에서 객체로 사용 가능.
    • AD and AP처럼 각 클래스에 본인의 메소드 primaryDMG()를 정의해주면 자동으로 구별 가능.
profile
권오남 / Onam Kwon

0개의 댓글