创建Game类,运行代码如下:

 package org.hanqi.pn0120;

 public class Game {

     private String name;
private String category;
private int totalcost = 10000; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public int getTotalcost() {
return totalcost;
}
public void setTotalcost(int totalcost) {
this.totalcost = totalcost;
} public Game(String name, String category) {
super();
this.name = name;
this.category = category;
} public void cost1(int cost)
{
if(cost <= 0)
{
System.out.println("做游戏的钱呢?!");
}
else if(cost>this.totalcost)
{
int excess=cost-this.totalcost;
System.out.println("超出预算了,超了"+excess+"金额的预算");
}
else
{
this.totalcost=cost;
System.out.println("这次预算是"+this.totalcost);
}
} private int sales;
public int getSales()
{
return this.sales;
}
public void money(int number,int price)
{
if(number<=0)
{
System.out.println("一份都没卖出去,赔钱了,公司要倒闭");
}
else if(price<=0)
{
System.out.println("为什么定价是0?");
}
else if(price>50)
{
System.out.println("定价太高了");
}
else
{
this.sales=number*price;
System.out.println("销售额是"+sales); if(this.sales<this.totalcost)
{
int debt = this.totalcost-this.sales;
System.out.println("入不敷出,公司倒闭,欠了 "+debt+" 金额的债务");
}
else
{
int profit = this.sales-this.totalcost;
System.out.println("挣钱了,利润是"+profit);
}
}
} public static void main(String[]args)
{
Game myGame = new Game("你与我的星空","Galgame");
System.out.println("游戏名称是:"+myGame.getName());
System.out.println("游戏类型为:"+myGame.getCategory());
myGame.cost1(-10000);
myGame.cost1(20000);
myGame.cost1(2000);
myGame.money(0, 25);
myGame.money(14, 0);
myGame.money(200, 88);
myGame.money(200, 25);
}
}

则运行代码为:

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

05-11 01:41