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;
}
}
按照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
}
}
/**
* 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()+"年龄相同");
}
}
}
我能独自完成作业!这里我的定义和张老师的不同, 张老师是鼓励大家讨论的其实也就是两种方式吧, 一种就是用实例, 就是给你一个例子, 要你用
603642367的程序就不错。
这个应该是练手 的题目, 楼主还是自己写写吧
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....