剛看了一個程序,並寫了一下,然後用Java 再寫了一下

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  這一個。哪都通用!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章