class People
{
//属性 成员变量 对象属性
private String name;
private int age;
private char sex;
private String contry;
private String hairColor;
private Father f;
//拷贝构造 通过已知对象来复制得到另外一对象
People(People p)
{
this.name = p.name;
this.age = p.age;
this.sex = p.sex;
this.contry = p.contry;
this.hairColor = p.hairColor;
this.f = new Father(p.f);
}
People()
{
}
//成员方法 对象功能
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setAge(int nAge)
{
int age; //方法内部声明的变量:局部变量 局部变量不能用访问控制符修饰 使用之前必须初始化
age = 0;
age++;
this.age = nAge; //允许局部变量和成员变量同名 区分方法:成员变量用this.来调用
}
public int getAge()
{
return age;
}
public void setSex(char nSex)
{
sex = nSex;
}
public char getSex()
{
return sex;
}
public void setContry(String nContry)
{
contry = nContry;
}
public String getContry()
{
return contry;
}
public void setHairColor(String nHairColor)
{
hairColor = nHairColor;
}
public String getHairColor()
{
return hairColor;
}
public void setF(Father f)
{
this.f = f;
}
public Father getF()
{
return this.f;
}
//吃
public void eat(Food food)
{
System.out.println(hairColor+"的"+contry+"吃"+food);
}
//喝
public void drink(Drink drink)
{
System.out.println(hairColor+"的"+contry+"喝"+drink);
}
//说
public void speak(Language lang)
{
System.out.println(hairColor+"的"+contry+"说"+lang);
}
}
class Father
{
private String name;
Father(String name)
{
this.name = name;
}
Father(Father f)
{
this.name = f.name;
}
public void setName(String name)
{
this.name = name;
}
public String toString()
{
return "父亲名字:"+name;
}
};
class Food
{
private String type;
public void setType(String type)
{
this.type = type;
}
public String getType()
{
return this.type;
}
public String toString()
{
return type;
}
};
class Drink
{
private String type;
public void setType(String type)
{
this.type = type;
}
public String getType()
{
return this.type;
}
public String toString()
{
return type;
}
};
class Language
{
private String type;
Language(String type)
{
this.type = type;
}
public String getType()
{
return this.type;
}
public String toString()
{
return type;
}
};
class PeopleTest
{
public static void main(String[] args)
{
// People p1 = new People();
// p1.setName("张三");
// p1.setAge(23);
//
// People p2 = new People();
// p2.setAge(50);
//
// System.out.println(p1.getAge());
People p1 = new People();
People p2 = new People();
p1.setHairColor("黄头发");
p2.setHairColor("黑头发");
p1.setContry("葡萄牙人");
p2.setContry("朝鲜人");
Food sandWitch = new Food();
sandWitch.setType("三明治");
p1.eat(sandWitch);
//p1.drink();
//p1.speak();
Drink d = new Drink();
d.setType("可乐");
p2.drink(d);
Language chinese = new Language("汉语");
p2.speak(chinese);
Father f = new Father("乔丹");
p1.setF(f);
People p = new People(p1);
p.setContry("中国人");
System.out.println(p1.getContry());
Father ff = p.getF();
ff.setName("拉塞尔");
System.out.println(p.getF());
System.out.println(p1.getF());
}
}