通过foo获取func的属性,可以不在同一个文件中也可以获取
最主要的内容就是main方法里面的内容,比较绕需要慢慢理解
public class ho{
public static void main (String[]args) {
Foo foo = new Foo("狗比",5); //声明一个对象,并传参
Func func = new Func("jisdnv",18);
foo.setFunc(func); //把func对象作为参数传进方法里面
func.setFunc(foo);
System.out.println(foo.getFunc().getName()); //通过foo获取func的属性
System.out.println(func.getFoo().getName());
}
}
class Foo{
private String name;
private int age;
static String xiaopang = "小胖";
private Func func; //声明一个私有化对象
public Func getFunc() {
return func;
}
public void setFunc(Func func) {
this.func = func;
}
public Foo(){}
public Foo(String name,int age){
this.name=name;
this.age=age;
}
public static void setXiaopang(String xiaopang) {
Foo.xiaopang=xiaopang;
}
public static String getXiaopang() {
return xiaopang;
}
public void setName(String name) {
this.name=name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age=age;
}
public int getAge() {
return age;
}
public void mun() {
System.out.println(name);
System.out.println(age);
}
}
class Func{
private String name;
private int age11;
private Foo foo; //声明一个私有化对象
public Foo getFoo() {
return foo;
}
public void setFunc(Foo foo) {
this.foo = foo;
} Func(){}
public Func(String name,int age11){
this.name=name;
this.age11=age11;
}
public void setName(String name) {
this.name=name; //this.name是对象的name
}
public String getName() {
return name;
}
public void setAge11(int age11) {
this.age11=age11;
}
public int getAge11() {
return age11;
}
public void mun() {
System.out.println(name);
System.out.println(age11);
}
}
键盘敲烂,月薪过万