java之,c++之父

來源:互聯網
上載者:User

java之,c++之父
MyDate類

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; //this是final變數,不能被更改值     this(d.year, d.month, d.day);   }      public void set(int year, int month, int day){//演算法不完善     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 year){     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);   }      public String toString(){     return this.year+"年"+this.month+"月"+this.day+"日";   }      public static boolean isLeapYear( int year ){     //在靜態方法中,不能調用直接非靜態方法。如果調,必須通過(new)對象去調。     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 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) ); //深拷貝  }  public Person(){  this("無名", new 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對象");  }    public String toString(){  return name + "," + this.birthday.toString();  }   }

Studen 類
public class Student extends Person{private String number; //學號: 0101 2015 0001private String speciality;private static int count;//變數隱藏private static int max=0;  public Student(String name,MyDate birthday,String speciality){  super(name,birthday);//必須在第一句  this.speciality = speciality;  //處理學號  max++;  String str = "000"+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("無名",new MyDate(),"");  }    public Student(){  this("無名",new MyDate(),"無業");  }  public Student(Student s){  this(s.name, s.birthday, s.speciality);  }  //set , get      //方法的覆蓋(子類與父類存在完全相同的方法),子類方法的許可權不能低於父類  public String toString(){  return number+","+name+"," +birthday.toString()+","+speciality;     }    //方法重載  public void set(String speciality){  this.speciality = speciality;  }    public static void howMany(){//靜態方法中是不允許直接存取:this或super  System.out.println("Person對象的數量:"+Person.count);  System.out.println("Student對象的數量:"+count); //this.count  }    public void finalize(){  count--;    }     public static void main(String args[]){     Person p1 = new Person("李小明",new MyDate(1995,4,20) );     Student s1 = new Student(p1,"電腦");     Student s2 = new Student(s1);//編譯時間多態     s2.set("Java");     s2.howMany();          Person p2 = new Student(s2);//運行時多態,new誰調誰     //Student s3 = new Person();//錯     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);     }        }}






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.