Enkapsulasi

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.4 kB
17
Indexable
Never
package Encapsulation1;

class Player {
    private String name;
    private int baseHealth;
    private Armor armor;

    public Player(String name) {
        this.name = name;
        this.baseHealth = 100;
    }

    public void setArmor(Armor armor) {
        this.armor = armor;
    }

    void display() {
        System.out.println("Nama  :" + this.name);
        System.out.println("Health : " + this.baseHealth);
        System.out.println("MaxHealth : " + this.maxHealth());
    }

    public int maxHealth() {
        // this.armor.defense = 100
        // return this.baseHealth + this.armor.defense
        return this.baseHealth + this.armor.getAddHealth();
    }
}

class Armor {
    private String name;
    private int defense;
    private int health;

    public Armor(String name, int defense, int health) {
        this.name = name;
        this.defense = defense;
        this.health = health;
    }

    public int getAddHealth() {
        return this.defense * 10 + this.health;
    }

    public int getDefense() {
        return this.defense;
    }
}

public class Main {
    public static void main(String[] args) {
        Player player1 = new Player("Agus");
        Armor armor1 = new Armor("Baju Zirah", 10, 100);
        player1.setArmor(armor1);
        player1.display();
    }
}
Leave a Comment