构建标准JavaBean快捷方式
快捷键:alt+insert(+Fn)->构造函数->全选构造带参方法,不选择则空参构造
插件:ptg,安装后右键单击,倒数第三个则为一键构造所有set/get函数
数据类型
基本数据类型:数据值存储在自己的空间中
特点:赋值给其他变量也是赋的真实的值
引用数据类型:数据存储在其他空间中,自己空间中存储的是地址值
特点:赋值给其他变量赋的是地址值
如:
Student s=new Student();//s为引用数据,其存储的是一个结构体的地址
s.name="李明";//结构体内name为基本数据类型,其值存储在自己空间中
小练习-简单版文字游戏
public class Game {
public static void main(String[] args){
People p1=new People("小兵1号",100);
People p2=new People("小兵2号",100);
while(p1.getBlood()>0&&p2.getBlood()>0){
p1.attack(p2);
p2.attack(p1);
}
}
}
import java.util.Random;
public class People {
private String name;
private int blood;
public People() {
}
public People(String name, int blood) {
this.name = name;
this.blood = blood;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public String toString() {
return "People{name = " + name + ", blood = " + blood + "}";
}
public void attack(People role){
Random r=new Random();
int damage=r.nextInt(25)+1;
int blood2=role.getBlood()-damage;
role.setBlood(blood2);
System.out.printf("%s攻击了%s,造成了%d点伤害。\n",this.getName(),role.getName(),damage);
if (role.getBlood()<=0) System.out.printf("%s死亡,游戏结束,%s胜利!\n",role.getName(),this.getName());
else {
System.out.printf("%s剩余血量为%d\n", role.getName(), role.getBlood());
}
}
}
对象数组
和c/c++的结构体数组差不多
public class Good {
private String name;
private String kind;//种类
private int price;//价格
private int amount;//数量
public Good() {
}
public Good(String name, String kind, int price, int amount) {
this.name = name;
this.kind = kind;
this.price = price;
this.amount = amount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String toString() {
return "Good{name = " + name + ", kind = " + kind + ", price = " + price + ", amount = " + amount + "}";
}
}
public class Main {
public static void main(String[] args) {
Good[] arr=new Good[3];
Good g1=new Good("巧克力","食品",15,50);
Good g2=new Good("笔记本","文具",8,90);
Good g3=new Good("短裙","衣物",79,20);
arr[0]=g1;
arr[1]=g2;
arr[2]=g3;
for (int i=0;i<3;i++) System.out.println(arr[i].getPrice());
}
}
其余对象数组的查找、删除、插入操作与c/c++类似