1.聚合
1.在一个类中将被聚合元素作为其属性
如果所有类都会用到一个类的对象,则把它作为属性;
在任何方法的任何类,都可以创建对象;
package 聚合;
public class Car {
public Wheel we=new Wheel();
public void run(){
we.zhuan();
System.out.println("跑");
}
public static void main(String[] args) {
}
}
package 聚合;
public class Wheel{
private String name="邓普璐";
private String color="黑色";
private int size=100;
public void setName(String name){
this.name=name;
}
public void setColor(String color){
this.color=color;
}
public void size(int size){
this.size=size;
}
public void zhuan(){
System.out.println("轮胎是:"+name+"颜色是"+color+"尺寸是"+size);
}
public static void main(String[] args) {
}
}
package 聚合;
public class Test {
public static void main(String[] args) {
Car car=new Car();
car.run();
}
}
结果:轮胎是:邓普璐颜色是黑色尺寸是100
跑
2.若在Car类中添加方法,改变属性的值,则值就改变。
因为都是we对象的值
package 聚合;
public class Car {
public Wheel we=new Wheel();
public void run(){
we.zhuan();
System.out.println("跑");
}
public void huantai(){
we.setName("米其林");
we.setColor("绿色");
we.setSize(200);
}
public static void main(String[] args) {
}
}
package 聚合;
public class Wheel{
private String name="邓普璐";
private String color="黑色";
private int size=100;
public void setName(String name){
this.name=name;
}
public void setColor(String color){
this.color=color;
}
public void setSize(int size){
this.size=size;
}
public void zhuan(){
System.out.println("轮胎是:"+name+"颜色是"+color+"尺寸是"+size);
}
public static void main(String[] args) {
}
}
package 聚合;
public class Test {
public static void main(String[] args) {
Car car=new Car();
car.huantai();
car.run();
}
}
结果:轮胎是:米其林颜色是绿色尺寸是200
跑
3.在run()方法中再创建一个对象,Wheel we=new Wheel();
package 聚合;
public class Car {
public Wheel we=new Wheel();
public void run(){
Wheel we=new Wheel();
we.zhuan();
System.out.println("跑");
}
public void huantai(){
we.setName("米其林");
we.setColor("绿色");
we.setSize(200);
}
public static void main(String[] args) {
}
}
结果为:轮胎是:邓普璐颜色是黑色尺寸是100
跑
因为在方法里创建相当于局部变量,与另一个we对象不是一个;
4.错误:
package 聚合;
public class Car {
public Wheel we;
public void run(){
Wheel we=new Wheel();
we.zhuan();
System.out.println("跑");
}
public void huantai(){
we.setName("米其林");
we.setColor("绿色");
we.setSize(200);
}
public static void main(String[] args) {
}
}
错误:NullPointerException 空指针异常;
产生原因:当对象为Null时,还要调用其下的属性或方法;
private Wheel we;
we是引用类型,所有引用类型没有赋值时都是null;
5.通过方法赋值
package 聚合;
public class Car {
public Wheel we;
public void setWe(Wheel we){
this.we=we;
}
public Wheel getWe(){
return we;
}
public void run(){
we.zhuan();
System.out.println("跑");
}
public void huantai(){
we.setName("米其林");
we.setColor("绿色");
we.setSize(200);
}
public static void main(String[] args) {
}
}
package 聚合;
public class Test {
public static void main(String[] args) {
Car car=new Car();
Wheel ee=new Wheel();
car.setWe(ee);
car.huantai();
car.run();
Wheel ww=car.getWe();
}
}
car.setWe(ee);
6.通过构造方法赋值:
package 聚合;
public class Car {
public Wheel we;
public Car(Wheel we){
this.we=we;
}
public void run(){
we.zhuan();
System.out.println("跑");
}
public void huantai(){
we.setName("米其林");
we.setColor("绿色");
we.setSize(200);
}
public static void main(String[] args) {
}
}
<pre class="java" name="code">package 聚合;
public class Test {
public static void main(String[] args) {
Wheel ee=new Wheel();
Car car=new Car(ee);
car.huantai();
car.run();
}
}