今天主要说明在面向对象操作中的引用传递情况。一句话,用面向对象设计可以解释生活中的所有问题。
1.对象数组
对象数组肯定是一个数组,回顾一下我们之前使用过的数组。
int a[] = null; 显然数组的每个成员都是int型变量,那么对象数组中的每个成员就是对象了,类比得知。
定义格式如下:
类名称 对象数组名称[] = new 类名称[长度];//使用构造方法定义数组中的对象成员。由于类本身属于引用数据类型,所以开辟对象之后都是null。
案例:
class Info
{
private String name;
private int no;
//构造方法
Info(String name, int no)
{
this.name = name;
this.no = no;
}
public String getInfo()
{
return "姓名:" + this.name + ", 编号:" + this.no ;
}
}
public class TestPass
{
public static void main(String[] args)
{
Info d1[] = new Info[2];
for (int i=0;i<d1.length ;++i )
{
System.out.println(d1[i]);
//这里如果使用System.out.println(d1[i].getInfo());会产生空指向,因为对象只声明,没有实例化
}
}
}
结果:
下面使用对象实例化
案例:
class Info
{
private String name;
private int no;
//构造方法
Info(String name, int no)
{
this.name = name;
this.no = no;
}
public String getInfo()
{
return "姓名:" + this.name + ", 编号:" + this.no ;
}
}
public class TestPass
{
public static void main(String[] args)
{
Info d1[] = new Info[2];
d1[0]= new Info("张三",20);
d1[1]= new Info("李四",30);
for (int i=0;i<d1.length ;++i )
{
System.out.println(d1[i].getInfo());
}
}
}
结果:
内存关系如图:
同数组非常相似,在对象数组实例化也可以进行静态初始化。
格式:
类名称 对象数组[] =new 类名[]{对象1,对象2}new 类名[]可以省略
案例:
class Info
{
private String name;
private int no;
//构造方法
Info(String name, int no)
{
this.name = name;
this.no = no;
}
public String getInfo()
{
return "姓名:" + this.name + ", 编号:" + this.no ;
}
}
public class TestPass
{
public static void main(String[] args)
{
Info d1[] ={new Info("张三",20),new Info("李四",30)};
for (int i=0;i<d1.length ;++i )
{
System.out.println(d1[i].getInfo());
}
}
}
虽然对象数组可以保存多个对象,但是数组的长度是固定,只有明确知道数据量的情况下才行。不知道个数需要其他手段来实现。
2.经典例题
案例1:
class Info
{
private int num=10;
public void setNum(int num)
{
this.num = num;
}
public int getNum()
{
return this.num;
}
}
public class TestPass
{
public static void main(String[] args)
{
Info d = new Info(); //1.初始化d的num=10
d.setNum(100); //2.设置了num=100
fun(d); //3.对象的引用传递,相当于指针,此时info的变化等于d的变化
System.out.println(d.getNum());
}
public static void fun(Info info)
{
info.setNum(30);
}
}
结果
内存说明图:
注意:以上传递的是对象内存的使用。
案例2:
public class TestPass
{
public static void main(String[] args)
{
String str= "hello";
str= "world";
fun(str);<span style="white-space:pre"> </span>//此时把world这个字符串传递给str1
System.out.println(str);
}
public static void fun(String str1)<span style="white-space:pre"> </span>
{
str1="nihao";
}
}
注意:如果操作的数据是String那么就按照基本类型理解,只是传递值。
内存说明:
案例3:
class Info
{
private String msg = "hello";
public void setMsg(String msg)
{
this.msg = msg;
}
public String getMsg()
{
return this.msg;
}
}
public class TestPass
{
public static void main(String[] args)
{
Info d = new Info();
d.setMsg("world");
fun(d);
System.out.println(d.getMsg());
}
public static void fun(Info temp)
{
temp.setMsg("nihao");
}
}
结果:
本题目中的属性是类的对象,同样是引用传递。同第一题类似。
3.对象的比较
在java中,一个类的属性被封装之后,外部无法通过对象直接进行访问,但是类的对象被回传到类之中,那么就可以用对象进行访问。
一个类的对象在类本身可以不受封装的限制,直接用对象.属性操作。
案例:
class Info
{
private String msg = "hello";
public void setMsg(Info d) //接收本类的对象
{
d.msg= "world"; //直接访问私有属性,并不是this.属性访问
}
public String getMsg()
{
return this.msg;
}
}
public class TestPass
{
public static void main(String[] args)
{
Info d = new Info();
d.setMsg(d);
System.out.println(d.getMsg());
}
}
结果:
程序要求:判断两个Person对象是否是相等的?
class Person
{
//进行封装
private String name;
private int age;
//构造方法
public Person(String name, int age)
{
this.setName(name);
this.setAge(age);
}
//普通的方法
public void setName(String name)
{
this.name = name;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return this.name;
}
public int getAge()
{
return this.age;
}
}
public class TestPass
{
public static void main(String[] args)
{
Person per[] = new Person[] {new Person("张三", 20),new Person("da ",22)};
if (per[0].getName().equals(per[1].getName()) && per[0].getAge() == per[1].getAge() )
{
System.out.println("是同一个对象");
}
else
{
System.out.println("不是同一个对象");
}
}
}
结果
但是这种实现是存在问题的,并不是对象之间自己的比较,是通过第三方来实现的。我们的main函数其实就是那个第三方,是客户端的操作。
代码修改如下,使用上面的实例作为因子,在类中用方法进行比较,得出结果。
class Person
{
//进行封装
private String name;
private int age;
//构造方法
public Person(String name, int age)
{
this.setName(name);this.setAge(age);
}
//普通的方法
public void setName(String name)
{
this.name = name;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return this.name;
}
public int getAge()
{
return this.age;
}
public boolean compare(Person person)
{
if (person == null)
{
return false;
}
if (this == person)//两个对象== 比较的是地址值
{
return true;
}
//类中引用同类可以直接调用封装的属性
if (this.getName().equals(person.getName()) && this.getAge() == person.getAge() )
{
return true;
}
else
{
return false;
}
}
}
public class TestPass
{
public static void main(String[] args)
{
Person per[] = new Person[] {new Person("张三", 20),new Person("张三", 20)};
if( per[0].compare(null))
{
System.out.println("是同一个对象");
}
else
{
System.out.println("不是同一个对象");
}
}
}
结果:
4.引用的深入操作
任何实物发展都是由生活而来,面向对象描述的就是生活。
例如:一个人只有一套房。这是典型的一对一的关系,问题来了:如何建立类和类之间的对应关系。
案例:
class Person
{
//进行封装
private String name;
private int age;
private House house; //在类中建立其他类,一个人只有一套房子
//构造方法
public Person(String name, int age)
{
this.name = name ;
this.age = age ;
}
//普通的方法
public String getName()
{
return this.name;
}
public int getAge()
{
return this.age;
}
public void setHouse(House house)
{
this.house = house;
}
public House getHouse(House house)
{
return this.house;
}
}
class House
{
private String loc;
private Person person;
public House(String loc)
{
this.loc = loc;
}
public String getLoc()
{
return this.loc;
}
public void setPerson(Person person)
{
this.person = person;
}
public Person getPerson()
{
return this.person;
}
}
public class TestPass
{
public static void main(String[] args)
{
Person per = new Person("张三", 20);
House hou = new House("乳山"); //并没有完全实例化
per.setHouse(hou); //一个人有一套房子
hou.setPerson(per); //一个房子属于一个人
System.out.println("姓名:" + per.getName() + ",年龄:"+ per.getAge() //人找房子
+ ",住址:" + per.getHouse(hou).getLoc());
System.out.println("住址:" + hou.getLoc() + ",属于:" +hou.getPerson().getName()
+ ",年龄:" + hou.getPerson().getAge()); //房子找人
}
}
结果:
问题来了:如何表示传递关系,一个人有一个孩子,一个孩子有一套房子?
由于孩子也是人的类,所以在人的类中引入人的对象
案例:
class Person
{
//进行封装
private String name;
private int age;
private House house; //在类中建立其他类,一个人只有一套房子
private Person child;
//构造方法
public Person(String name, int age)
{
this.name = name ;
this.age = age ;
}
//普通的方法
public String getName()
{
return this.name;
}
public int getAge()
{
return this.age;
}
public void setChild(Person child)
{
this.child =child;
}
public Person getChild()
{
return this.child;
}
public void setHouse(House house)
{
this.house = house;
}
public House getHouse(House house)
{
return this.house;
}
}
class House
{
private String loc;
private Person person;
public House(String loc)
{
this.loc = loc;
}
public String getLoc()
{
return this.loc;
}
public void setPerson(Person person)
{
this.person = person;
}
public Person getPerson()
{
return this.person;
}
}
public class TestPass
{
public static void main(String[] args)
{
Person per = new Person("张三", 20);
House hou = new House("乳山"); //并没有完全实例化
Person chi = new Person("张涨", 2);
per.setChild(chi);
hou.setPerson(chi);
chi.setHouse(hou);
System.out.println("姓名:" + per.getName() + ",年龄:"+ per.getAge() //人找房子
+ ",孩子:" +chi.getName() +",年龄:"+ chi.getAge()
+ ",住址:" + chi.getHouse(hou).getLoc());
}
}
结果:
问题又来了:上面这个例子显然是一对一的关系,那么出现了一对多的关系该如何?
比如一个人有两本书,多的概念可以通过数组来表示
案例:
class Person
{
//进行封装
private String name;
private int age;
private Books[] books;
//构造方法
public Person(String name, int age)
{
this.name = name ;
this.age = age ;
}
//普通的方法
public String getName()
{
return this.name;
}
public int getAge()
{
return this.age;
}
public void setBooks(Books[] books)
{
this.books = books;
}
public Books[] getBooks()
{
return this.books;
}
}
class Books
{
private String title;
private Person person;
public Books(String title)
{
this.title = title;
}
public void setPerson(Person person)
{
this.person = person;
}
public Person getPerson()
{
return this.person;
}
public String getTitle()
{
return this.title;
}
}
public class TestPass
{
public static void main(String[] args)
{
Person per = new Person("张三", 20);
Books books[] = new Books[]{new Books("java"),new Books("c")};
per.setBooks(books); //人对应书
for (int i=0;i<books.length ; ++i)
{
books[i].setPerson(per); //书给人
System.out.println("姓名:" + books[i].getPerson().getName() + ",书名:" + books[i].getTitle());
}
}
}
结果
本次课程
非常重要,健健康康,快快乐乐。