类和对象(一)

来源:互联网 发布:io域名怎么注册 编辑:程序博客网 时间:2024/05/20 23:07

定义学校对象,输出学校信息

要求:(1)学校至少有5个组成部分;如:学校由老师、学生、建筑...组成,每个对象都是一个单独的java文件
如:Student.java;Teacher.java;Building.java...

(2) 在Test.java中输出学校信息
包含:至少3个老师信息;至少3个学生信息;至少2个建筑信息...
输出格式:
---------------------------------------------
School school = new School();
……
System.out.println(school);
---------------------------------------------

1.学生类

 public class Student{ //学生的特征,Student类的属性值 String stuName = "代冬";//学生姓名 String stuGender = "男";//学生性别 String stuAge = "23";//学生年龄 String stuNo = "201107010101";//学生学号 }

2.教师类

public class Teacher{  //Teacher类中的属性值 String tName = "老陈";//教师姓名 String tNo = "1001";//教师工号 String tGender = "男";//教师性别 String tAge = "46";//教师年龄 }

3.建筑类

public class Building{     //Building类中的属性值 String buildName = "教学楼";//建筑物的名字     String buildHeight = "20米";//建筑物的高度 String buildArea = "1500平方米";//建筑物的面积  }

4.植物类

public class Plant{ //Plant类的属性值 String plantName = "松树";//植物名称 String plantHeight = "10米";//植物高度 }

5.动物类

public class Animal{ //Animal类的属性值 String aniKinds = "小猫、小狗";//动物种类 String aniAmount = "很多";//动物数量 }

6.学校类

public class School{ Student stu = new Student();//创建Student类的对象stu Teacher teach = new Teacher();//创建Teacher类的对象teach Building build = new Building();//创建Building类的对象build Plant plant = new Plant();//创建Plant类的对象plant Animal ani = new Animal();//创建Animal类的对象ani //创建toString类,返回一些字符串到Test类中main方法 public String toString(){     return "学生姓名:" + stu.stuName + " 学生年龄:" + stu.stuAge + " 学生性别:" + stu.stuGender + " 学生学号:" + stu.stuNo + "\n" + "教师姓名:" + teach.tName + " 教师工号:" + teach.tNo + " 教师性别:" + teach.tGender + " 教师年龄:" + teach.tAge + "\n" + "建筑名称:" + build.buildName + " 建筑高度" + build.buildHeight + " 建筑面积:" + build.buildArea + "\n" + "植物名称:" + plant.plantName + " 植物高度:" + plant.plantHeight + "\n" + "动物名称:" + ani.aniKinds + "动物数量" + ani.aniAmount + "\n"; } }

7.主方法所在的Test类

public class Test{ public static void main(String [] args){//main方法    School school = new School();//定义一个School类的对象schoolSystem.out.println(school);//输出school对象的返回值 } }


 

0 0
原创粉丝点击