Java 面向对象简单小练习!!!

qq_15869949 2014-06-18 11:25:17
问题一:
定义一个Person类,Person类的子类Student类。
Person类包含人的身份证号id表示,
写一个带一个参数的构造函数,
写一个取得身份证号ID的方法。
Student类包含学生的学号stuid,
写一个带两参数的构造函数,
写一个取得学生学号的方法。
定义一个PersonManage类,
此类中有一个包含3个元素的数组,
数组中每一个元素是一个Person类的对象的一个引用,
并且给数组的每一个元素创建实例对象,写一个返回数组中对象引用的方法。
同样定义一个StudentManage类,此类中有一个包含3个元素的数组,
数组中每一个元素是一个Student类的对象的一个引用,
并且给数组的每一个元素创建实例对象,
写一个返回数组中对象引用的方法。
在测试类中,生成一个StudentManage类的对象,
该对象调用返回数组中对象引用的方法取得1号对象
,最后返回该对象的身份证号码和学号。


问题二:
定义一个Student类,
该类中用方法introduce实现返回字符串“我是一个学生!”,
定义学生类的3个子类,分别是大学生类,中学生类,小学生类。
在每一个类中分别覆盖方法introduce,
输出对应的“我是一个**学生!”。
定义一个名字叫做多态的类,
该类中定义一个名字叫sayme的方法,
该方法的参数为学生,
实现调用3类不同学生的方法introduce。
在测试类中,生成3类学生的实例对象,生成多态类的一个实例对象,
然后该对象通过调用sayme方法显示输出三类学生。


麻烦大神帮忙写一下!!!希望能给加上注释,写详细点儿// /* 谢谢大家 */ 本人初学一个月
...全文
483 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
姜小白- 2014-06-18
  • 打赏
  • 举报
回复
引用 3 楼 u012724379 的回复:
初学一个月恰好可以写写这些
引用 2 楼 stonefeng 的回复:
不能当伸手党啊,伸手党学不会东西的,起码要自己写写,然后拿去运行,再找找毛病,找不出来再到这里来问。
引用 1 楼 cbxjj 的回复:
这个是给你练手的作业 不是拿这里来问的 话说都快放暑假了怎么还有作业
建议大家以后对这种伸手直接要代码的帖子直接不闻不问,然后让版主直接给关小黑屋。
-江沐风- 2014-06-18
  • 打赏
  • 举报
回复
初学一个月恰好可以写写这些
疯癫行者 2014-06-18
  • 打赏
  • 举报
回复
不能当伸手党啊,伸手党学不会东西的,起码要自己写写,然后拿去运行,再找找毛病,找不出来再到这里来问。
剑神一笑 2014-06-18
  • 打赏
  • 举报
回复
这个是给你练手的作业 不是拿这里来问的 话说都快放暑假了怎么还有作业
daxiang253 2014-06-18
  • 打赏
  • 举报
回复
引用 6 楼 abc_key 的回复:
代码给你,自己看吧。
public class Person {

	private String id;

	public Person(String id) {
		this.setId(id);
	}

	public Person() {
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
}
public class Student extends Person {

	private String stuid;

	public Student(String id, String stuid) {
		this.setId(id);
		this.setStuid(stuid);
	}

	public Student() {
	}

	public void introduce() {
		System.out.println("我是一个学生!");
	}

	public String getStuid() {
		return stuid;
	}

	public void setStuid(String stuid) {
		this.stuid = stuid;
	}
}

public class PersonManage {
	
	private static Person[] arr = new Person[3];
	
	private static PersonManage manage;
	
	private PersonManage(){
		Person p1 = new Person("id1");
		Person p2 = new Person("id2");
		Person p3 = new Person("id3");
		arr[0] = p1;
		arr[1] = p2;
		arr[2] = p3;
	}
	
	public static PersonManage getInstance(){
		if(manage == null){
			manage = new PersonManage();
		}
		return manage;
	}
	
	public Person getPerson(int idx){
		if(idx > arr.length-1){
			return null;
		}
		return arr[idx];
	}

}

public class StudentManage {
	private static Student[] arr = new Student[3];
	
	private static StudentManage manage;
	
	private StudentManage(){
		Student s1 = new Student("id1", "stuid1");
		Student s2 = new Student("id2", "stuid2");
		Student s3 = new Student("id3", "stuid3");
		arr[0] = s1;
		arr[1] = s2;
		arr[2] = s3;
	}
	
	public static StudentManage getInstance(){
		if(manage == null){
			manage = new StudentManage();
		}
		return manage;
	}
	
	public Student getStudent(int idx){
		if(idx > arr.length-1){
			return null;
		}
		return arr[idx];
	}
}

/**
 * 大学生类
 */
public class CollegeStudent extends Student {

	public CollegeStudent(String id, String stuid) {
		super(id, stuid);
	}
	
	public CollegeStudent(){}

	@Override
	public void introduce() {
		System.out.println("我是一个大学生!");
	}

}

/**
 * 中学生类
 */
public class MiddleStudent extends Student {

	public MiddleStudent(String id, String stuid) {
		super(id, stuid);
	}
	
	public MiddleStudent(){}

	@Override
	public void introduce() {
		System.out.println("我是一个中学生!");
	}
}

/**
 * 小学生类
 */
public class PrimaryStudent extends Student {

	public PrimaryStudent(String id, String stuid) {
		super(id, stuid);
	}
	
	public PrimaryStudent(){}

	@Override
	public void introduce() {
		System.out.println("我是一个小学生!");
	}
}

/**
 * 多态类
 */
public class PolymorphismClass {
	
	public void sayme(Student student){
		student.introduce();
	}

}
/**
 * 测试
 */
public class Test0 {

	public static void main(String[] args) {
		
		//问题一
		StudentManage manage = StudentManage.getInstance();
		Student s = manage.getStudent(0);
		System.out.println(s.getId() + " " + s.getStuid());
		
		//问题二
		CollegeStudent cs = new CollegeStudent();
		MiddleStudent ms = new MiddleStudent();
		PrimaryStudent ps = new PrimaryStudent();
		PolymorphismClass poly = new PolymorphismClass();
		poly.sayme(cs);
		poly.sayme(ms);
		poly.sayme(ps);
	}

}
这个OK?....
低调Di程序猿 2014-06-18
  • 打赏
  • 举报
回复
楼上已给
xlight2023 2014-06-18
  • 打赏
  • 举报
回复
代码给你,自己看吧。
public class Person {

	private String id;

	public Person(String id) {
		this.setId(id);
	}

	public Person() {
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
}
public class Student extends Person {

	private String stuid;

	public Student(String id, String stuid) {
		this.setId(id);
		this.setStuid(stuid);
	}

	public Student() {
	}

	public void introduce() {
		System.out.println("我是一个学生!");
	}

	public String getStuid() {
		return stuid;
	}

	public void setStuid(String stuid) {
		this.stuid = stuid;
	}
}

public class PersonManage {
	
	private static Person[] arr = new Person[3];
	
	private static PersonManage manage;
	
	private PersonManage(){
		Person p1 = new Person("id1");
		Person p2 = new Person("id2");
		Person p3 = new Person("id3");
		arr[0] = p1;
		arr[1] = p2;
		arr[2] = p3;
	}
	
	public static PersonManage getInstance(){
		if(manage == null){
			manage = new PersonManage();
		}
		return manage;
	}
	
	public Person getPerson(int idx){
		if(idx > arr.length-1){
			return null;
		}
		return arr[idx];
	}

}

public class StudentManage {
	private static Student[] arr = new Student[3];
	
	private static StudentManage manage;
	
	private StudentManage(){
		Student s1 = new Student("id1", "stuid1");
		Student s2 = new Student("id2", "stuid2");
		Student s3 = new Student("id3", "stuid3");
		arr[0] = s1;
		arr[1] = s2;
		arr[2] = s3;
	}
	
	public static StudentManage getInstance(){
		if(manage == null){
			manage = new StudentManage();
		}
		return manage;
	}
	
	public Student getStudent(int idx){
		if(idx > arr.length-1){
			return null;
		}
		return arr[idx];
	}
}

/**
 * 大学生类
 */
public class CollegeStudent extends Student {

	public CollegeStudent(String id, String stuid) {
		super(id, stuid);
	}
	
	public CollegeStudent(){}

	@Override
	public void introduce() {
		System.out.println("我是一个大学生!");
	}

}

/**
 * 中学生类
 */
public class MiddleStudent extends Student {

	public MiddleStudent(String id, String stuid) {
		super(id, stuid);
	}
	
	public MiddleStudent(){}

	@Override
	public void introduce() {
		System.out.println("我是一个中学生!");
	}
}

/**
 * 小学生类
 */
public class PrimaryStudent extends Student {

	public PrimaryStudent(String id, String stuid) {
		super(id, stuid);
	}
	
	public PrimaryStudent(){}

	@Override
	public void introduce() {
		System.out.println("我是一个小学生!");
	}
}

/**
 * 多态类
 */
public class PolymorphismClass {
	
	public void sayme(Student student){
		student.introduce();
	}

}
/**
 * 测试
 */
public class Test0 {

	public static void main(String[] args) {
		
		//问题一
		StudentManage manage = StudentManage.getInstance();
		Student s = manage.getStudent(0);
		System.out.println(s.getId() + " " + s.getStuid());
		
		//问题二
		CollegeStudent cs = new CollegeStudent();
		MiddleStudent ms = new MiddleStudent();
		PrimaryStudent ps = new PrimaryStudent();
		PolymorphismClass poly = new PolymorphismClass();
		poly.sayme(cs);
		poly.sayme(ms);
		poly.sayme(ps);
	}

}
-江沐风- 2014-06-18
  • 打赏
  • 举报
回复
引用 4 楼 magi1201 的回复:
[quote=引用 3 楼 u012724379 的回复:] 初学一个月恰好可以写写这些
引用 2 楼 stonefeng 的回复:
不能当伸手党啊,伸手党学不会东西的,起码要自己写写,然后拿去运行,再找找毛病,找不出来再到这里来问。
引用 1 楼 cbxjj 的回复:
这个是给你练手的作业 不是拿这里来问的 话说都快放暑假了怎么还有作业
建议大家以后对这种伸手直接要代码的帖子直接不闻不问,然后让版主直接给关小黑屋。[/quote] 你去申请个版主呗,,

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧