类和对象
-
对象是客观存在的实体,类是对象的抽象。
-
类是对象的抽象,对象是类的实例。
-
创建类:
-
实例化对象: 类名 对象名=new 类名(); 对象名.属性名=属性值; 对象名.方法名();
-
类:属性和方法
-
无参方法: 语法: 方法的修饰符 返回值类型 方法名(){
}
方法的修饰符:public
返回值类型: 无返回值——void 有返回值—— int ,String, 对象,数组
方法名:自己起的名字
方法体:方法要做的事情
无返回值方法:
有返回值的方法:
-
变量的作用域: 成员变量:定义在类中,方法的外面,作用范围是整个类。成员变量会给一个初始值。 局部变量:定义在方法里,作用范围在定义此变量的方法里。局部变量必须声明赋值后使用。
-
注释: 单行注释: // 多行注释: /* / 文档注释:/* */
难题:创建类
import java.util.Scanner;
public class Gouwu {
// 创建扫描仪
Scanner input = new Scanner(System.in);
String name = "admin"; // 账号
int mm = 123456; // 密码
boolean flag = true;
// 登录菜单
public void dlcd() {
System.out.println("\t欢迎使用我行我素购物管理系统\n");
System.out.println("\t\t1.登录系统\n");
System.out.println("\t\t2.退出\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * *");
System.out.print("请选择,输入数字:");
int dlxh = input.nextInt(); // 输入序号
System.out.print("请输入用户名:");
switch (dlxh) {
case 1:
String srname = input.next(); // 输入的用户名
System.out.print("请输入密码:");
int srmm = input.nextInt(); // 输入的密码
if (srname.equals(name) && srmm == mm) {
System.out.println("@@登录成功:" + name + "@@");
// 进入主菜单
zcd();
} else {
System.out.println("@@您没有权利进入系统,请重新登录。@@");
// 重新进入此菜单
dlcd();
}
break;
case 2:
System.out.println("成功退出,谢谢使用!");
break;
default:
System.out.println("请输入正确的序号");
dlcd();
break;
}
}
// 主菜单
public void zcd() {
System.out.println("\t我行我素购物管理系统主菜单\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * *\n");
System.out.println("\t\t1.客户信息管理\n");
System.out.println("\t\t2.真情回馈\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * *");
System.out.print("请选择,输入数字或按0返回上一级菜单:");
int zxh = input.nextInt();
switch (zxh) {
case 0:
// 返回登录菜单
dlcd();
break;
case 1:
// 进入客户信息管理菜单
khgl();
break;
case 2:
// 进入真情回馈菜单
zqhk();
break;
default:
System.out.println("请输入正确的序号");
zcd();
break;
}
}
// 客户信息管理菜单
public void khgl() {
System.out.println("客户管理菜单");
}
// 真情回馈菜单
public void zqhk() {
do {
System.out.println("\t我行我素购物管理系统 > 真情回馈\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * *\n");
System.out.println("\t\t1.幸运大放送\n");
System.out.println("\t\t2.幸运抽奖\n");
System.out.println("\t\t3.生日问候\n");
System.out.println("* * * * * * * * * * * * * * * * * * * * *");
System.out.print("请选择,输入数字或按0返回上一级菜单:");
int zqhkxh = input.nextInt(); // 真情回馈里的序号
switch (zqhkxh) {
case 0:
// 返回上一个菜单
zcd();
break;
case 1:
System.out.println("执行幸运大放送");
System.out.println("您继续吗(y/n)");
String pd1 = input.next();
if (pd1.equals("y")) {
continue;
} else if (pd1.equals("n")) {
break;
} else {
System.out.println("请输入(y/n)");
continue;
}
case 2:
System.out.println("执行幸运抽奖");
System.out.println("您继续吗(y/n)");
String pd2 = input.next();
if (pd2.equals("y")) {
continue;
} else if (pd2.equals("n")) {
break;
} else {
System.out.println("请输入(y/n)");
continue;
}
case 3:
System.out.println("执行生日问候");
System.out.println("您继续吗(y/n)");
String pd3 = input.next();
if (pd3.equals("y")) {
continue;
} else if (pd3.equals("n")) {
break;
} else {
System.out.println("请输入(y/n)");
continue;
}
default:
System.out.println("请输入正确的序号");
zqhk();
break;
}
} while (flag);
}
}
测试类
public class GouwuTest {
public static void main(String[] args) {
Gouwu xh=new Gouwu();
xh.dlcd();
}
}