
还是那句话面向对象是Java的重点,同时也是难点,小编会在此部分分享一些综合型的案例,难度从易到难,本篇文章只是一些基础的操作,综合能力并不是太高。大家可以试着观看一遍自己去打开编译器尝试一下。
不知道有多少读者是跟着小编进行学习的,今天是学习Java的第五天,希望大家能够一同坚持下去。

为了防止读者们遗忘,小编将基础部分放在下面了欧
运算符、判断、循环
数组、方法
双色球系统

注:代码分析部分省略了对JavaBeen部分的分析,Javabeen部分的代码在最后
public class text1 {
public static void main(String[] args) {
//案例一、对象数组1
//1.创建一个数组
Goods[]goods = new Goods[3];
//2.创建三个商品对象
Goods g1 = new Goods("001","西瓜",18.5,50);
Goods g2 = new Goods("002","苹果",19.8,100);
Goods g3 = new Goods("003","香蕉",11.5,50);
//3.将商品添加到数组中
goods[0] = g1;
goods[1] = g2;
goods[2] = g3;
//4.遍历数组
for (int i = 0; i < goods.length; i++) {
Goods g = goods[i];
System.out.println(g.getId()+","+g.getName()+","+g.getPrice());
}
}类和对象:
Goods 类的定义g1, g2, g3 是 Goods 类的实例,每个实例代表一个商品,具有不同的属性:编号、名称、价格等。对象数组:
Goods[] goods = new Goods[3]; 创建了一个长度为 3 的 Goods 类型的数组 goods,用来存储商品对象。实例化对象:
g1, g2, g3 分别创建了三个商品对象,并初始化它们的属性。数组赋值:
g1, g2, g3 分别赋值给数组 goods 的第 0、1、2 个位置,即将这些商品对象存储在数组中。遍历数组:
for 循环遍历数组 goods。Goods 对象赋值给 g,然后输出商品的编号、名称和价格。public static class text2 {
public static void main(String[] args) {
//案例二、对象数组2
//1.创建数组存放汽车对象
Cars[]car = new Cars[3];
//2.创建汽车对象
Scanner sc = new Scanner(System.in);
for (int i = 0; i < car.length; i++) {
Cars c = new Cars();
System.out.println("请输入汽车品牌:");
String brand = sc.next();
c.setBrand(brand);
System.out.println("请输入汽车价格:");
double price = sc.nextDouble();
c.setPrice(price);
System.out.println("请输入汽车颜色:");
String color = sc.next();
c.setColor(color);
//3.将汽车对象存入数组中
car[i] = c;
}
//4.遍历数组
for (int i = 0; i < car.length; i++) {
Cars c = car[i];
System.out.println(c.getBrand() + " " + c.getPrice() + " " + c.getColor());
}
}
}类和对象:
Cars 类的定义Cars 类包括了以下方法: setBrand(String brand):设置汽车品牌。setPrice(double price):设置汽车价格。setColor(String color):设置汽车颜色。getBrand(), getPrice(), getColor():用于获取汽车的品牌、价格和颜色。对象数组:
Cars[] car = new Cars[3]; 创建了一个长度为 3 的 Cars 类型的数组 car,用来存储汽车对象。用户输入和对象填充:
Scanner 类来获取用户输入。for 循环,循环 3 次(数组的长度),每次创建一个新的 Cars 对象 c。c.setXXX() 方法设置到对象 c 中。c 对象存入数组 car 的第 i 个位置。遍历数组并输出:
for 循环遍历数组 car。Cars 对象 c,并使用 c.getXXX() 方法获取汽车的品牌、价格和颜色信息。Cars 类在程序中正确定义,并且包含了用于设置和获取汽车属性的方法。double 类型,可以通过 sc.nextDouble() 获取。键盘录入
第一套体系
nextInt();接收整数
nextDouble();接收小数
next();接收字符串
注:该体系中遇到空格、制表符、回车就停止接收。
第二套体系
nextLine();接收字符串
可以接收空格、制表符,回车就停止接收。
键盘录入的两套体系不能混用public static class text3 {
public static void main(String[] args) {
//案例三、对象数组3
//1.创建一个数组
Phone[] phone = new Phone[3];
//2.创建三个手机对象
Phone phone1 = new Phone("小米14",4399.9,"白色");
Phone phone2 = new Phone("苹果15",8999.9,"黑色");
Phone phone3 = new Phone("华为",3999.9,"蓝色");
//3.将手机添加到数组中
phone[0] = phone1;
phone[1] = phone2;
phone[2] = phone3;
//4.获取三部手机的平均价格
double sum = 0;
for (int i = 0; i < phone.length; i++) {
sum += phone[i].getPrice();
}
System.out.println("手机平均价格为:"+(int)(sum/phone.length));
}
}对象和数组的创建:
Phone[] phone = new Phone[3]; 创建了一个长度为 3 的 Phone 类型的数组 phone,用于存储手机对象。手机对象的创建:
Phone phone1 = new Phone("小米14", 4399.9, "白色");
Phone phone2 = new Phone("苹果15", 8999.9, "黑色");
Phone phone3 = new Phone("华为", 3999.9, "蓝色");Phone 类的构造函数创建了三个手机对象:将手机对象添加到数组中:
phone[0] = phone1;
phone[1] = phone2;
phone[2] = phone3;phone 的每个位置:计算手机的平均价格:
使用循环遍历数组 phone,累加每个手机对象的价格:
double sum = 0;
for (int i = 0; i < phone.length; i++) {
sum += phone[i].getPrice();
}phone[i].getPrice() 用于获取第 i 个手机对象的价格,并累加到 sum 中。
输出平均价格:
sum/phone.length。(int) 强制转换为整数,以便输出整数部分:Phone 类在程序中要有正确的定义,包含了适当的属性和构造函数。double 类型,与手机对象中定义的价格类型一致。public static class text4 {
public static void main(String[] args) {
//案例四、对象数组4
//1.创建一个数组
GirlFriends[] girlFriends = new GirlFriends[4];
//2.创建四个女朋友对象
GirlFriends girl1 = new GirlFriends("貂蝉",18,"姣好靓丽","吃零食");
GirlFriends girl2 = new GirlFriends("王昭君",19,"芙蓉出水","玩游戏");
GirlFriends girl3 = new GirlFriends("西施",21,"冰清玉洁","学习");
GirlFriends girl4 = new GirlFriends("杨贵妃",22,"美若天仙","看书");
//3.将女朋友添加到数组中
girlFriends[0] = girl1;
girlFriends[1] = girl2;
girlFriends[2] = girl3;
girlFriends[3] = girl4;
//4.获取四个女朋友的平均年龄
double sum = 0;
double sum2 = 0;
for (int i = 0; i < girlFriends.length; i++) {
sum+=girlFriends[i].getAge();
}
sum2=sum/girlFriends.length;
//5.打印比平均年龄低的女朋友信息
for (int i = 0; i < girlFriends.length; i++) {
if(girlFriends[i].getAge()<=sum2){
System.out.println(girlFriends[i].getName()+","+girlFriends[i].getAge()+","+girlFriends[i].getGender()+","+girlFriends[i].getHobby());
}
}
}
}对象和数组的创建:
GirlFriends[] girlFriends = new GirlFriends[4];GirlFriends 类型的数组 girlFriends,用于存储女朋友对象。女朋友对象的创建:
温馨提示:此处定义的对象是女朋友对象,不是你的对象欧

使用 GirlFriends 类的构造函数创建了四个女朋友对象:
GirlFriends girl1 = new GirlFriends("貂蝉", 18, "姣好靓丽", "吃零食");
GirlFriends girl2 = new GirlFriends("王昭君", 19, "芙蓉出水", "玩游戏");
GirlFriends girl3 = new GirlFriends("西施", 21, "冰清玉洁", "学习");
GirlFriends girl4 = new GirlFriends("杨贵妃", 22, "美若天仙", "看书");每个女朋友对象包含姓名、年龄、性格描述和兴趣爱好。
将女朋友对象添加到数组中:
将创建好的女朋友对象分别存放到数组 girlFriends 的每个位置:
girlFriends[0] = girl1;
girlFriends[1] = girl2;
girlFriends[2] = girl3;
girlFriends[3] = girl4;计算女朋友的平均年龄:
使用循环遍历数组 girlFriends,累加每个女朋友对象的年龄:
double sum = 0;
for (int i = 0; i < girlFriends.length; i++) {
sum += girlFriends[i].getAge();
}girlFriends[i].getAge() 用于获取第 i 个女朋友对象的年龄,并累加到 sum 中。
计算平均年龄并打印比平均年龄低的女朋友信息:
计算平均年龄:sum / girlFriends.length。
使用第二个循环遍历数组 girlFriends,判断每个女朋友的年龄是否低于或等于平均年龄 sum2:
double sum2 = sum / girlFriends.length;
for (int i = 0; i < girlFriends.length; i++) {
if (girlFriends[i].getAge() <= sum2) {
System.out.println(girlFriends[i].getName() + "," + girlFriends[i].getAge() + "," + girlFriends[i].getGender() + "," + girlFriends[i].getHobby());
}
}如果年龄符合条件,则打印女朋友的姓名、年龄、性格描述和兴趣爱好。
GirlFriends 类在程序中有正确的定义,包含了适当的属性和构造函数。int 类型,与女朋友对象中定义的年龄类型一致。package day5;
import day4.Cars;
import day4.Goods;
import day4.Phone;
import java.util.Scanner;
public class text1 {
public static void main(String[] args) {
//案例一、对象数组1
//1.创建一个数组
Goods[]goods = new Goods[3];
//2.创建三个商品对象
Goods g1 = new Goods("001","西瓜",18.5,50);
Goods g2 = new Goods("002","苹果",19.8,100);
Goods g3 = new Goods("003","香蕉",11.5,50);
//3.将商品添加到数组中
goods[0] = g1;
goods[1] = g2;
goods[2] = g3;
//4.遍历数组
for (int i = 0; i < goods.length; i++) {
Goods g = goods[i];
System.out.println(g.getId()+","+g.getName()+","+g.getPrice());
}
}
public static class text2 {
public static void main(String[] args) {
//案例二、对象数组2
//1.创建数组存放汽车对象
Cars[]car = new Cars[3];
//2.创建汽车对象
Scanner sc = new Scanner(System.in);
for (int i = 0; i < car.length; i++) {
Cars c = new Cars();
System.out.println("请输入汽车品牌:");
String brand = sc.next();
c.setBrand(brand);
System.out.println("请输入汽车价格:");
double price = sc.nextDouble();
c.setPrice(price);
System.out.println("请输入汽车颜色:");
String color = sc.next();
c.setColor(color);
//3.将汽车对象存入数组中
car[i] = c;
}
//4.遍历数组
for (int i = 0; i < car.length; i++) {
Cars c = car[i];
System.out.println(c.getBrand() + " " + c.getPrice() + " " + c.getColor());
}
}
}
public static class text3 {
public static void main(String[] args) {
//案例三、对象数组3
//1.创建一个数组
Phone[] phone = new Phone[3];
//2.创建三个手机对象
Phone phone1 = new Phone("小米14",4399.9,"白色");
Phone phone2 = new Phone("苹果15",8999.9,"黑色");
Phone phone3 = new Phone("华为",3999.9,"蓝色");
//3.将手机添加到数组中
phone[0] = phone1;
phone[1] = phone2;
phone[2] = phone3;
//4.获取三部手机的平均价格
double sum = 0;
for (int i = 0; i < phone.length; i++) {
sum += phone[i].getPrice();
}
System.out.println("手机平均价格为:"+(int)(sum/phone.length));
}
}
public static class text4 {
public static void main(String[] args) {
//案例四、对象数组4
//1.创建一个数组
GirlFriends[] girlFriends = new GirlFriends[4];
//2.创建四个女朋友对象
GirlFriends girl1 = new GirlFriends("貂蝉",18,"姣好靓丽","吃零食");
GirlFriends girl2 = new GirlFriends("王昭君",19,"芙蓉出水","玩游戏");
GirlFriends girl3 = new GirlFriends("西施",21,"冰清玉洁","学习");
GirlFriends girl4 = new GirlFriends("杨贵妃",22,"美若天仙","看书");
//3.将女朋友添加到数组中
girlFriends[0] = girl1;
girlFriends[1] = girl2;
girlFriends[2] = girl3;
girlFriends[3] = girl4;
//4.获取四个女朋友的平均年龄
double sum = 0;
double sum2 = 0;
for (int i = 0; i < girlFriends.length; i++) {
sum+=girlFriends[i].getAge();
}
sum2=sum/girlFriends.length;
//5.打印比平均年龄低的女朋友信息
for (int i = 0; i < girlFriends.length; i++) {
if(girlFriends[i].getAge()<=sum2){
System.out.println(girlFriends[i].getName()+","+girlFriends[i].getAge()+","+girlFriends[i].getGender()+","+girlFriends[i].getHobby());
}
}
}
}
}package day4;
public class Goods {
private String id;
private String name;
private double price;
private int count;
//空参
public Goods(){
}
//构造函数
public Goods(String id, String name, double price, int count) {
this.id = id;
this.name = name;
this.price = price;
this.count = count;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}package day4;
public class Cars {
private String brand;//品牌
private double price;//价格
private String color;
//空参
public Cars() {
}
public Cars(String brand, double 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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}package day4;
public class Phone {
private String brind;
private double price;
private String color;
public Phone() {
}
public Phone(String brind, double price, String color) {
this.brind = brind;
this.price = price;
this.color = color;
}
public String getBrind() {
return brind;
}
public void setBrind(String brind) {
this.brind = brind;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}