类和对象的基本概念
封装
Private权限修饰符
this关键字
构造方法:
标准的javaBean类
插件PTG可以一键生成javabean标准类
成员变量和局部变量
练习:文字版格斗游戏
Role类
import java.util.Random;
public class Role {
private String name;
private int blood;
//空参
public Role(){}
//全参
public Role(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 void attack(Role role){//方法的调用者去攻击参数,参数是对象,而不是一个name了
Random rd=new Random();
int hurt=rd.nextInt(20);
//剩余血量
int remainblood=role.getBlood()-hurt;
remainblood=remainblood<0?0:remainblood;//剩余血量如果为负数了,就修改为0
role.setblood(remainblood);//这里要修改一下,被揍的人的血量,其血量为当前所剩的血量
System.out.println(this.name+"举起拳头打了"+role.name+"一下,造成了"+hurt+"点伤害,"+role.getName()+"还剩下"+remainblood+"点血");
}
}
test类
public class test {
public static void main(String[] args) {
Role role1 = new Role("乔峰", 100);
Role role2 = new Role("鸠摩智", 100);
while(true){
role1.attack(role2);
if(role2.getBlood()==0){
System.out.println(role1.getName()+"K.O了"+role2.getName());
break;
}
role2.attack(role1);
if(role1.getBlood()==0){
System.out.println(role2.getName()+"K.O了"+role1.getName());
break;
}
}
}
}
文字版格斗游戏升级版(加招式,性别,样貌)
Role类
import java.util.Random;
public class Role {
private String name;
private int blood;
private char gender;
private String face;//长相是随机的
String[] boyface ={"风流俊雅", "气宇轩昂","面目狰狞"};
String[] girlface={"美轮美奂","沉鱼落雁","相貌丑陋"};
String[] attacks_desc={
"%s使出了一招【背心钉】,转到对方的身后,一掌向%s背心的灵台穴拍去。",
"%s使出了一招【游空】,飞起身形子半空中变掌为抓锁向%s。",
"%s大喝一声,一招【劈裂坠地】,锤向%s双腿",
};
String[] injureds_desc={
"结果%s退了半步,毫发无伤",
"结果给%s造成了一处瘀伤",
};
//空参
public Role(){}
//全参
public Role(String name,int blood,char gender){
this.name=name;
this.blood=blood;
this.gender=gender;
setFace(gender);
//this.face=face;随机长相,所以不是在创建对象的时候给的
}
public char getGender(){
return gender;
}
public void setgender(){
this.gender=gender;
}
public String getFace(){
return face;
}
public void setFace(char gender){
Random rd=new Random();
if(gender =='男'){
int index=rd.nextInt(boyface.length);
this.face=boyface[index];
}
else if(gender=='女'){
int index=rd.nextInt(girlface.length);
this.face=girlface[index];
}
else{
this.face="面目狰狞";
}
}
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 void attack(Role role){//方法的调用者去攻击参数,参数是对象,而不是一个name了
Random r=new Random();
int index2=r.nextInt(attacks_desc.length);
String kunfu=attacks_desc[index2];
//输出一个攻击效果
System.out.printf(kunfu,this.getName(),role.getName());//souf没有换行效果
System.out.println();
int hurt=r.nextInt(20);
//剩余血量
int remainblood=role.getBlood()-hurt;
remainblood=remainblood<0?0:remainblood;//剩余血量如果为负数了,就修改为0
role.setblood(remainblood);//这里要修改一下,被揍的人的血量,其血量为当前所剩的血量
//受伤效果的描述
//血量>90 0索引描述
if(remainblood>90){
System.out.printf(injureds_desc[0],role.getName());//此处要有souf,其中%s表示占位,后面的参数表示填充进去
}else{
System.out.printf(injureds_desc[1],role.getName());
}
System.out.println();
//System.out.println(this.name+"举起拳头打了"+role.name+"一下,造成了"+hurt+"点伤害,"+role.getName()+"还剩下"+remainblood+"点血");
}
public void showRoleinfo(){
System.out.println("姓名为:"+getName());
System.out.println("血量为:"+getBlood());
System.out.println("性别为:"+getGender());
System.out.println("长相为:"+getFace());
}
}
test类
public class test {
public static void main(String[] args) {
Role role1 = new Role("乔峰", 100, '男');
Role role2 = new Role("鸠摩智", 100, '男');
role1.showRoleinfo();
role2.showRoleinfo();
while(true){
role1.attack(role2);
if(role2.getBlood()==0){
System.out.println(role1.getName()+"K.O了"+role2.getName());
break;
}
role2.attack(role1);
if(role1.getBlood()==0){
System.out.println(role2.getName()+"K.O了"+role1.getName());
break;
}
}
}
}
练习:对象数组1
Goods类
public class Goods {
private String id;
private String name;
private int price;
private int remain;
public Goods(){
}
public Goods(String id,String name,int price,int remain){
this.id=id;
this.name=name;
this.price=price;
this.remain=remain;
}
public String getId(){
return id;
}
public void setId(){
this.id=id;
}
public String getName(){
return name;
}
public void setName(){
this.name=name;
}
public int getPrice(){
return price;
}
public void setPrice(){
this.price=price;
}
public int getRemain(){
return remain;
}
public void setRemain(){
this.remain=remain;
}
}
test类
public class test {
public static void main(String[] args) {
//创建一个对象数组
Goods[] arr=new Goods[3];
//创建三个商品对象
Goods g1=new Goods("001","华为p40",2000,100);
Goods g2=new Goods("002","保温杯",100,50);
Goods g3=new Goods("003","鼠标",200,20);
//把商品添加到数组中
arr[0]=g1;
arr[1]=g2;
arr[2]=g3;
//遍历
for (int i = 0; i < arr.length; i++) {
Goods goods=arr[i];//创建一个新对象用来接收对象数组中的对象
System.out.println(goods.getId()+","+goods.getName()+","+goods.getPrice()+","+goods.getPrice());
}
}
}
键盘录入
第一套体系:
nextInt();接收整数
nextDouble();接收小数
next();接收字符串
//第一套体系的特点:遇到空格,回车就停止接收,这些符号后面的数据就不会被接收了
第二套体系:
nextLine();接收字符串
//可以接收空格,制表符,遇到回车才停止接收数据
//注意两套键盘录入体系最好不要混用,先以第一套体系为主
练习:对象数组2
Car类
public class Car {
private String brand;
private int price;
private String color;
public Car(){
}
public Car( String brand, int price, String color){
this.brand=brand;
this.price=price;
this.color=color;
}
public String getBrand(){
return brand;
}
public void setBrand(String brand){
this.brand=brand;
}
public int getPrice(){
return price;
}
public void setPrice(int price){//没有返回值用void,set方法要传参数
this.price=price;
}
public String getColor(){
return color;
}
public void setColor(String color){
this.color=color;
}
}
test类
import java.util.Scanner;
public class test {
public static void main(String[] args) {
Car[] arr=new Car[3];
Scanner sc=new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
Car c=new Car();
System.out.println("请输入汽车品牌");
String brand=sc.next();
c.setBrand(brand);
System.out.println("请输入汽车的价格");
int price=sc.nextInt();
c.setPrice(price);
System.out.println("请输入汽车的颜色");
String color=sc.next();
c.setColor(color);
arr[i]=c;
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i].getBrand()+","+arr[i].getPrice()+","+arr[i].getColor());
}
}
}
练习:对象数组3(求平均价格)
Phone类
package day2;
public class test {
public static void main(String[] args) {
GirlFriend[] arr=new GirlFriend[4];
GirlFriend g1=new GirlFriend("小芳",29,'女',"唱歌");
GirlFriend g2=new GirlFriend("小菜",26,'女',"hipop");
GirlFriend g3=new GirlFriend("圆圆",18,'女',"跳舞");
GirlFriend g4=new GirlFriend("方方",23,'女',"踢足球");
arr[0]=g1;
arr[1]=g2;
arr[2]=g3;
arr[3]=g4;
int sum=0;
int average=0;
for (int i = 0; i < arr.length; i++) {
sum+=arr[i].getAge();
}
average=sum/arr.length;
System.out.println("女朋友的平均年龄是"+average);
int count=0;
for (int i = 0; i < arr.length; i++) {
if(arr[i].getAge()<average){
arr[i].showInfo();
count++;
}
}
System.out.println("比平均年龄小的共有"+count+"个");
}
}
test类
public class test {
public static void main(String[] args) {
Phone[] arr=new Phone[3];//定义对象数组
//创建手机对象
Phone phone1=new Phone("小米",1999,"粉色");//实例化对象
Phone phone2=new Phone("华为",4999,"白色");
Phone phone3=new Phone("魅族",3999,"黑色");
int sum=0;
double average=0.0;
//把手机对象添加到数组中
arr[0]=phone1;
arr[1]=phone2;
arr[2]=phone3;
for (int i = 0; i < arr.length; i++) {
sum+=arr[i].getPrice();
}
average=sum*1.0/arr.length;
System.out.println(average);
}
}
练习:对象数组4(定义女朋友对象)
GirlFriend类
package day2;
public class GirlFriend {
private String name;
private int age;
private char gender;
private String hobby;
public GirlFriend(){ }
public GirlFriend(String name,int age,char gender,String hobby){
this.name=name;
this.age=age;
this.gender=gender;
this.hobby=hobby;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
public char getGender(){
return gender;
}
public void setGender(char gender){
this.gender=gender;
}
public String getHobby(){
return hobby;
}
public void setHobby(String hobby){
this.hobby=hobby;
}
public void showInfo(){
System.out.println(this.name+","+this.age+","+this.gender+","+this.hobby);
}
}
test类
package day2;
public class test {
public static void main(String[] args) {
GirlFriend[] arr=new GirlFriend[4];
GirlFriend g1=new GirlFriend("小芳",29,'女',"唱歌");
GirlFriend g2=new GirlFriend("小菜",26,'女',"hipop");
GirlFriend g3=new GirlFriend("圆圆",18,'女',"跳舞");
GirlFriend g4=new GirlFriend("方方",23,'女',"踢足球");
arr[0]=g1;
arr[1]=g2;
arr[2]=g3;
arr[3]=g4;
int sum=0;
int average=0;
for (int i = 0; i < arr.length; i++) {
sum+=arr[i].getAge();
}
average=sum/arr.length;
System.out.println("女朋友的平均年龄是"+average);
int count=0;
for (int i = 0; i < arr.length; i++) {
if(arr[i].getAge()<average){
arr[i].showInfo();
count++;
}
}
System.out.println("比平均年龄小的共有"+count+"个");
}
}
练习:对象数组5(学生信息管理)
Student类
package day2;
public class Student {
private String name;
private int id;
private int age;
public Student(){
}
public Student(String name,int id,int age){
this.name=name;
this.id=id;
this.age=age;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getId()
{
return id;
}
public void setId(int id){
this.id=id;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
}
Stutest类
package day2;
import java.util.Scanner;
public class Stutest {
public static void main(String[] args) {
Student[] arr=new Student[3];
Student stu1=new Student("张三",202316,18);
Student stu2=new Student("李四",202317,19);
Student stu3=new Student("王五",202318,20);
//将对象添加到数组当中
arr[0]=stu1;
arr[1]=stu2;
arr[2]=stu3;
//要添加的对象
Student stu4=new Student("赵六",202319,35);
//添加一个学生对象
//数组已满,需要创建新的数组,将老数组的元素添加到新数组中,数组长度为老数组+1
//添加的时候要判断学号的唯一性
Student[] arr2=new Student[arr.length+1];
if(!Repeat(arr,stu4)){
if(getCount(arr)<arr.length){//数组中元素个数小于数组长度说明还有空位置
arr[getCount(arr)]=stu4;
Check(arr);
}else{
arr2=newArr(arr,stu4);
Check(arr2);
}
}
else{
System.out.println("Id重复,请修改id后重新添加");
}
//通过id删除学生信息
Scanner sc=new Scanner(System.in);
System.out.println();
System.out.println("请输入你想要学生年龄+1的ID");
int num=sc.nextInt();
if(index(num,arr2)>0){
arr2[index(num,arr2)].setAge(arr2[index(num,arr2)].getAge()+1);
}
else{
System.out.println("没有找到");
}
Check(arr2);
}
//定义方法:按照指定id返回其数组下标
public static int index(int idnum,Student[] Arr){
for (int i = 0; i < Arr.length; i++) {
if(Arr[i].getId()==idnum){
return i;
}
}
return -1;
}
//要判断遍历老数组还是新数组,代码会重复,所以定义一个方法遍历所有学生信息
public static void Check(Student[] Arr){
for (int i = 0; i < Arr.length; i++) {
if(Arr[i]!=null){//不为空才打印
System.out.println(Arr[i].getName()+","+Arr[i].getId()+","+Arr[i].getAge());
}
}
}
//定义方法:创建新的数组,并将老数组中的元素加到新数组中
public static Student[] newArr(Student[] Arr,Student Stu4){
Student[] Arr2=new Student[Arr.length+1];
for (int i = 0; i < Arr.length; i++) {//将老数组的元素添加到新数组中
Arr2[i]=Arr[i];
}
Arr2[Arr.length]=Stu4;
return Arr2;
}
//定义方法获得数组中元素的个数
public static int getCount(Student[] arr){
int count=0;
for (int i = 0; i < arr.length; i++) {
if(arr[i]!=null){
count++;
}
}
return count;
}
//判断加的元素id是否重复
public static Boolean Repeat(Student[] arr,Student Stu4){
for (int i = 0; i < arr.length; i++) {//判断id是否重复,不重复则添加
if(arr[i]!=null) {//此处要加非空判断
if (arr[i].getId() == Stu4.getId()) {
return true;
}
}
}
return false;
}
}