C# 的源码是这样的:
using System;
namespace Date
{
class Date
{
private int month;
private int day;
private int year;
public Date(int themonth,int theday,int theyear)
{
if (themonth > 0 && themonth <= 12)
month = themonth;
else
{
month = 1;
Console.Write("Month {0} invaild. Set to month 1", themonth);
}
year = theyear;
day = CheckDay (theday);
}
private int CheckDay(int testday)
{
int[] days = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if (testday > 0 && testday <= days[month])
{
return testday;
}
if (month == 2 && month == 29 && (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)))
return testday;
Console.Write("Day {0} invaild. Set to day 1.", testday);
return 1;
}
public string ToDateString()
{
return month + "/" + day + "/" + year;
}
}
}
using System;
namespace Date
{
class Employee
{
private string firstname;
private string lastname;
private Date birthday;
private Date hireday;
public Employee(string first,string last,int birthMonth,
int birthDay,int birthYear,int hireMonth,int hireDay,
int hireYear)
{
firstname = first;
lastname = last;
birthday = new Date(birthMonth, birthDay, birthYear);
hireday = new Date(hireMonth, hireDay, hireYear);
}
public string ToEmployeeString()
{
return lastname + " " + firstname + " /n" +
"Hired : " + hireday.ToDateString() +
"/nBirthday : " + birthday.ToDateString();
}
}
}
using System;
namespace Date
{
class CompositionTest
{
static void Main(string[] args)
{
Employee e = new Employee("Bob", "chen", 12, 11, 1990, 11, 21, 2010);
string output = e.ToEmployeeString();
Console.Write(output);
Console.ReadKey();
}
}
}
Java 的源码是这样的:
import java.applet.*;
public class Date {
private int month;
private int day;
private int year;
public Date(int themonth,int theday,int theyear)
{
if(themonth > 0 && themonth <= 12)
month = themonth;
year = theyear;
day = CheckDay(theday);
}
private int CheckDay(int testDay)
{
int[] DaysofMonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if(testDay > 0 && testDay <= DaysofMonth[month])
return testDay;
if(month==2 && testDay==29 && ((year%400==0)||(year%4==0 && year%100!=0)))
return testDay;
System.out.println("Day" + testDay + "invaild . Set to day 1.");
return 1;
}
public String ToDateString()
{
return month + "/" + day + "/" + year;
}
}
import java.applet.*;
public class Employee {
private String firstname;
private String lastname;
private Date birthDate;
private Date hireDate;
public Employee(String first,String last,int BMonth,int BDay,int BYear,
int HMonth,int HDay,int HYear)
{
firstname = first;
lastname = last;
birthDate = new Date(BMonth,BDay,BYear);
hireDate = new Date(HMonth,HDay,HYear);
}
public String ToEmployeeString()
{
return lastname + " " + firstname + "/n" +
"Hired : " + hireDate.ToDateString() +
"/nBirthDay : " + birthDate.ToDateString();
}
}
import java.applet.*;
public class CompositionTest {
public static void main(String[] args) {
Employee e = new Employee("Bob","Chen",12,21,1899,11,5,2007);
System.out.print(e.ToEmployeeString());
}
}
最后我发现,其实两者没啥大的区别。不过中间在写Java 的时候还是遇到了麻烦、
在C# 中,String 和 string 这两个单词,是不一样的。表达不同的意思, 要分开用。
但是在Java 中,就只有 String 这一个。哪都通用!!