Зміст курсу
Java Extended
Java Extended
Complex Classes Usage
Using class objects in another class
Now we will discuss the more complex usage of classes, specifically using class objects within another class.
Team and players
Let's imagine a situation where we have a Team
class. Every team should have players. We could fill the players
field with simple String
values representing their names, but that wouldn't be the best practice. It would be much better to create a Player
class with its own fields and methods and then create an array of Player
objects within the Team
class.
Let's consider an example:
Team
class Team { String title; String country; Player[] players; void play() { System.out.println("Team " + title + "is playing!"); } } class Player { String name; int age; int yearsOfExperience; public Player(String name, int age, int yearsOfExperience) { this.name = name; this.age = age; this.yearsOfExperience = yearsOfExperience; } void introduce() { System.out.println("Hi, my name is " + name + ", I am " + age + " years old and have " + yearsOfExperience + " years of experience"); } }
As you can see, we are using an array of Player
objects in the players
field of the Team
class. From this, we can draw a few conclusions:
- We can create arrays of objects from our own created class;
- We can use objects of one class within another class to improve the overall logic.
But the question arises: How do we fill this array?
Answer: To do this, we need to create several Player
objects and add them to the array of players. Let's create a Dream Team in the main method and see an example:
Main
package com.example; import java.util.Arrays; public class Main { public static void main(String[] args) { Player bob = new Player("Bob", 32, 5); Player alice = new Player("Alice", 28, 8); Player john = new Player("John", 41, 20); Player[] players = {bob, alice, john}; Team dreamTeam = new Team("Dream Team", "USA", players); System.out.println(dreamTeam); } } class Team { String title; String country; Player[] players; public Team(String title, String country, Player[] players) { this.title = title; this.country = country; this.players = players; } void play() { System.out.println("Team " + title + "is playing!"); } @Override public String toString() { return "Team{" + "title='" + title + '\'' + ", country='" + country + '\'' + ", players=" + Arrays.toString(players) + '}'; } } class Player { String name; int age; int yearsOfExperience; public Player(String name, int age, int yearsOfExperience) { this.name = name; this.age = age; this.yearsOfExperience = yearsOfExperience; } void introduce() { System.out.println("Hi, my name is " + name + ", I am " + age + " years old and have " + yearsOfExperience + " years of experience"); } @Override public String toString() { return "Player{" + "name='" + name + '\'' + ", age=" + age + ", yearsOfExperience=" + yearsOfExperience + '}'; } }
We created 3 objects of the Player
class and initialized their fields through the constructor. Then we create an array with the Player
type and add Bob, Alice, and John to it.
Next, we create a Team
object and initialize its fields through the constructor. We initialize the players[]
field with the previously created array.
We print the object to the console and see that the Team
object has Player
objects in the output.
Owner and pet
Let's look at another simpler example. Let's say we have an Owner
and a Pet
. We will create a separate class for each.
The Pet
class will have only one field - String name
.
The Owner
class will have two fields - String name
and Pet pet
.
Pet
class Pet { String name; public Pet(String name) { this.name = name; } } class Owner { String name; Pet pet; public Owner(String name, Pet pet) { this.name = name; this.pet = pet; } }
As you can see, these two classes are also connected, as the Owner
class has a field of type Pet
(which is the class we created ourselves!).
Дякуємо за ваш відгук!