From 75b257255fe088a702c6dd66477bd753162409fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E4=BC=9F=E5=B1=B1?= <2609838563@qq.com> Date: Fri, 23 Dec 2022 12:50:58 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...41\347\220\206\347\263\273\347\273\237.md" | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 "39 \345\247\234\344\274\237\345\261\261/20221223 \345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" diff --git "a/39 \345\247\234\344\274\237\345\261\261/20221223 \345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/39 \345\247\234\344\274\237\345\261\261/20221223 \345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000..f2a3e69 --- /dev/null +++ "b/39 \345\247\234\344\274\237\345\261\261/20221223 \345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,142 @@ +```java +import java.util.Scanner; + +public class D7 { + // 把扫描器放在最外层,让所有方法都可以用 + static Scanner sc = new Scanner(System.in); + + public static void main(String[] args) { + String[] students = new String[10]; + students[0] = "小强"; + students[1] = "大强"; + while (true) { + start(); + choicMenu(sc.nextInt(), students); + } + } + + public static void start() { + System.out.println("-----------------------------" + + "\n- 欢迎使用三班的学生管理系统-" + + "\n- \t\t1.浏览所有学生信息\t\t-" + + "\n- \t\t2.添加学生信息\t\t-" + + "\n- \t\t3.修改学生信息\t\t-" + + "\n- \t\t4.删除学生信息\t\t-" + + "\n- \t\t5.查询学生信息\t\t-" + + "\n- \t\t6.退出管理系统\t\t-" + + "\n------------------------------"); + } +// 菜单 + + public static void choicMenu(int num, String[] stu) { + switch (num) { + case 1: +// System.out.println("你选择了浏览学生信息"); + viewAllStuendt(stu); + break; + case 2: + addStudent(stu); +// System.out.println("你选择了添加学生信息"); + break; + case 3: + editStudent(stu); +// System.out.println("你选择了修改学生信息"); + break; + case 4: + deleteStudent(stu); +// System.out.println("你选择了删除学生信息"); + break; + case 5: + searchStudent(); +// System.out.println("你选择了查询学生信息"); + + break; + case 6: +// System.out.println("你选择了退出管理系统"); + break; + default: +// System.out.println("选项错误"); + } + } + + public static void viewAllStuendt(String[] stu) { + System.out.println("三班现有以下学生:"); + for (String name : stu) { + if (name == null) { + continue; + } + System.out.print(name + "\t"); + } + } + + public static void addStudent(String[] stu) { + System.out.println("请输入你要添加的学生:"); + String name = sc.next(); + int index = searchIndex(stu,name); + if (index !=-1){ + System.out.println("该学生已经在数据库中,请不要重复添加"); + }else{ + int nullIndex = searchIndex(stu,null); + stu[nullIndex] = name; + System.out.println("添加成功"); + viewAllStuendt(stu); + } + + } + + private static int searchIndex(String[] stu, String str) { + int index =-1; + for (int i=0;i Date: Tue, 27 Dec 2022 13:38:40 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...42\345\220\221\345\257\271\350\261\241.md" | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 "39 \345\247\234\344\274\237\345\261\261/20221225 \351\235\242\345\220\221\345\257\271\350\261\241.md" diff --git "a/39 \345\247\234\344\274\237\345\261\261/20221225 \351\235\242\345\220\221\345\257\271\350\261\241.md" "b/39 \345\247\234\344\274\237\345\261\261/20221225 \351\235\242\345\220\221\345\257\271\350\261\241.md" new file mode 100644 index 0000000..363b517 --- /dev/null +++ "b/39 \345\247\234\344\274\237\345\261\261/20221225 \351\235\242\345\220\221\345\257\271\350\261\241.md" @@ -0,0 +1,122 @@ +## 1. 类和对象 + +### 1.1 类和对象的理解 + +客观存在的事物皆为对象 ,所以我们也常常说万物皆对象。 + +- 类 + - 类的理解 + - 类是对现实生活中一类具有共同属性和行为的事物的抽象 + - 类是对象的数据类型,类是具有相同属性和行为的一组对象的集合 + - 简单理解:类就是对现实事物的一种描述 + - 类的组成 + - 属性:指事物的特征,例如:手机事物(品牌,价格,尺寸) + - 行为:指事物能执行的操作,例如:手机事物(打电话,发短信) +- 类和对象的关系 + - 类:类是对现实生活中一类具有共同属性和行为的事物的抽象 + - 对象:是能够看得到摸的着的真实存在的实体 + - 简单理解:**类是对事物的一种描述,对象则为具体存在的事物** + +### 1.2 类的定义 + +类的组成是由属性和行为两部分组成 + +- 属性:在类中通过成员变量来体现(类中方法外的变量) +- 行为:在类中通过成员方法来体现(和前面的方法相比去掉static关键字即可) + +类的定义步骤: + +①定义类 + +②编写类的成员变量 + +③编写类的成员方法 + +``` +public class 类名 { + // 成员变量 + 变量1的数据类型 变量1; + 变量2的数据类型 变量2; + … + // 成员方法 + 方法1; + 方法2; +} +``` + +示例代码: + +``` +/* + 手机类: + 类名: + 手机(Phone) + + 成员变量: + 品牌(brand) + 价格(price) + + 成员方法: + 打电话(call) + 发短信(sendMessage) + */ +public class Phone { + //成员变量 + String brand; + int price; + + //成员方法 + public void call() { + System.out.println("打电话"); + } + + public void sendMessage() { + System.out.println("发短信"); + } +} +``` + +### 1.3 对象的使用 + +- 创建对象的格式: + - 类名 对象名 = new 类名(); +- 调用成员的格式: + - 对象名.成员变量 + - 对象名.成员方法(); +- 示例代码 + +```java +/* + 创建对象 + 格式:类名 对象名 = new 类名(); + 范例:Phone p = new Phone(); + + 使用对象 + 1:使用成员变量 + 格式:对象名.变量名 + 范例:p.brand + 2:使用成员方法 + 格式:对象名.方法名() + 范例:p.call() + */ +public class PhoneDemo { + public static void main(String[] args) { + //创建对象 + Phone p = new Phone(); + + //使用成员变量 + System.out.println(p.brand); + System.out.println(p.price); + + p.brand = "小米"; + p.price = 2999; + + System.out.println(p.brand); + System.out.println(p.price); + + //使用成员方法 + p.call(); + p.sendMessage(); + } +} +``` \ No newline at end of file -- Gitee