Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet97/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   93   94   95   96   97   98   99   100   ...   245
Bog'liq
Java 17 Recipes

 Solution
Create a JavaBean that represents the object that you want to create. JavaBean objects 
provide the capability for object fields to be declared as private, and they also allow 
the attributes to be read and updated so that an object can be passed around and used 
within an application. This recipe demonstrates the creation of a JavaBean named Team. 
The Team object contains a few different fields that can contain information.
public class Team implements TeamType {
private List
players;
private String name;
private String city;
/**
* @return the players
*/
public List
getPlayers() {
return players;
}
/**
* @param players the players to set
*/
@Override
public void setPlayers(List
players) {
this.players = players;
}
/**
* @return the name
Chapter 5 ObjeCt-Oriented java


166
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
@Override
public void setName(String name) {
this.name = name;
}
/**
* @return the city
*/
public String getCity() {
return city;
}
/**
* @param city the city to set
*/
@Override
public void setCity(String city) {
this.city = city;
}
}
The PlayerType interface is:
public interface TeamType {
void setPlayers(List
players);
void setName(String name);
void setCity(String city);
}
As you can see, the object in this solution contains three fields, and each of those 
fields is declared as private. However, each field has two accessor methods
―getters 
and setters
―that allow the fields to be indirectly accessible.
Chapter 5 ObjeCt-Oriented java


167

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   93   94   95   96   97   98   99   100   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling