java 写一个类Student,包含三个属性学号id,姓名name和年龄age;
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 写一个类Student,包含三个属性学号id,姓名name和年龄age;相关的知识,希望对你有一定的参考价值。
要求覆盖基类Object中的ToString()方法和Equals()方法,使得直接输出Student对象时输出的是
对象的id,name和age信息;并且当两个对象的学号相同时认为它们相等;
然后写一个主方法测试以上定义。
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));
参考技术A 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
+ "]";
参考技术B //类就不给你写了
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)); 参考技术C 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));
参考技术D 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("不相同");
java定义一个学生类要求学号,姓名,年龄,三个成员变量
两个构造方法参数分别为:学号、姓名和;学号、姓名、年龄。要使用THIS关键字,将接收到的实参的值为每一个对应的成员变量赋值。请大家帮帮忙在线等
class Studentprivate int STU_NUM;
private String STU_NAME;
private int STU_AGE;
public Student(int NUM,String NAME)
this.STU_NAME=NAME;
this.STU_NUM=NUM;
public Student(int NUM,String NAME,int AGE)
this.STU_NAME=NAME;
this.STU_NUM=NUM;
this.STU_AGE=AGE;
参考技术A public class Student
public int Sno; //学号
public String Sname; //Student Name;
public int Sage; //年龄
public Student()
//无参构造器
public Student(int Sno,String Sname, int Sage)
this.Sno=Sno;
this.Sname=Sname;
this.Sage=Sage;
如果要封装, 将属性改为 private 的话 继续追问 参考技术B public class Student
private String number;
private int age;
private String name;
Student(String number,String name)
this.number = number;
this.name = name;
Student(String number,String name,int age)
this.number = number;
this.name = name;
this.age = age;
public String getNumber()
return number;
public void setNumber(String number)
this.number = number;
public int getAge()
return age;
public void setAge(int age)
this.age = age;
public String getName()
return name;
public void setName(String name)
this.name = name;
以上是关于java 写一个类Student,包含三个属性学号id,姓名name和年龄age;的主要内容,如果未能解决你的问题,请参考以下文章
一个学生数据表student,包含三个字段:no(学号),姓名(name),成绩(score)。
用java语句,1、建立一个数据库student,在数据库中建一个student表,表中至少有学号、姓名、专业三个字段