4.10总结
一、面向对象
面向:找拿; 对象:东西。
比如扫地,找扫把解决问题。编程中找的Scanner ,Random。如果没有已经设计好的,则需要自己设计新的方法。
1.创建对象,设计类
类是什么?—— 对象共同特征的描述
对象: 真实存在的具体实例
必须先设计类,才能创建对象使用
2.类的格式:
public class 类名{
成员变量:代表属性
成员方法:代表行为
构造器
代码块
内部块
}
3.如何得到类的对象
类名 对象名 = new 类名();
Car c = new Car();
如何使用:
访问属性: 对象名.成员变量
访问行为: 对象名.方法名(...)
4.定义类注意事项
- 类名首字母建议大写,不能是关键字,必须是合法标识符
- 一个Java文件可以定义多个class类,但是只能一个类是public修饰,且public修饰的类名必须成为代码文件名。建议一个代码文件定义一个类,可新建类定义其他类。
- 成员变量完整定义格式: 修饰符 数据类型 变量名称 = 初始化值;一般无需指定初始化值。
- 默认值:byte short int long——0 ; double float——0.0 ; boolean——false ; String等引用类型——null
5.面向对象——模拟购物车案例
需求:实现添加商品到购物车去,同时需要提供修改商品的购买数量,结算商品价格的功能。
分析:1.购物车每一个商品都是商品类,需要定义一个商品类,商品类中创建对象
2.购物车也是一个对象:可以使用数组对象代表它。 ——类似容器,存放商品。
3.完成界面架构,让用户选择操作功能。
一阶段:添加查询商品,代码如下
package ShopCarTest;
import java.util.Scanner;
public class ShopCar {
public static void main(String[] args) {
//1.首先定义商品类,用来存放商品对象
//2.定义购物车对象,使用一个数组对象表示。(模拟)
Goods[] shopCar = new Goods[100]; //代表商品类型的数组,最多可以装100个商品对象。
//3.搭建操作界面架构
while (true) {
System.out.println("请您选择如下命令操作:");
System.out.println("添加商品到您的购物车:add");
System.out.println("查询商品到您的购物车:query");
System.out.println("修改商品购买数量:update");
System.out.println("结算商品购买金额:pay");
Scanner sc = new Scanner(System.in);
System.out.println("请您输入命令:");
String command = sc.next();
switch (command){
case "add":
//添加上商品到购物车
addGoods(shopCar);
break;
case "query":
//查询
queryGoods(shopCar);
break;
case "update":
//修改
updateGoods(shopCar);
break;
case "pay":
//结算
payGoods(shopCar);
break;
default:
System.out.println("没有该功能!");
}
}
}
public static void payGoods(Goods[] shopCar) {
}
public static void updateGoods(Goods[] shopCar) {
}
public static void queryGoods(Goods[] shopCar) {
//查询商品对象信息,并展示出来。
System.out.println("==========查询购物车信息如下==========");
System.out.println("编号\t\t名称\t\t\t价格\t\t\t购买数量");
for (int i = 0; i < shopCar.length; i++) {
Goods g = shopCar[i];
if (shopCar[i] != null){
//展示商品信息
System.out.println(g.id + "\t\t" + g.name + "\t\t" + g.price + "\t\t" + g.Buynumber);
}else {
//遍历结束
break;
}
}
}
public static void addGoods(Goods[] shopCar) {
//1.录入商品信息
Scanner sc = new Scanner(System.in);
System.out.println("请您输入商品编号(不重复):");
int id = sc.nextInt();
System.out.println("请您输入购买的商品名称:");
String name = sc.next();
System.out.println("请您输入购买数量:");
int Buynumber = sc.nextInt();
System.out.println("请您输入购买商品的价格:");
double price = sc.nextDouble();
//2.把这些购买商品的信息封装成一个商品对象
Goods g = new Goods();
g.id = id;
g.Buynumber = Buynumber;
g.price = price;
g.name = name;
//3.把商品对象添加到购物车数组中去。
for (int i = 0; i < shopCar.length; i++) {
if (shopCar[i] == null);{
//说明此位置没有元素填入
shopCar[i] = g;
break;//结束,商品成功存入,不需要找位置了。
}
}
System.out.println("您的商品成功添加到购物车!");
}
}
二阶段,修改结算商品,代码如下:
package ShopCarTest;
import java.util.Scanner;
public class ShopCar {
public static void main(String[] args) {
//1.首先定义商品类,用来存放商品对象
//2.定义购物车对象,使用一个数组对象表示。(模拟)
Goods[] shopCar = new Goods[100]; //代表商品类型的数组,最多可以装100个商品对象。
//3.搭建操作界面架构
while (true) {
System.out.println("请您选择如下命令操作:");
System.out.println("添加商品到您的购物车:add");
System.out.println("查询商品到您的购物车:query");
System.out.println("修改商品购买数量:update");//修改购买的商品数量,并展示
System.out.println("结算商品购买金额:pay");//输入pay命令后,展示全部购买的商品信息和总金额;
Scanner sc = new Scanner(System.in);
System.out.println("请您输入命令:");
String command = sc.next();
switch (command){
case "add":
//添加上商品到购物车
addGoods(shopCar);
break;
case "query":
//查询
queryGoods(shopCar);
break;
case "update":
//修改
updateGoods(shopCar);
break;
case "pay":
//结算
payGoods(shopCar);
break;
default:
System.out.println("没有该功能!");
}
}
}
public static void payGoods(Goods[] shopCar) {
queryGoods(shopCar);
//输入pay命令后,展示全部购买的商品信息和总金额;
//1.定义求和变量,累加金额
double money = 0;
//2.遍历全部商品,累加单价*数量
for (int i = 0; i < shopCar.length; i++) {
Goods g = shopCar[i];
if (g != null){
money += (g.price * g.Buynumber);
}else {
break;
}
}
System.out.println("订单总金额" + money);
}
public static void updateGoods(Goods[] shopCar) {
//修改商品购买数量
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("请您输入商品id:");
int id = sc.nextInt();
Goods g = getGoodsById(shopCar, id);
if (g == null){
//没有该商品
System.out.println("对不起,您没有购买该商品。");
}else {
//存在该商品,可以修改
System.out.println("请您输入" + g.name + "商品的购买数量:");
int buyNumber = sc.nextInt();
g.Buynumber = buyNumber;
System.out.println("修改完成!");
queryGoods(shopCar);//给予再次查询条件
break;//商品修改完后结束循环
}
}
}
public static Goods getGoodsById(Goods[] shopCar , int id){
for (int i = 0; i < shopCar.length; i++) {
Goods g = shopCar[i];
if (g != null){//判断是否为我们要找的商品
if (g.id == id){
return g;
}
}else {
return null;//找完已经填入的商品,没有找到。
}
}
return null;//表示遍历完商品也没有找到和输入id相同的商品
}
public static void queryGoods(Goods[] shopCar) {
//查询商品对象信息,并展示出来。
System.out.println("==========查询购物车信息如下==========");
System.out.println("编号\t\t名称\t\t\t价格\t\t\t购买数量");
for (int i = 0; i < shopCar.length; i++) {
Goods g = shopCar[i];
if (shopCar[i] != null){
//展示商品信息
System.out.println(g.id + "\t\t" + g.name + "\t\t" + g.price + "\t\t" + g.Buynumber);
}else {
//遍历结束
break;
}
}
}
public static void addGoods(Goods[] shopCar) {
//1.录入商品信息
Scanner sc = new Scanner(System.in);
System.out.println("请您输入商品编号(不重复):");
int id = sc.nextInt();
System.out.println("请您输入购买的商品名称:");
String name = sc.next();
System.out.println("请您输入购买数量:");
int Buynumber = sc.nextInt();
System.out.println("请您输入购买商品的价格:");
double price = sc.nextDouble();
//2.把这些购买商品的信息封装成一个商品对象
Goods g = new Goods();
g.id = id;
g.Buynumber = Buynumber;
g.price = price;
g.name = name;
//3.把商品对象添加到购物车数组中去。
for (int i = 0; i < shopCar.length; i++) {
if (shopCar[i] == null);{
//说明此位置没有元素填入
shopCar[i] = g;
break;//结束,商品成功存入,不需要找位置了。
}
}
System.out.println("您的商品成功添加到购物车!");
}
}
遇到的问题:
添加商品不完善,封装信息不了解。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏