深层次拷贝

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



	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值