继承和多态的习题

  1. 需求
  1. 定义一个(Person)类,包含属性:姓名,年龄,性别;写好相关的方法,能够构造的时候初始化或者后续修改属性值。
  2. 定义一个Employee类继承于Person类,包含新的属性:职位;写好相关的方法,能够构造的时候初始化或者后续修改属性值,重载equals方法,规则:当两个对象的姓名,年龄,性别,职位都相等时返回true,否则返回false。
  3. 写一个测试类进行相关测试(要求可以打印对象的信息,能够比较对象是否"相等")。

效果:

 

package zuoyejicheng;

public class  Person {
	String name;//姓名
	int age;//年龄
	String sex;//性别
	public Person(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	public boolean equals(String name1,String name2) {
		return true;
		
	} 
	

}

 

package zuoyejicheng;

public class Employee extends Person {
	

	String job;//职位
	
	public Employee(String name, int age, String sex,String job) {
		super(name, age, sex);
		this.job=job;
		// TODO Auto-generated constructor stub
					
			}
	
	
	public String getJob() {
		return job;
	}


	public void setJob(String job) {
		this.job = job;
	}


	public 	boolean equals (Employee e) {
		if (this.getName().equals(e.getName())&&this.getAge()==e.getAge()&&this.getSex().equals(e.getSex())&& this.getJob().equals(e.getJob())) {
			return true;
			
		}else {
			return false;
			
		}
	}
	public String show() {
		
		retur
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值