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

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

最新文章

  1. 编译器开发系列--Ocelot语言7.中间代码
  2. vim 标记 mark 详解 (转载)
  3. MyBatis(3.2.3) - Cache
  4. 页面正在载入js
  5. 【转】Spring事务超时时间可能存在的错误认识
  6. python 线程与进程
  7. ogg-oracle to sqlserver
  8. uuid原理及使用例子
  9. Java单元测试神器之Mockito
  10. luogu P1723 高手过愚人节
  11. ES 01 - Elasticsearch入门 + 基础概念学习
  12. SpringBoot之加密
  13. 升级 phpstud y中的 mysql 版本
  14. MySQL— 索引
  15. 2017-2018-2 20155309南皓芯 Exp8 WEB基础实践
  16. 菜单联动,select联动菜单(搜索之后默认选中)
  17. Android 列表使用(ListView GridView Gallery图片计时滚动)
  18. (最小生成树) Jungle Roads -- POJ -- 1251
  19. PHP 在Win下的安装
  20. JAXB实现java对象与xml之间转换

热门文章

  1. nyoj24-素数 距离问题
  2. Python学习笔记之函数
  3. Vue CLI 3.x 简单体验
  4. SpringBoot后台如何实现文件上传下载
  5. SSH框架下单元测试的实现
  6. 使用Neo4j和简单分词算法实现菜品推荐系统
  7. 修改oracle客户端的字符集
  8. MySQL 面试题目
  9. android 细节之An internal error occurred during: &amp;quot;Launching New_configuration&amp;quot;.
  10. HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)