Java 类 对象 包

时间:2023-03-09 15:34:11
Java 类 对象 包

Java类和对象

类是具有相同属性和行为的一组对象的集合。(属性是用来描述对象的特征可以理解为成员变量 例如:一个学生(对象)他的类可能是学校,它的属性可能是学号,姓名,年龄,班级,成绩等等)

例子:

学生管理系统

要求实现登陆,学生信息的添加、显示,删除,修改,查询,排序,退出功能。

建立一个学生类,类中有学生的三个需要用到的属性(学号、姓名、成绩)

 public class Student {
/** 学生学号 */
public int number;
/** 学生姓名 */
public String name;
/** 学生成绩 */
public int grade;
}

随后在StuInfo的mian方法中实例化Student对象完成系统

在main方法中应用类型声明为数组

建立6个mian下的方法,分别实现(添加,显示,删除,查找,修改,排序)功能

package Hw0420;

import javax.swing.JOptionPane;

public class StuInfo {
public static Student[] array = new Student[20];
public static int stunum = 0; public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "欢迎使用XXX学生管理系统");
boolean bl = login();
if (!bl) {
JOptionPane.showMessageDialog(null, "非法用户");
}
while (true) {
String s = JOptionPane.showInputDialog(null, "1.添加\n2.显示\n3.删除\n4.查找\n5.修改\n6.排序\n7.退出");
int input = Integer.parseInt(s);
switch (input) {
case 1:
add();
break;
case 2:
show();
break;
case 3:
del();
break;
case 4:
find();
break;
case 5:
mod();
break;
case 6:
sort();
break;
case 7:
JOptionPane.showMessageDialog(null, "感谢使用XXX学生管理系统");
System.exit(0);
break;
}
}
}
/**
* 登录判断
*
* @return是否登录成功
*/
public static boolean login() {
for (int i = 0; i < 3; i++) {
String id = JOptionPane.showInputDialog(null, "请输入您的账号");
String pwd = JOptionPane.showInputDialog(null, "请输入您的密码");
if (id.equals(pwd)) {
return true;
}
}
return false;
}
/**
* 添加
*/
public static void add() {  
String strcode = JOptionPane.showInputDialog(null, "请输入学生学号");
String strname = JOptionPane.showInputDialog(null, "请输入学生姓名");
String strgrade = JOptionPane.showInputDialog(null, "请输入学生成绩");
Student s = new Student();在此完成对学生对象各个属性的初始化
s.number = Integer.parseInt(strcode);
s.name = strname;
s.grade = Integer.parseInt(strgrade);
array[stunum] = s;
stunum++;
}
/**
* 查找学生
*
* @return找到返回下标 找不到返回-1
*/
public static int findByname() {
String s = JOptionPane.showInputDialog(null, "请输入您要查找的学生姓名");
for (int i = 0; i < stunum; i++) {
if (s.equals(array[i].name)) {
return i;
}
}
JOptionPane.showMessageDialog(null, "查无此人");
return -1;
}
/**
* 显示
*/
public static void show() {
String str = "学号 姓名 成绩\n";
for (int i = 0; i < stunum; i++) {
str += array[i].number + " " + array[i].name + " " + array[i].grade
+ "\n";
}
JOptionPane.showMessageDialog(null, str);
}
/**
* 删除
*/
public static void del() {
int index = findByname();
if (index != -1) {
for (int i = index; i < stunum; i++) {
array[i] = array[i + 1];
}
JOptionPane.showMessageDialog(null, "已删除");
show();
stunum--;
}
}
/**
* 查找
*/
public static void find() {
int index = findByname();
if (index != -1) {
String str = "学号:" + array[index].number + "\n" + "姓名:" + array[index].name + "\n" + "成绩:"
+ array[index].grade;
JOptionPane.showMessageDialog(null, str);
}
} /**
* 修改
*/
public static void mod() {
int index = findByname();
if (index != -1) {
String strcode = JOptionPane.showInputDialog(null, "请输入学生学号");
String strname = JOptionPane.showInputDialog(null, "请输入学生姓名");
String strgrade = JOptionPane.showInputDialog(null, "请输入学生成绩");
array[index].number = Integer.parseInt(strcode);
array[index].name = strname;
array[index].grade = Integer.parseInt(strgrade);
}
show();
}
/**
* 排序
*/
public static void sort() {
for (int i = 0; i < stunum; i++) {
for (int j = i + 1; j < stunum; j++) {
if (array[i].grade > array[j].grade) {
Student s = array[i];
array[i] = array[j];
array[j] = s;
}
}
}
show();
}
}

总结

在JAVA类的引用中,注意静态变量和成员变量的作用范围及定义,静态变量是所有对象共享的变量(比如所有学生都是在一个学校上学,那么学校的名称就可以是静态的变量)静态变量可以通过对象或者类名两种方式访问,但是成员对象相互独立(比如一个学校每个学生都有属于自己的年龄和姓名还有学号)成员变量只能通过对象的方式来访问。

注意在对象的使用上,指向各个属性的变量名都是应用的地址,注意其指向方向的改变。