• P168 实战练习(权限修饰符)


    创建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 }

    则运行代码为:

  • 相关阅读:
    28SQL 撤销索引、表以及数据库
    常见漏洞利用讲解
    6JavaScript 输出
    29_SQL ALTER TABLE 语句
    【首发】入门必看,性能测试指标详解,小白从零入门性能测试
    使用阿里云oss,在小程序端部分图片有时候显示,有时候不显示
    (0 , _auth.default) is not a function的问题
    uniapp开发小程序onReachBottom只触发一次
    Httprunner环境搭建
    4、vite创建vue项目
  • 原文地址:https://www.cnblogs.com/hanazawalove/p/5246795.html
Copyright © 2020-2023  润新知