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());
}
}