在学习泛型中遇到的多态的一个小问题详解编程语言

在学习泛型的过程中,我需要创建person、worker、student 一个父类两个子类,但是在创建过程遇到一些问题,先上代码

package cn.haiyisoft.domain; 
 
public class PersonForGeneric implements Comparable<PersonForGeneric> { 
 
	private String name; 
	private int age; 
 
	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; 
	} 
 
	@Override 
	public int hashCode() { 
		final int prime = 31; 
		int result = 1; 
		result = prime * result + age; 
		result = prime * result + ((name == null) ? 0 : name.hashCode()); 
		return result; 
	} 
 
	@Override 
	public boolean equals(Object obj) { 
		if (this == obj) 
			return true; 
		if (obj == null) 
			return false; 
		if (getClass() != obj.getClass()) 
			return false; 
		PersonForGeneric other = (PersonForGeneric) obj; 
		if (age != other.age) 
			return false; 
		if (name == null) { 
			if (other.name != null) 
				return false; 
		} else if (!name.equals(other.name)) 
			return false; 
		return true; 
	} 
 
	public PersonForGeneric(String name, int age) { 
		super(); 
		this.name = name; 
		this.age = age; 
	} 
 
	public PersonForGeneric() { 
		super(); 
	} 
 
	@Override 
	public int compareTo(PersonForGeneric o) { 
		// TODO Auto-generated method stub 
		if (this.getName().equals(o.getName())) 
			return this.getAge() - o.getAge(); 
		else 
			return this.getName().compareTo(o.getName()); 
 
	} 
 
} 

package cn.haiyisoft.domain; 
 
public class WorkerForGeneric extends PersonForGeneric { 
 
	private String name; 
	private int age; 
 
	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 WorkerForGeneric(String name, int age) { 
		super(); 
		this.name = name; 
		this.age = age; 
	} 
 
	@Override 
	public int hashCode() { 
		final int prime = 31; 
		int result = super.hashCode(); 
		result = prime * result + age; 
		result = prime * result + ((name == null) ? 0 : name.hashCode()); 
		return result; 
	} 
 
	@Override 
	public boolean equals(Object obj) { 
		if (this == obj) 
			return true; 
		if (!super.equals(obj)) 
			return false; 
		if (getClass() != obj.getClass()) 
			return false; 
		WorkerForGeneric other = (WorkerForGeneric) obj; 
		if (age != other.age) 
			return false; 
		if (name == null) { 
			if (other.name != null) 
				return false; 
		} else if (!name.equals(other.name)) 
			return false; 
		return true; 
	} 
 
	@Override 
	public String toString() { 
		return "WorkerForGeneric [name=" + name + ", age=" + age + "]"; 
	} 
 
		 
	 
	} 
 
 

package cn.haiyisoft.domain; 
 
public class StudentForGeneric extends PersonForGeneric { 
 
	private String name; 
 
	private int age; 
 
	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; 
	} 
 
	@Override 
	public int hashCode() { 
		final int prime = 31; 
		int result = 1; 
		result = prime * result + age; 
		result = prime * result + ((name == null) ? 0 : name.hashCode()); 
		return result; 
	} 
 
	@Override 
	public boolean equals(Object obj) { 
		if (this == obj) 
			return true; 
		if (obj == null) 
			return false; 
		if (getClass() != obj.getClass()) 
			return false; 
		StudentForGeneric other = (StudentForGeneric) obj; 
		if (age != other.age) 
			return false; 
		if (name == null) { 
			if (other.name != null) 
				return false; 
		} else if (!name.equals(other.name)) 
			return false; 
		return true; 
	} 
 
	public StudentForGeneric(String name, int age) { 
		super(); 
		this.name = name; 
		this.age = age; 
	} 
 
	@Override 
	public String toString() { 
		return "StudentForGeneric [name=" + name + ", age=" + age + "]"; 
	} 
 
	 
} 

person类实现了comparable接口,worker和student类继承了person类,按照正常的设计来讲,worker和student类不需要再定义name。age以及相应的set和get方法,但是我在两个类里又重新定义了name和age 这就导致了person类中的compareto方法有了些问题

正常来讲compareto方法里面应该是这样的

public int compareTo(PersonForGeneric o) { 
		// TODO Auto-generated method stub 
		if (this.name.equals(o.name)) 
			return this.age - o.age; 
		else 
			return this.name.compareTo(o.name); 
 
	}

但是如果在treeset中传入子类,那么就会使得this.name等指向了父类的name,但是父类的name是没有被赋值的(成员变量没有多态)最后运行时会报空指针,如果一定要坚持的话,那么compareto方法要改成这样

public int compareTo(PersonForGeneric o) { 
		// TODO Auto-generated method stub 
		if (this.getName().equals(o.getName())) 
			return this.getAge() - o.getAge(); 
		else 
			return this.getName().compareTo(o.getName()); 
 
	}

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/17688.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论