刚看了一个程序,并写了一下,然后用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  这一个。哪都通用!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值