java 写一个类Student,包含三个属性学号id,姓名name和年龄age;
要求覆盖基类Object中的ToString()方法和Equals()方法,使得直接输出Student对象时输出的是对象的id,name和age信息;并且当两个对象的学号...
要求覆盖基类Object中的ToString()方法和Equals()方法,使得直接输出Student对象时输出的是
对象的id,name和age信息;并且当两个对象的学号相同时认为它们相等;
然后写一个主方法测试以上定义。 展开
对象的id,name和age信息;并且当两个对象的学号相同时认为它们相等;
然后写一个主方法测试以上定义。 展开
8个回答
展开全部
public class Student {
public String id;
public String name;
public int age;
public Student(String id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Student(String id) {
super();
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
public class StuTest {
public static void main(String[] args) {
Student stu1=new Student("1234", "张三", 12);
Student stu2=new Student("1234");
System.out.println(stu2.equals(stu1));
}
}
public String id;
public String name;
public int age;
public Student(String id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Student(String id) {
super();
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
public class StuTest {
public static void main(String[] args) {
Student stu1=new Student("1234", "张三", 12);
Student stu2=new Student("1234");
System.out.println(stu2.equals(stu1));
}
}
展开全部
toString和equals方法可以用eclipse自动工具生成
package day01;
public class Student {
//属性
private int id;
private String name;
private int age;
//get和set方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
//equals方法
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
//toString方法
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age
+ "]";
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//类就不给你写了
public String toString(){
return "id="+this.id+"\tname="+this.name+"\tage="+this.age;
}
public boolean equals(Object obj){
Student stu=(Student)obj;
if(stu.id==this.id){
return true;
}
else{
return false;
}
}
//main方法中调用,声明stu1,stu2两个对象
System.out.println(stu1.toString());
System.out.println(stu1.equals(stu2));
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Student {
private String id;
private String name;
private int age;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.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;
final Student other = (Student) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
@Override
public String toString() {
return "学号:" + id + " 姓名:" + name + " 年龄:" + age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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 static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
s1.setId("1");
s1.setAge(2);
s1.setName("小王");
s2.setId("2");
s2.setAge(2);
s2.setName("小张");
s3.setId("1");
s3.setAge(4);
s3.setName("小王");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Student{
int id;
String name;
int age;
@Override
public String(){
return "id:"+id+" name:"+name+" age:"+age;
}
@Override
public boolean equals(Student stu){
if(this.id==stu.id){
return true;
}
return false;
}
public static void main(String[] args) {
Student s1=new Student();
s1.id=1;
s1.name="小明";
s1.age=10;
Student s2=new Student();
s2.id=3;
s2.name="小红";
s2.age=10;
System.out.println(s1);
if(s1.equals(s2)){
System.out.println("相同");
}else{
System.out.println("不相同");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询