您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 2.Java第六章程序练习
1.定义一个Person类,含姓名、性别、年龄等字段;继承Person类设计Teacher类,增加职称、部门等字段;继承Person类设计Student类,增加学号、入学时间、专业等字段。定义各类的构造方法和toString()方法,并分别创建对象进行测试。packagea.b.c;classperson{Stringname;charsex;intage;publicperson(Stringname,charsex,intage){this.name=name;this.sex=sex;this.age=age;}}classStudentextendsperson{Stringno;Stringenter;Stringmajor;publicStudent(Stringname,charsex,intage,Stringno,Stringenter,Stringmajor){super(name,sex,age);this.no=no;this.enter=enter;this.major=major;}publicStringtoString(){returnname+,+sex+,+no+,+enter+,+major;}}classTeacherextendsperson{Stringpro;Stringdepartment;publicTeacher(Stringname,charsex,intage,Stringpro,Stringdepartment){super(name,sex,age);this.pro=pro;this.department=department;}publicStringtoString(){returnname+,+sex+,+age+,+pro+,+department;}}publicclassex6_2{publicstaticvoidmain(String[]args){Studentno1=newStudent(no1,W,20,001,2009/12,computer);System.out.println(学生的基本信息为:+no1);Teachert1=newTeacher(张三,M,43,教授,教科院);System.out.println(教室的信息为:+t1);}}2.改进例5-6的Circle类,提供若干求面积的方法,形态分别如下:Publicdoublearea()//求当前圆的面积Publicstaticdoublearea(doubler)//求半径为r的圆的面积Publicstaticdoublearea(Circlec)//求参数指定的圆的面积packagea.b.c;classCircle{doubler;publicCircle(doubler){this.r=r;}publicdoublearea(){returnMath.PI*r*r;}publicstaticdoublearea(Circlec){returnMath.PI*c.r*c.r;}}publicclassex6_3{publicstaticvoidmain(String[]args){Circlec1=newCircle(2);System.out.println(c1.area());System.out.println(Circle.area(2.5));System.out.println(Circle.area(c1));}}
本文标题:2.Java第六章程序练习
链接地址:https://www.777doc.com/doc-4911130 .html