package com.hanqi; public class Cat { private static int counter=0; public Cat(String title){ System.out.println("售出图书:"+title); counter++; } public static int getCounter(){ return counter; } }}
package com.hanqi; import java.util.Random; import java.util.Scanner; public class Test { public static void main(String[] args) { // TODO 自动生成的方法存根 String titles[]={ "入门精通","编程词典","视频学习" }; for(int i=0;i<6;i++){ new Cat(titles[new Random().nextInt(3)]); } System.out.println("总计销售了"+Cat.getCounter()+"本书"); }}