P168 실전 연습(권한 수식자)

13402 단어
다음과 같은 실행 코드를 사용하여 Game 클래스를 만듭니다.
  1 package org.hanqi.pn0120;
  2 
  3 public class Game {
  4 
  5     private String name;
  6     private String category;
  7     private int totalcost = 10000;
  8     
  9     public String getName() {
 10         return name;
 11     }
 12     public void setName(String name) {
 13         this.name = name;
 14     }
 15     public String getCategory() {
 16         return category;
 17     }
 18     public void setCategory(String category) {
 19         this.category = category;
 20     }
 21     public int getTotalcost() {
 22         return totalcost;
 23     }
 24     public void setTotalcost(int totalcost) {
 25         this.totalcost = totalcost;
 26     }
 27     
 28     public Game(String name, String category) {
 29         super();
 30         this.name = name;
 31         this.category = category;        
 32     }    
 33     
 34     public void cost1(int cost)
 35     {
 36         if(cost <= 0)
 37         {
 38             System.out.println("      ?!");            
 39         }
 40         else if(cost>this.totalcost)
 41         {
 42             int excess=cost-this.totalcost;
 43             System.out.println("     ,  "+excess+"     ");
 44         }
 45         else
 46         {
 47             this.totalcost=cost;
 48             System.out.println("     "+this.totalcost);            
 49         }            
 50     }    
 51     
 52     private int sales;
 53     public int getSales()
 54     {
 55         return this.sales;
 56     }
 57     public void money(int number,int price)
 58     {
 59         if(number<=0)
 60         {
 61             System.out.println("       ,   ,     ");
 62         }
 63         else if(price<=0)
 64         {
 65             System.out.println("      0?");
 66         }
 67         else if(price>50)
 68         {
 69             System.out.println("     ");
 70         }
 71         else
 72         {
 73             this.sales=number*price;
 74             System.out.println("    "+sales);
 75         
 76         if(this.sales<this.totalcost)
 77         {
 78             int debt = this.totalcost-this.sales;
 79             System.out.println("    ,    ,   "+debt+"      ");
 80         }
 81         else
 82         {
 83             int profit = this.sales-this.totalcost;
 84             System.out.println("   ,   "+profit);
 85         }
 86         }
 87     }
 88     
 89     public static void main(String[]args)
 90     {
 91         Game myGame = new Game("      ","Galgame");
 92              System.out.println("     :"+myGame.getName());
 93              System.out.println("     :"+myGame.getCategory());
 94              myGame.cost1(-10000);             
 95              myGame.cost1(20000);
 96              myGame.cost1(2000);
 97              myGame.money(0, 25);
 98              myGame.money(14, 0);
 99              myGame.money(200, 88);
100              myGame.money(200, 25);             
101     }    
102 }

실행 코드는 다음과 같습니다.

좋은 웹페이지 즐겨찾기