寒假集训大作业(一)
寒假集训大作业(一)前言作业内容综合题思考题第七日课程容器类分类CollectionMap前言1月31日,布置了寒假集训大作业(本来计划在当天完成这篇博客,但是无奈容器类并没有能够在当天搞懂QAQ)作业内容综合题构思一个宠物店,用Java语言实现,体现面向对象思想。基本要求:体现面向对象特性:封装,继承,多态体现代码规范体现用户友好性(对用户输入做规范化处理利用Java集合类完成增删改查功能加分点
前言
无Bug和有脑洞真是不可兼得,害
作业内容
综合题
构思一个宠物店,用Java语言实现,体现面向对象思想。
- 基本要求:
体现面向对象特性:封装,继承,多态
体现代码规范
体现用户友好性(对用户输入做规范化处理
利用Java集合类完成增删改查功能 - 加分点:
绘制UML类图,推荐使用processon,mermaid,plantuml等工具
科学合理的类设计以及好玩的脑洞 - 提交要求:
提交源码,并编写README文档,附有设计思路,功能说明,运行截图,项目总结等。
思考题
宠物商店部分
源码
public class Cat extends Pet {
private int age;
private int sex;//0为雌性1为雄性
private String color;
private String special;
private String name;
private String like;
private boolean eat;//是否饥饿
private boolean mood;//是否开心
private long price;
public Cat() {
}
public Cat(int age, int sex, String color, String special, String name, long price) {
this.age = age;
this.sex = sex;
this.color = color;
this.special = special;
this.name = name;
this.price = price;
}
public Cat(String special) {
this.special = special;
}
public void shout(boolean mood) {
if (mood == true) {
System.out.println("喵呜");
} else {
System.out.println("噜噜噜");
}
}
@Override
public String getColor() {
return color;
}
@Override
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSpecial() {
return special;
}
public void setSpecial(String special) {
this.special = special;
}
public String getLike() {
return like;
}
public void setLike(String like) {
this.like = like;
}
public boolean isEat() {
return eat;
}
public void setEat(boolean eat) {
this.eat = eat;
}
public boolean isMood() {
return mood;
}
public void setMood(boolean mood) {
this.mood = mood;
}
@Override
public void moren() {
System.out.println("呜噜噜噜噜(好像在睡觉)");
}
@Override
public void shengqi() {
System.out.println("喵!(目露凶光)");
}
@Override
public void sajiao() {
System.out.println("呜!(蹭了蹭你)");
}
@Override
public void ele() {
System.out.println("喵~(摊在地上)");
}
@Override
public long getPrice() {
return price;
}
@Override
public void setPrice(long price) {
this.price = price;
}
@Override
public String getZhiCheng() {
return null;
}
}
import java.util.Calendar;
public class Date {
Calendar cal = Calendar.getInstance();
public int year = cal.get(Calendar.YEAR); //年
public int month = cal.get(Calendar.MONTH) + 1; //月
public int day = cal.get(Calendar.DAY_OF_MONTH); //日
public int hour = cal.get(Calendar.HOUR_OF_DAY); //小时
public int minute = cal.get(Calendar.MINUTE); //分
public int second = cal.get(Calendar.SECOND); //秒
//拼接成字符串
String date = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}
import java.util.Random;
public class DianYuan extends Pet {
private int age;
private int sex;//0为女性1为男性
private String color;//用于标记是否白班
private String special;//储存职称
private String name;
private String like;
private boolean eat;//是否饥饿
private boolean mood;//是否开心
private long price;
private String ZhiCheng;
boolean BaiBan;//是否上白班(6点到20点白班)
public DianYuan() {
}
public DianYuan(int age, int sex, String color, String special, String name) {
this.age = age;
this.sex = sex;
this.color = color;
this.special = special;
this.name = name;
}
@Override
public String getColor() {
return color;
}
@Override
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSpecial() {
return special;
}
public void setSpecial(String special) {
this.special = special;
}
public String getLike() {
return like;
}
public void setLike(String like) {
this.like = like;
}
public boolean isEat() {
return eat;
}
public void setEat(boolean eat) {
this.eat = eat;
}
public boolean isMood() {
return mood;
}
public void setMood(boolean mood) {
this.mood = mood;
}
@Override
public void moren() {
System.out.println("呜噜噜噜噜(好像在睡觉)");
}
@Override
public void shengqi() {
System.out.println("喵!(目露凶光)");
}
@Override
public void sajiao() {
System.out.println("呜!(蹭了蹭你)");
}
@Override
public void ele() {
System.out.println("喵~(摊在地上)");
}
@Override
public long getPrice() {
return price;
}
@Override
public void setPrice(long price) {
this.price = price;
}
@Override
public String getZhiCheng() {
return null;
}
}
public class Dog extends Pet {
private int age;
private int sex;//0为雌性1为雄性
private String color;
private String special;
private String name;
private String like;
private boolean eat;//是否饥饿
private boolean mood;//是否开心
private long price;
public Dog() {
}
public Dog(int age, int sex, String color, String special, String name, long price) {
this.age = age;
this.sex = sex;
this.color = color;
this.special = special;
this.name = name;
this.price = price;
}
public Dog(String special) {
this.special = special;
}
public void shout(boolean mood) {
if (mood == true) {
System.out.println("汪汪");
} else {
System.out.println("噜噜噜");
}
}
@Override
public String getColor() {
return color;
}
@Override
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSpecial() {
return special;
}
public void setSpecial(String special) {
this.special = special;
}
public String getLike() {
return like;
}
public void setLike(String like) {
this.like = like;
}
public boolean isEat() {
return eat;
}
public void setEat(boolean eat) {
this.eat = eat;
}
public boolean isMood() {
return mood;
}
public void setMood(boolean mood) {
this.mood = mood;
}
@Override
public void moren() {
System.out.println("呜噜噜噜噜(好像在睡觉)");
}
@Override
public void shengqi() {
System.out.println("呜呜~汪!(目露凶光)");
}
@Override
public void sajiao() {
System.out.println("汪~ha~ha~(摇尾巴)");
}
@Override
public void ele() {
System.out.println("呜汪~(摊在地上)");
}
@Override
public long getPrice() {
return price;
}
@Override
public void setPrice(long price) {
this.price = price;
}
@Override
public String getZhiCheng() {
return null;
}
}
public abstract class Pet implements PetMoods,PetTools{
private int age;
private int sex;//0为雌性1为雄性
private String name;
private String color;
private String special;
private String like;
private boolean eat;//是否饥饿
private boolean mood;//是否开心
private long price;
private String ZhiCheng;
boolean BaiBan;//是否上白班(6点到20点白班)
boolean ZaiDian;//是否在店里
public abstract String getZhiCheng();
}
public class PetCage { // 宠物商店
private Pet[] pets ; // 保存一组宠物
private int foot ;
public PetCage(int len){
if(len>0){
this.pets = new Pet[len] ; // 开辟数组大小
}else{
this.pets = new Pet[1] ; // 至少开辟一个空间
}
}
public boolean add(Pet pet){ // 增加的是一个宠物
if(this.foot<this.pets.length){
this.pets[this.foot] = pet ; // 增加宠物
this.foot ++ ;
return true ;
}else{
return false ;
}
}
public Pet[] search(String keyWord){
// 应该确定有多少个宠物符合要求
Pet p[] = null ;
int count = 0 ; // 记录下会有多少个宠物符合查询结果
for(int i=0;i<this.pets.length;i++){
if(this.pets[i]!=null){ // 表示此位置有宠物
if(this.pets[i].getName().indexOf(keyWord)!=-1
||this.pets[i].getColor().indexOf(keyWord)!=-1){
count++ ; // 修改查找到的记录数
}
}
}
p = new Pet[count] ; // 开辟指定的大小空间
int f = 0 ; // 增加元素的位置标记
for(int i=0;i<this.pets.length;i++){
if(this.pets[i]!=null){ // 表示此位置有宠物
if(this.pets[i].getName().indexOf(keyWord)!=-1
||this.pets[i].getColor().indexOf(keyWord)!=-1){
p[f] = this.pets[i] ;
f++ ;
}
}
}
return p ;
}
}
/**
* 情绪接口
* 包含:默认(趴着睡觉)、撒娇、生气、饿了四种情绪
*/
public interface PetMoods {
public void moren();
public void shengqi();
public void sajiao();
public void ele();
}
public interface PetTools {
public int getAge();
public int getSex();
public String getName();
public String getColor();
public String getSpecial();
public String getLike();
public boolean isEat();
public boolean isMood();
public long getPrice();
public void setAge(int age);
public void setSex(int sex);
public void setName(String name);
public void setColor(String color);
public void setSpecial(String special);
public void setLike(String like);
public void setEat(boolean eat);
public void setMood(boolean mood);
public void setPrice(long price);
}
/**
* 宠物商店系统
* 每次登入系统随机打折
* 白班店员和晚班店员不同
* 店里有一只不能卖的白猫,是店长的(实际上要是给好多钱还是会卖的)
* 每一只宠物都有自己的名字、性别、年龄、颜色
* 每一位店员都有年龄、性别、名称、上班时段
*/
import com.sun.istack.internal.localization.NullLocalizable;
import java.util.Random;
import java.util.Scanner;
public class Shop {
public static void main(String[] args) {
//以下添加宠物商店的基本宠物(店长的宠物)和三只可出售的宠物
PetCage pets = new PetCage(4);
pets.add(new Cat(1, 0, "白色", "白猫", "酸奶", 100000));
pets.add(new Cat(1, 1, "灰色", "灰猫", "憨憨", 2000));
pets.add(new Dog(2, 1, "黄色", "金毛", "狮子", 5000));
pets.add(new Dog(1, 0, "灰色", "哈士奇", "阿呆", 3000));
//以下添加宠物商店的店员
PetCage dianYuan = new PetCage(4);
dianYuan.add(new DianYuan(23, 0, "白班标记", "副店长", "白晶晶"));
dianYuan.add(new DianYuan(23, 1, "白班标记", "店长", "唐三"));
dianYuan.add(new DianYuan(19, 0, "晚班标记", "店员", "紫霞"));
dianYuan.add(new DianYuan(21, 1, "晚班标记", "保洁", "张三"));
//以下为基本菜单
System.out.println("==================欢迎来到宠物の家==================");
Date date = new Date();
int N = 9;
Random rand1 = new Random();
int a = rand1.nextInt(N);
if (a == 0) {
System.out.println("tip:据说店长很喜欢白猫,今天除了白猫,全场八折。");
}
if (a == 1 || a == 2) {
System.out.println("tip:据说小橘猫弄翻了猫砂,今天橘猫八折。");
}
if (a == 3 || a == 4 || a == 8) {
System.out.println("tip:据说有一只黑和店长的白猫打架了,今天黑猫八折。");
}
if (a == 5 || a == 6 || a == 7) {
System.out.println("tip:据说灰猫不让店长撸,今天灰猫八折。");
}
if (a == 1 || a == 3) {
}
if (a == 2 || a == 4) {
}
if (a == 5 || a == 7) {
}
if (a == 6) {
}
System.out.println(date.date);
boolean B;
if (date.hour < 20 && date.hour > 6) {
B = true;
System.out.println("现在是是白班时段");
Pet p[] = dianYuan.search("白班标记");
for (int i = 0; i < p.length; i++) {
System.out.println(p[i].getSpecial() + p[i].getName() + "在店营业");
}
} else {
System.out.println("现在是是晚班时间");
Pet p[] = dianYuan.search("晚班标记");
for (int i = 0; i < p.length; i++) {
System.out.println(p[i].getSpecial() + p[i].getName() + "在店营业");
}
}
//以下为功能操作模块
boolean key = true;
do {
System.out.println("***********************菜单***********************");
System.out.println("1.进入店长模式");
System.out.println("2.进入买家模式");
System.out.println("3.撸猫模式");
System.out.println("4.退出系统");
System.out.println("************************************************");
System.out.print("请输入选择:");
Scanner sc = new Scanner(System.in);
int model;
model = sc.nextInt();
if (model == 1) {//店长模式
boolean key1 = true;
do {
System.out.println("************************************************");
System.out.println("1.查询当前全部宠物");
System.out.println("2.丢弃宠物");
System.out.println("3.购进宠物");
System.out.println("4.给宠物改名");
System.out.println("5.退出");
System.out.println("************************************************");
System.out.print("请输入选择:");
int ch;
Scanner sc1 = new Scanner(System.in);
ch = sc1.nextInt();
switch (ch) {
case 1: {
printp(pets.search("色"));
break;
}
case 2: {
System.out.println("请输入丢弃宠物的名字");
String ch100;
Scanner sc100 = new Scanner(System.in);
ch100 = sc100.nextLine();
shanchu(pets.search(ch100));
break;
}
case 3: {
System.out.println("1.添加狗狗");
System.out.println("2.添加猫猫");
int ch1;
Scanner sc2 = new Scanner(System.in);
ch1 = sc2.nextInt();
switch (ch1) {
case 1: {
zengjia1(pets);
break;
}
case 2: {
zengjia2(pets);
break;
}
}
break;
}
case 4: {
System.out.println("请输入改名宠物的名字");
String ch100;
Scanner sc100 = new Scanner(System.in);
ch100 = sc100.nextLine();
gaiming(pets.search(ch100));
break;
}
case 5: {
key1 = false;
break;
}
default: {
System.out.println();
System.out.println("请正确选择模式");
System.out.println();
break;
}
}
} while (key1 == true);
} else if (model == 2) {//买家模式
boolean key2 = true;
do {
System.out.println("************************************************");
System.out.println("1.查询当前全部宠物");
System.out.println("2.查询指定");
System.out.println("3.购买指定宠物");
System.out.println("4.退出");
System.out.println("************************************************");
System.out.print("请输入选择:");
int ch2;
Scanner sc3 = new Scanner(System.in);
ch2 = sc3.nextInt();
switch (ch2) {
case 1: {
printp(pets.search("色"));
break;
}
case 2: {
System.out.println("请输入关键词");
String ch99;
Scanner sc99 = new Scanner(System.in);
ch99 = sc99.nextLine();
printp(pets.search(ch99));
break;
}
case 3: {
System.out.println("请输入想要购买的宠物的名字");
String ch99;
Scanner sc99 = new Scanner(System.in);
ch99 = sc99.nextLine();
System.out.println("您购买的宠物是");
printp(pets.search(ch99));
System.out.println("请加微信XXXXXX付款");
System.out.println("欢迎下次光临");
key = key2 = false;
break;
}
case 4: {
key2 = false;
break;
}
default: {
System.out.println();
System.out.println("请正确选择模式");
System.out.println();
break;
}
}
} while (key2 == true);
} else if (model == 4) {
key = false;
} else if (model == 3) {
System.out.println("你推开店内的一扇小门,走了进去");
System.out.println("里面有三只猫");
boolean mkey = true;
Cat cat1 = new Cat();
cat1.setColor("白色");
Cat cat2 = new Cat();
cat1.setColor("灰色");
Cat cat3 = new Cat();
cat1.setColor("黑色");
System.out.println("定下心神,你打算——");
int X, Y;
X = 0;
Y = 0;
do {
int N1 = 4;
Random rand2 = new Random();
int b = rand2.nextInt(N1);
Random rand3 = new Random();
int c = rand2.nextInt(N1);
Random rand4 = new Random();
int d = rand2.nextInt(N1);
System.out.println("1.向前走一步");
System.out.println("2.向左走一步");
System.out.println("3.向右走一步");
System.out.println("4.向后走一步");
System.out.println("5.离开");
int mm;
Scanner scm = new Scanner(System.in);
mm = scm.nextInt();
switch (mm) {
case 1: {
Y++;
if (Y > 0 && Y < 3) {
System.out.println("你向前走了一步");
System.out.println("白猫走在你的脚下");
switch (b) {
case 0: {
cat1.shengqi();
break;
}
case 1: {
cat1.moren();
break;
}
case 2: {
cat1.ele();
break;
}
case 3: {
cat1.sajiao();
break;
}
}
} else {
System.out.println("你撞到了墙");
}
break;
}
case 2: {
X--;
if (X > -4 && X < 4) {
System.out.println("你向左走了一步");
System.out.println("黑猫走在你的脚下");
switch (c) {
case 0: {
cat2.shengqi();
break;
}
case 1: {
cat2.moren();
break;
}
case 2: {
cat2.ele();
break;
}
case 3: {
cat2.sajiao();
break;
}
}
} else {
System.out.println("你撞到了墙");
}
break;
}
case 3: {
X++;
if (X > -4 && X < 4) {
System.out.println("你向右走了一步");
System.out.println("灰猫走在你的脚下");
switch (d) {
case 0: {
cat3.shengqi();
break;
}
case 1: {
cat3.moren();
break;
}
case 2: {
cat3.ele();
break;
}
case 3: {
cat3.sajiao();
break;
}
}
} else {
System.out.println("你撞到了墙");
break;
}
break;
}
case 4: {
Y--;
if (Y <= 0) {
System.out.println("你退了出去");
mkey = false;
break;
} else {
System.out.println("三只猫一起走到了你的脚下,蹭了蹭你的裤脚");
switch (b) {
case 0: {
cat1.shengqi();
break;
}
case 1: {
cat1.moren();
break;
}
case 2: {
cat1.ele();
break;
}
case 3: {
cat1.sajiao();
break;
}
}
switch (d) {
case 0: {
cat3.shengqi();
break;
}
case 1: {
cat3.moren();
break;
}
case 2: {
cat3.ele();
break;
}
case 3: {
cat3.sajiao();
break;
}
}
switch (c) {
case 0: {
cat2.shengqi();
break;
}
case 1: {
cat2.moren();
break;
}
case 2: {
cat2.ele();
break;
}
case 3: {
cat2.sajiao();
break;
}
}
}
break;
}
case 5: {
mkey = false;
break;
}
default: {
System.out.println("你做了一个奇怪的动作,摔倒了");
System.out.println("等你醒来时,发现自己正躺在宠物店的地上,一只白猫正在用看傻子的眼神看你");
System.out.println("请正确选择动作");
break;
}
}
} while (mkey == true);
} else {
System.out.println();
System.out.println("请正确选择模式");
System.out.println();
}
} while (key);
}
public static void printp(Pet p[]) {
for (int i = 0; i < p.length; i++) {
if (p[i] != null) {
String XingBie;
if (p[i].getSex() == 1) {
XingBie = "公";
} else {
XingBie = "母";
}
System.out.println(p[i].getAge() + "岁的" + p[i].getColor() + XingBie + p[i].getSpecial()
+ p[i].getName() + "价格是" + p[i].getPrice() + "编号是" + i);
System.out.println();
}
}
}
public static void shanchu(Pet p[]) {
for (int i = 0; i < p.length; i++) {
if (p[i] != null) {
p[i].setAge(0);
p[i].setSex(0);
p[i].setSpecial(null);
p[i].setName(null);
p[i].setColor(null);
p[i].setPrice(0);
}
}
}
public static void zengjia1(PetCage pets) {
Scanner sc = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
Scanner sc3 = new Scanner(System.in);
Scanner sc4 = new Scanner(System.in);
Scanner sc5 = new Scanner(System.in);
Scanner sc6 = new Scanner(System.in);
Scanner sc7 = new Scanner(System.in);
int sex, age, prize;
String special, name, color;
System.out.println("请输入年龄");
age = sc.nextInt();
System.out.println("请输入性别(公为1,母为0)");
sex = sc1.nextInt();
System.out.println("请输入品种");
special = sc3.nextLine();
System.out.println("请输入名字");
name = sc4.nextLine();
System.out.println("请输入颜色(以色字结尾)");
color = sc5.nextLine();
System.out.println("请输入价格");
prize = sc.nextInt();
pets.add(new Dog(age, sex, color, special, name, prize));
}
public static void zengjia2(PetCage pets) {
Scanner sc = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
Scanner sc3 = new Scanner(System.in);
Scanner sc4 = new Scanner(System.in);
Scanner sc5 = new Scanner(System.in);
Scanner sc6 = new Scanner(System.in);
Scanner sc7 = new Scanner(System.in);
int sex, age, prize;
String special, name, color;
System.out.println("请输入年龄");
age = sc.nextInt();
System.out.println("请输入性别(公为1,母为0)");
sex = sc1.nextInt();
System.out.println("请输入品种");
special = sc3.nextLine();
System.out.println("请输入名字");
name = sc4.nextLine();
System.out.println("请输入颜色");
color = sc5.nextLine();
System.out.println("请输入价格");
prize = sc.nextInt();
pets.add(new Cat(age, sex, color, special, name, prize));
}
public static void gaiming(Pet p[]) {
for (int i = 0; i < p.length; i++) {
if (p[i] != null) {
Scanner sc = new Scanner(System.in);
String special, name, color;
System.out.println("请输入名字");
name = sc.nextLine();
p[i].setName(name);
}
}
}
}
设计思路
首先宠物店里肯定不只有宠物,还要有店员。
宠物有性别、颜色、种类、名字、价格、心情。
店员分白班晚班,有职位,有名字和性别。
宠物有猫和狗两大类(别的动物也一样,只要继承Pet父类就好了)
使用该系统的一般有两种人,买家和卖家
买家不一定来了就买,可能是来撸猫的(比如我)
所以大致分为两个个板块——基本板块(包括买家模式和卖家模式)和撸猫板块
功能说明
每次登录系统随机打折
增删改查——增就是增加宠物,删就是删除宠物,改的话,性别年龄种类颜色总不能改吧,所以就是改名,查的话就是关键词搜索。
但是增删改查除了查,应该都是店员的工作而不是买家的事,所以分成了卖家和买家两个功能分区
每次登入系统会随机打折
买家模式:查询全部宠物,定向查询宠物,购买宠物
卖家模式:购进宠物,宠物改名,丢弃宠物,查询全部宠物
ps:最后新增了撸猫模式…
基本模式
包含店长模式和买家模式,两种模式均可查看全部库存
店长模式可以丢猫改名和买进
买家模式可以定向查询(模糊查询指定宠物)
撸猫模式
之前这个模式还没搞好,所以分开了…
进入宠物店内一个6*3大小的房间,里面有三只猫,会根据你的动作改变情绪进行互动
走过头了会撞墙,不按要求选择动作会摔倒
项目总结
原本还想加入很多功能(从自己源码里可以看得出痕迹),但是最终没bug和有脑洞不可兼得,放弃了。
UML类图
博客链接
https://blog.csdn.net/weixin_52292970?spm=1011.2124.3001.5343
有几篇在草稿箱,还在修改
思考题
1.
看一下源码
int 一个数时如果在这个范围内时
直接从数组内调用,不用new一个对象
128超出了,所以要new,导致哈希值不一样
具体为什么应该是可以节省运行时间,跟容量池作用一样
全部源码如下
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer[] cache;
static Integer[] archivedCache;
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
h = Math.max(parseInt(integerCacheHighPropValue), 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(h, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
// Load IntegerCache.archivedCache from archive, if possible
//(如果可能,从存档中加载整数缓存)
VM.initializeFromArchive(IntegerCache.class);
int size = (high - low) + 1;
// Use the archived cache if it exists and is large enough
//(如果存档缓存存在且足够大,则使用存档缓存)
if (archivedCache == null || size > archivedCache.length) {
Integer[] c = new Integer[size];
int j = low;
for(int i = 0; i < c.length; i++) {
c[i] = new Integer(j++);
}
archivedCache = c;
}
cache = archivedCache;
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
2.
QAQ因为自己不会,就去B站找课程了,然后找到了讲底层的课。(所以相当于是别人教我的,不能完全算自己的思考)
首先,这个是int类型的指令
创建代码:
public class Text2{
public static void main(String[] args){
int i=1;
}
}
我们找到类的字节码所在的文件,在doc窗口执行以下命令
javap -c Text2.class
得到
Compiled from "Text2.java"
public class Text2 {
public Text2();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: iconst_1 //我们看这两行
1: istore_1 //我们看这两行
2: return
}
看到代码中标记出的两行
将一个常量加载到操作数栈和将一个数值从操作数栈存储到局部变量表还有其他的以写指令
再看我放的第一张图,可知运行int i = 1;这条语句的过程中,JVM先将1压入操作数栈,然后将其储存到了局部变量表
更多推荐
所有评论(0)