Class类是反射的基石,Class是一个类,封装了当前对象所对应类的信息。
此处新建一个安卓项目,新建一个MysteryBox类:
MysteryBox类代码如下:
package com.example.studyfour;
import java.util.Random;
public class MysteryBox {
private final String content;
private boolean isOpened;
public final int price;
private final String brand;
public MysteryBox(){
//无参的构造函数
this.price = 10;
this.brand = "手办盲盒";
isOpened = false;
int random = new Random().nextInt();
if (random % 100 == 1) {
content = "隐藏款";
} else {
content = "普通款";
}
}
private MysteryBox(String brand){
//有一个参数的构造函数
this.brand = brand;
this.price = 10;
isOpened = false;
int random = new Random().nextInt();
if (random % 100 == 1) {
content = "隐藏款";
} else {
content = "普通款";
}
}
public MysteryBox(int price){
//有一个参数的构造函数
this.brand = "手办盲盒";
this.price = price;
isOpened = false;
int random = new Random().nextInt();
int p = 100;
if (price > 100) {
p = 10;
}
if (random % p == 1) {
content = "隐藏款";
} else {
content = "普通款";
}
}
public MysteryBox(int price ,String brand)


6422

被折叠的 条评论
为什么被折叠?



