MyDate class
public class mydate{private int year, month, day; private static int thisyear=2015; Public mydate (int year, int month, Int. day) {this.year=year; This.month=month; This.day = day; Public MyDate () {this (1970,1,1);//date new Date ()--long} public mydate (MyDate d) {//this=d;//th IS is a final variable and cannot be changed by the value this (D.year, D.month, D.day); } public void Set (int year, int month, Int. day) {//algorithm imperfect this.year = year; This.month = (month>=1 && month<=12)? Month:1; This.day = (day>=1 && day<=31)? Day:1; } public void Set (MyDate d) {this.set (d.year, D.month, D.day); } public void Setyear (int.) {this.year = year; } public void setmonth (int month) {this.month = month; } public void Setday (int day) {this.day = day; } public int GetYear () {return this.year; } public int GetMonth () {return this.month; } public int GetDay () {return this.day; } public static int getthisyear () {return thisyear=java.util.calendar.getinstance (). get (Java.util.Calendar.YEAR); The public String toString () {return this.year+ "year" +this.month+ "month" +this.day+ "Day"; public static Boolean isleapyear (int year) {//In a static method, a direct non-static method cannot be called. If it is, it must be adjusted by the (new) object. Boolean Boo=year%400==0 | | year%100!=0&&year%4==0; return boo; } public boolean isleapyear () {return this.isleapyear (this.year); } public static int daysofmonth (Int. year,int month) {switch (month) {case 1:case 3:case 5:case 7:case 8: Case 10:case 12:return 31; Case 4:case 6:case 9:case 11:return 30; Case 2:return isleapyear (year)? 29:28; Default:return 0; }} public int daysofmonth () {return daysofmonth (this.year,this.month); } public void Tomorrow () {this.day++; if (This.day > This.daysofmonth ()) {day = 1; month++; if (month>12) {this.month=1; this.year++; } }} public MyDate Yestoday () {mydate yes = new MyDate (this); yes.day--; if (yes.day<1) {yes.month--; if (yes.month==0) {yes.month=12; yes.year--; } Yes.day = Daysofmonth (yes.year,yes.month); } return yes; }}
Person class
Class person{protected String name;protected mydate birthday;protected static int count=0; Public person (String name, mydate birthday) { this.name = name; this.birthday= birthday; count++; } Public person (person p) {This (p.name, New MyDate (P.birthday));//Deep copy} public person () {This ("nameless", NE W mydate ()); } public void Set (String name) { this.name = name; } public void set (MyDate birthday) { this.birthday = birthday; } public int getage () { return (Mydate.getthisyear ()-birthday.getyear ()); } Public String GetName () { return this.name; } Public MyDate Getbirthday () { return this.birthday; } public int oldthen (person p) { return p.birthday.getyear ()-this.birthday.getYear (); } public static void Howmany () { System.out.println (count+ ' Person object '); } Public String toString () { return name + "," + this.birthday.toString (); } }
Studen class
public class Student extends person{private string number;//Learn No: 0101 0001private string speciality;private static in T count;//variable hides private static int max=0; Public Student (String name,mydate birthday,string speciality) {super (Name,birthday);//Must be in the first sentence this.speciality = speciality; Processing number max++; String str = "+max"; int n = str.length (); str = str.substring (n-4,n); [n-4,n-1] This.number = "0101" +mydate.getthisyear () +str; count++; } public Student (person p, String speciality) {This (P.getname (), P.getbirthday (), speciality); } public Student (String name, mydate birthday) {This ("nameless", New MyDate (), ""); } public Student () {This ("nameless", New MyDate (), "unemployed"); } public Student (Student s) {This (S.name, S.birthday, s.speciality); }//set, the Get//method overrides (the subclass exists exactly the same way as the parent class), the permissions of the subclass method cannot be lower than the parent class public String toString () {return number+ "," +name+ "," +birthday . toString () + "," +speciality; }//method overloads public void set (String speciality) {this.speciality = speciality; } PUBlic static void Howmany () {///static method is not allowed direct access: This or Super System.out.println ("Number of person objects:" +person.count); System.out.println ("Number of Student objects:" +count); This.count} public void Finalize () {count--; } public static void Main (String args[]) {person P1 = new person ("Ted", New MyDate (1995,4,20)); Student S1 = new Student (P1, "Computer"); Student s2 = new Student (s1);//compile-time polymorphic s2.set ("Java"); S2.howmany (); person P2 = new Student (s2);//Run-time polymorphic, new who tune who//student s3 = new Person ();//Wrong System.out.println (p2.tostring ()); System.out.println ("---------"); Person ps[] = {P1,S1,S2,P2}; for (int i=0; i<ps.length; i++) {System.out.println (ps[i]); } System.out.println ("---------"); Object objs[] = {P1,S1,S2,P2}; for (Object obj:objs) {System.out.println (obj); } }}
------classes and objects of Java