i know that in composition the class composed inside, it's object should not exists outside of its class, but in the example below the Sword
class is composed into the Unit
class, and i can still create it's object independently without the unit class, like i did in main, how i can prevent for Sword
object to exists independently?
public class Main {
public static void main(String[] args) {
Sword sword = new Sword();
}
}
class Sword {
private int a;
}
class Unit {
private String s1;
private Sword s;
void setProperty(String s1, Sword s) {
this.s1 = s1;
this.s = s;
}
}
Aucun commentaire:
Enregistrer un commentaire