码迷,mamicode.com
首页 > 编程语言 > 详细

java之 ------ 类与对象

时间:2015-05-02 12:30:56      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

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 0001
	private 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);	
     }
     
   }
}






java之 ------ 类与对象

标签:

原文地址:http://blog.csdn.net/u011479875/article/details/45438657

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!