1. 程式人生 > >java對象封裝的理解

java對象封裝的理解

int [] rgs think his ati pub clas java對象


class A{
private String name;

private String value;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public A(String name){
this.name = name;
}

@Override
public String toString() {
return "A{" +
"name=‘" + name + ‘\‘‘ +
", value=‘" + value + ‘\‘‘ +
‘}‘;
}
}
class B{
private Object data;

public Object getData() {
return data;
}

public void setData(Object data) {
this.data = data;
}

@Override
public String toString() {
return "B{" +
"data=" + data +
‘}‘;
}
}
public class ThinkTest {
public static void main(String[] args) {
A a1 = new A("第一個A對象");
System.out.println(a1);
B b = new B();
b.setData(a1);
System.out.println(b);
a1 = new A("第二個A對象");
a1.setValue("success");
System.out.print(b);
}
}

A{name=‘第一個A對象‘, value=‘null‘}
B{data=A{name=‘第一個A對象‘, value=‘null‘}}
B{data=A{name=‘第一個A對象‘, value=‘null‘}}

java對象封裝的理解