一个简单的Java关于类的小作业
题目:
1.定义抽象类Person,类中有属性:姓名、性别、出生日期,实例方法:计算年龄,抽象方法:play
2.定义学生类Student,继承Person,新增属性:学号;
|-定义中小学生类,继承Student,新增属性:年级、班级;方法:能够计算圆的面积
|-实现play
|-定义大学生类,继承Student,新增属性:专业、年级、班级,方法:能够计算椎体的体积
|-大学生类的学号:要求使用正则表达式验证学号是否符合规则->20开头,8位数字字符
|-实现play
3.定义员工类Employer,继承Person,新增属性:工号、月薪、部门、职位,方法:…
4.新建中小学生对象、大学生对象、员工,输出每个对象的字符串表示(属性值)、 测试各个方法。
package class_test;
import java.math.*;
public class CollegeStudent extends Student {
@Override
public String toString() {
return "bigStudent [zhuanye=" + zhuanye + ", nainji=" + nainji + ", banji=" + banji + ", getZhuanye()="
+ getZhuanye() + ", getNainji()=" + getNainji() + ", getBanji()=" + getBanji() + ", getNumber()="
+ getNumber() + ", getName()=" + getName() + ", getSex()=" + getSex() + ", getBirthday()="
+ getBirthday() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()="
+ super.toString() + "]";
}
public String getZhuanye() {
return zhuanye;
}
public void setZhuanye(String zhuanye) {
this.zhuanye = zhuanye;
}
public String getNainji() {
return nainji;
}
public void setNainji(String nainji) {
this.nainji = nainji;
}
public int getBanji() {
return banji;
}
public void setBanji(int banji) {
this.banji = banji;
}
private String zhuanye;
private String nainji;
private int banji;
public CollegeStudent(String zhuanye, String nainji, int i) {
super();
this.zhuanye = zhuanye;
this.nainji = nainji;
this.banji = i;
}
public CollegeStudent() {
}
int getyuanzhui(int r,int h) {
long area;
area = (long) ((r*r*(Math.PI))*h*(0.3));
return (int) area;
}
}
package class_test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Employer extends Person {
@Override
public String toString() {
return "Employer [gonghao=" + gonghao + ", yuexin=" + yuexin + ", bumen=" + bumen + ", zhiwei=" + zhiwei
+ ", days=" + days + ", getGonghao()=" + getGonghao() + ", getYuexin()=" + getYuexin() + ", getBumen()="
+ getBumen() + ", getZhiwei()=" + getZhiwei() + ", getName()=" + getName() + ", getSex()=" + getSex()
+ ", getBirthday()=" + getBirthday() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
+ ", toString()=" + super.toString() + "]";
}
public int getGonghao() {
return gonghao;
}
public void setGonghao(int gonghao) {
this.gonghao = gonghao;
}
public int getYuexin() {
return yuexin;
}
public void setYuexin(int yuexin) {
this.yuexin = yuexin;
}
public String getBumen() {
return bumen;
}
public void setBumen(String bumen) {
this.bumen = bumen;
}
public String getZhiwei() {
return zhiwei;
}
public void setZhiwei(String zhiwei) {
this.zhiwei = zhiwei;
}
private int gonghao;
private int yuexin;
private String bumen;
private String zhiwei;
private int days;
public Employer(String name, String sex, String birthday, int gonghao, int yuexin, String bumen, String zhiwei) {
this.gonghao = gonghao;
this.yuexin = yuexin;
this.bumen = bumen;
this.zhiwei = zhiwei;
}
public Employer() {
}
public void getAge(String birthday1) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss");
Date date = sdf.parse(birthday1);
long time = date.getTime();
System.out.println("出生时间距离时间原点"+time+"毫秒");
Date date1 = new Date();
System.out.println("当前时间为"+date1);
long time1 = date1.getTime();
long time2 = ((time1-time)/86400000)/365;
System.out.println("年龄为"+time2);
}
}
package class_test;
public abstract class Person {
@Override
public String toString() {
return "Person [name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", getName()=" + getName()
+ ", getSex()=" + getSex() + ", getBirthday()=" + getBirthday() + ", getClass()=" + getClass()
+ ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
private String name;
private String sex;
// birthday是一个yyyy-MM-dd HH:mm:ss 类型的时间;
private String birthday;
public Person(String name, String sex, String birthday) {
super();
this.name = name;
this.sex = sex;
this.birthday = birthday;
}
public Person() {
}
void getAge() {
}
}
package class_test;
import java.math.*;
public class Pupils extends Student {
@Override
public String toString() {
return "littleStudent [nianji=" + nianji + ", banji=" + banji + ", getNianji()=" + getNianji() + ", getBanji()="
+ getBanji() + ", getNumber()=" + getNumber() + ", getName()=" + getName() + ", getSex()=" + getSex()
+ ", getBirthday()=" + getBirthday() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
+ ", toString()=" + super.toString() + "]";
}
public String getNianji() {
return nianji;
}
public void setNianji(String nianji) {
this.nianji = nianji;
}
public String getBanji() {
return banji;
}
public void setBanji(String banji) {
this.banji = banji;
}
private String nianji;
private String banji;
public Pupils(String name, String sex, String birthday, String number, String nianji, String banji) {
super(name, sex, birthday, number);
this.nianji = nianji;
this.banji = banji;
}
public Pupils() {
}
int getCircle(int r) {
int area;
area = (int) (r*r*(Math.PI));
return area;
}
}
package class_test;
public class Student extends Person {
@Override
public String toString() {
return "Student [number=" + number + ", getNumber()=" + getNumber() + ", toString()=" + super.toString()
+ ", getName()=" + getName() + ", getSex()=" + getSex() + ", getBirthday()=" + getBirthday()
+ ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + "]";
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
private String number;
public Student(String name, String sex, String birthday, String number) {
super(name, sex, birthday);
this.number = number;
}
public Student() {
}
}
package class_test;
import java.text.ParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class main1 {
public static void main(String[] args) throws ParseException {
CollegeStudent bg = new CollegeStudent("网络工程","2018",1);
Pupils ls = new Pupils("张三", "男","2000-12-12,00:00:00", "20180001", "2018", "1");
Employer ep = new Employer("李四", "男","1970-12-12,00:00:00" , 20110001, 3000, "清洁部", "保安");
boolean a;
a = isNumLegal(ls.getNumber());
System.out.println(a);
System.out.println(bg.toString());
System.out.println(ls.toString());
System.out.println(ep.toString());
int circle = ls.getCircle(2);
System.out.println(circle);//算圆
int circle1 = bg.getyuanzhui(8,4);
System.out.println(circle1);//算圆柱
//算年龄
ep.getAge(ls.getBirthday());
}
private static String String(int number) {
// TODO Auto-generated method stub
return null;
}
private static boolean isNumLegal(String str){
String regExp = "^((20)\\d{6})$";
Pattern p = Pattern.compile(regExp);
Matcher m = p.matcher(str);
return m.matches();
}
}