java (use class type for field)

has-a relationship

Define Sword class

public class Sword {
  String name;
  int damage;
}

Define Hero class

public class Hero {
  String name;
  int hp;
  Sword sword;                //Sword information
  public void attack() {
    System.out.println(this.name + "I attacked!");
    System.out.println("Inflicted 5 points of damage on the enemy!");
  } 
}

It is also possible to declare a class type variable in the field with Sword type instead of int type and String type. ** has-a relationship ** Hero has-a Sword

Recommended Posts

java (use class type for field)
Java class type field
[Java] Precautions for type conversion
How to use java class
[Java] Memo for naming class names
[Java] How to use LinkedHashMap class
How to use class methods [Java]
[Java] How to use Math class
How to use Java enum type
Use PostgreSQL data type (jsonb) from Java
[Java] How to use the HashMap class
[Java] Data type / string class cheat sheet
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
[Java] Instance method, instance field, class method, class field, constructor summary
[Java] How to use the Calendar class
Java type conversion
Java class methods
java Scanner class
For JAVA learning (2018-03-16-01)
Java HashMap class
[Java] Enumeration type
Java Optional type
java (abstract class)
[Java] Nested class
Java anonymous class
2017 IDE for Java
Java double type
About Java class
[Java] Use Collectors.collectingAndThen
[java] abstract class
[Java] Object class
Java local class
Java for statement
[Java] How to use FileReader class and BufferedReader class
[Java] What should I use for writing files?
Use Java7 try-with-resources statement for Cursor close processing
Use Java external library for the time being
Let's use Java New FileIO! (Introduction, for beginners)
[Java] How to use Calendar class and Date class
[Java] for statement, while statement
[Java] How to use compareTo method of Date class
Use of Date class
About class division (Java)
[Beginner] Java class field method / encapsulation (getter setter) [Note 25]
About Java StringBuilder class
[Java] Package for management
[Java] About Singleton Class
[Java] for statement / extended for statement
[Java] Data type ①-Basic type
Java inner class review
Countermeasures for Java OutOfMemoryError
Use OpenCV in Java
[Java, Kotlin] Type Variance
How to use Truth (assertion library for Java / Android)
Type determination in Java
Java study # 1 (typical type)
Java programming (class method)
Use variables for class and id names in haml
NLP for Java (NLP4J) (2)
About Java String class