发布网友 发布时间:2022-04-23 09:59
共5个回答
热心网友 时间:2023-10-10 13:57
package test;
public class Student {
private String name;
private String id;
private String clazz;
private int age;
private String address;
/**
* sayHello方法
*/
public void sayHello() {
System.out.println("学号为" + this.id + "的同学的具体信息如下:");
System.out.println("姓名:" + this.name);
System.out.println("班级:" + this.clazz);
System.out.println("年龄:" + this.age);
System.out.println("家庭住址:" + this.address);
}
/**
* 测试方法
*
* @param args
*/
public static void main(String[] args) {
// 第一问
Student student = new Student();
student.setAddress("百度知道");
student.setAge(1);
student.setClazz("一班");
student.setId("071251000");
student.setName("lsy605604013");
student.sayHello();
// 第二问
Student studentNew = new Student();
studentNew.setAddress("搜搜知道");
studentNew.setAge(2);
studentNew.setClazz("二班");
studentNew.setId("071251001");
studentNew.setName("lady");
if (student.getAge() < studentNew.getAge())
studentNew.sayHello();
else if (student.getAge() > studentNew.getAge())
student.sayHello();
else
System.out.println("两人一样大");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
热心网友 时间:2023-10-10 13:57
java:
@Test
void contextLoads() {
Student student = new Student ("张三","1","三年级四班",18,"xxx");
Student student2 = new Student ("张三","1","三年级四班",10,"xxx");
System.out.println (sayHello(student,student2));
}
private Student sayHello(Student student, Student student2) {
return student.getAge ()>student2.getAge ()? student:student2;
}
// 编写一个student类,其中包括(姓名、学号、班级、年龄、家庭住址)和取得这些
// 属性的方法,再增加一个sayHello()方法,用于输出学生的详细信息(1)创建Student
// 类的对象,编写程序测试这些方法。(2)创建两个学生对象,并比较二者的年龄,输出
// 年龄较大者的学生信息
}
@Data
class Student{
private String name; //姓名
private String stuId; //学号
private String stuClass;//班级
private Integer age;//年龄
private String homeAddr;//住址
public Student() {
}
public Student(String name, String stuId, String stuClass, Integer age, String homeAddr) {
this.name = name;
this.stuId = stuId;
this.stuClass = stuClass;
this.age = age;
this.homeAddr = homeAddr;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", stuId='" + stuId + '\'' +
", stuClass='" + stuClass + '\'' +
", age=" + age +
", homeAddr='" + homeAddr + '\'' +
'}';
}
}
热心网友 时间:2023-10-10 13:58
/**
* student类
*
*/
public class Student {
private String name;
private String id;
private String clazz;
private int age;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Student() {
}
public Student(String name, String id, String clazz, int age,
String address) {
this.name = name;
this.id = id;
this.clazz = clazz;
this.age = age;
this.address = address;
}
public void sayHello(){
System.out.println(getName()+"的详细信息如下:");
System.out.println("学号:"+getId());
System.out.println("班级:"+getClazz());
System.out.println("年龄:"+getAge());
System.out.println("地址:"+getAddress());
}
}
/**
* 测试类
*
*/
public class Test {
public static void main(String[] args) {
//Student st1 = new Student("张三", "0000001", "一班", 15, "张三家");
//Student st2 = new Student("李四", "0000002", "二班", 16, "李四家");
Student st1 = new Student();
Student st2 = new Student();
st1.setName("张三");
st1.setId("000001");
st1.setClazz("一班");
st1.setAge(15);
st1.setAddress("张三家");
st2.setName("李四");
st2.setId("000002");
st2.setClazz("二班");
st2.setAge(16);
st2.setAddress("李四家");
if(st1.getAge()>st2.getAge()){
System.out.println(st1.getName()+"年龄比"+st2.getName()+"大,下面将输出"+st1.getName()+"的详细信息");
st1.sayHello();
}else if(st1.getAge()<st2.getAge()){
System.out.println(st2.getName()+"年龄比"+st1.getName()+"大,下面将输出"+st2.getName()+"的详细信息");
st2.sayHello();
}else {
System.out.println(st1.getName()+"和"+st2.getName()+"年龄相同");
}
}
}
热心网友 时间:2023-10-10 13:59
按照OO的思想,比较操作实现接口比较好。代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MyCSharpProjects
{
class Student:IComparable
{
#region private fields
private string name;
private string id;
private string grade;
private int age;
private string address;
#endregion
#region public properties
public string Name
{
get { return name; }
set { name = value; } // here you could apply pattern or regular expression to the name.
}
// ...其他属性类型,照抄即可。
#endregion
#region protected method
public override string ToString()
{
StringBuilder strbuilder = new StringBuilder();
strbuilder.Append("Name :");
strbuilder.AppendLine(Name);
// 其他属性找这个抄都写到strbuilder中
return strbuilder.ToString();
}
#endregion
#region public methods
public void SayHello()
{
this.ToString();
}
public override bool Equals(object obj)
{
if (CompareTo(obj) == 0)
return true;
return false;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public virtual int CompareTo(object obj)
{
Student s = obj as Student;
if (s != null)
{
if (s == this)
{
return 0;
}
else
{
if (s.Name == this.Name && s.id == this.id && s.grade == this.grade && s.age == this.age && s.address == this.address)
return 0;
else if (s.age > this.age)
{
return 1;
}
else
{
return -1;
}
}
}
throw new Exception("invalid type");
}
#endregion
}
}
热心网友 时间:2023-10-10 13:59
我能独自完成作业!这里我的定义和张老师的不同, 张老师是鼓励大家讨论的其实也就是两种方式吧, 一种就是用实例, 就是给你一个例子, 要你用