Java 17 Recipes
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
- Bu sahifa navigatsiya:
- 5-14. Increasing Class Encapsulation Problem
How It Works
Abstract classes are labeled as such, and they contain field declarations and methods that can be used within subclasses. What makes them different from a regular class is that they contain abstract methods, which are method declarations with no implementation. The solution to this recipe contains an abstract method named calculateDaysPlayed(). Abstract classes may or may not contain abstract methods. They can contain fields and fully implemented methods as well. Abstract classes cannot be instantiated; other classes can only extend them. When a class extends an abstract class, it gains all the fields and functionality of the abstract class. However, any abstract methods declared within the abstract class must be implemented by the subclass. You may wonder why the abstract class wouldn’t just contain the implementation of the method so that it was available for all its subclasses to use. If you think about the concept, it makes perfect sense. One type of object may perform a task differently from another. Using an abstract method forces the class extending the abstract class to implement it, but it allows the ability to customize how it is implemented. 5-14. Increasing Class Encapsulation Problem One of your classes requires the use of another class’s functionality. However, no other class requires the use of that same functionality. Rather than creating a separate class that includes this additional functionality, you’d like to generate an implementation that can only be used by the class that needs it while placing the code in a logical location. Chapter 5 ObjeCt-Oriented java 200 Solution Create an inner class within the class that requires its functionality. import java.util.ArrayList; import java.util.List; /** * Inner class example. This example demonstrates how a team object could be * built using an inner class object. * * @author juneau */ public class TeamInner { private Player player; private List playerList; private int size = 4; /** * Inner class representing a Player object */ class Player { private String firstName ; private String lastName ; private String position ; private int status = -1; public Player() { } public Player(String position, int status) { this.position = position; this.status = status; } protected String playerStatus() { String returnValue = null; switch (getStatus()) { Chapter 5 ObjeCt-Oriented java 201 case 0: returnValue = "ACTIVE"; break; case 1: returnValue = "INACTIVE"; break; case 2: returnValue = "INJURY"; break; default: returnValue = "ON_BENCH"; break; } return returnValue; } public String playerString() { return getFirstName() + " " + getLastName() + " - " + getPosition(); } /** * @return the firstName */ public String getFirstName() { return firstName; } /** * @param firstName the firstName to set */ public void setFirstName(String firstName) { this.firstName = firstName; } /** * @return the lastName */ Chapter 5 ObjeCt-Oriented java 202 public String getLastName() { return lastName; } /** * @param lastName the lastName to set */ public void setLastName(String lastName) { this.lastName = lastName; } /** * @return the position */ public String getPosition() { return position; } /** * @param position the position to set */ public void setPosition(String position) { this.position = position; } /** * @return the status */ public int getStatus() { return status; } /** * @param status the status to set */ public void setStatus(int status) { this.status = status; } @Override public String toString(){ Chapter 5 ObjeCt-Oriented java 203 return this.firstName + " " + this.lastName + " - "+ this.position + ": " + this.playerStatus(); } } public TeamInner() { final int ACTIVE = 0; // In reality, this would probably read records from a database using // a loop...but for this example we will manually enter the player data. playerList = new ArrayList(); playerList.add(constructPlayer("Josh", "Juneau", "Right Wing", ACTIVE)); playerList.add(constructPlayer("Joe", "Blow", "Left Wing", ACTIVE)); playerList.add(constructPlayer("John", "Smith", "Center", ACTIVE)); playerList.add(constructPlayer("Bob","Coder", "Defense", ACTIVE)); playerList.add(constructPlayer("Jonathan", "Gennick", "Goalie", ACTIVE)); } public Player constructPlayer(String first, String last, String position, int status){ Player player = new Player(); player.firstName = first; player.lastName = last; player.position = position; player.status = status; return player; } public List getPlayerList() { return this.playerList; } Chapter 5 ObjeCt-Oriented java 204 public static void main(String[] args) { TeamInner inner = new TeamInner(); System.out.println("Team Roster"); System.out.println("==========="); for(Player player:inner.getPlayerList()){ System.out.println(player.playerString()); } } } The result of running this code lists the players on the team. Team Roster =========== Josh Juneau - Right Wing Joe Blow - Left Wing John Smith - Center Bob Coder - Defense Jonathan Gennick - Goalie Download 3.2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling