10.Person对象的创建及使用过程
类的方法声明
public String getName() {
return name;
}
public void display() {
System.out.println(“姓名:” + name + “,年龄:” + age
- “,地址:” + address);
}
11.程序引用传递的执行过程
12.方法重载
三个原则:
在同一个类中;
方法名相同;
参数列表不同,即参数的个数、或对应位置上的类型不同。
public int add(int a, int b) {
return a + b;
}
public float add(float a, float b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
13.构造方法
1)方法名和类名相同
2)方法名的前面没有返回值类型的声明
3)方法中不能使用return语句返回一个值
14.类的构造器
具有特点和作用:
1)构造方法是用类名作构造方法名;
2)构造方法具有参数和语句体,但没有返回类型的声明。如果有返回类型
声明,则此方法就再不是构造方法,而成为一个一般的成员方法;
3)构造方法不是类的成员方法,所以不能用对象调用它。
4)构造方法的调用是由new运算符实现;
5)构造方法返回的是这个类的实例的引用;
6)构造方法中的语句实现对成员变量的初始化;
7)构造方法的方法过载。一个类可以有多个构造方法;
8)构造方法之间通过this()形式相互调用。
类的构造方法又可以分为两种
1)默认构造方法
2)非默认构造方法。
15.this 关键字
1).指代对象本身
this用于指代调用成员方法的当前对象自身。语法格式:this
2). this访问本类的成员变量和成员方法
this关键字可以出现在类的实例方法中,代表使用该方法的当前对象。
this.成员变量
this.成员方法
3).在构造方法中使用this
this关键字可以出现在类的构造方法中,代表使用该构造方法所创建的对象。注意:只能在构造方法里使用
this([参数列表])
16.对象的浅拷贝与深拷贝
例子:package com.imau.test3;
/**
*
-
@author yangzj 管理学生:增删改查; 数据存储:数组
*/
public class StuManage {// 学生信息存储:50;
private Student stus[];private static int count;
public StuManage() {
stus = new Student[50]; for(int i=0;i<3;++i) { stus[i]=new Student(20180+i,"AA"+i,"wl"); ++count; }
}
// 添加学生信息
public boolean add(Student student) {if (count > stus.length) { return false; } stus[count++] = student; return true;
}
//3
public boolean del(int stuNo) {
// 1.遍历学生数组,找到学生信息位置
for (int i = 0; i < count; ++i) {if (stuNo == stus[i].getStuNo()) { // 2.删除;后边元素往前移动 for (int j = i; j < count - 1; ++j) { stus[j] = stus[j + 1]; } count--; return true; } } return false;
}
public boolean del(Student student) {
// 1.遍历学生数组,找到学生信息位置
for (int i = 0; i < count; ++i) {if (student.getStuNo() == stus[i].getStuNo()) { // 2.删除;后边元素往前移动 for (int j = i; i < count - 1; ++j) { stus[j] = stus[j + 1]; } return true; } } return false;
}
public boolean update(Student oldStu, Student newStu) {
// 1.遍历学生数组,找到学生信息位置 for (int i = 0; i < count; ++i) { if (oldStu.getStuNo() == stus[i].getStuNo()) { // 2.替换;后边元素往前移动 stus[i] = newStu; return true; }