zoukankan      html  css  js  c++  java
  • 代码清单4-4 Person类的部分代码,其中包含了年龄的计算

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Person turing = new Person("Alan Turing", new DateTime(1912, 6, 3), new DateTime(1954, 6, 7));
                Person knuth = new Person("Donald Knuth", new DateTime(1938, 1, 10), null);
                Console.ReadKey();
            }
    
            class Person
            {
                DateTime birth;
                DateTime? death;
                string name;
                public TimeSpan Age
                {
                    get
                    {
                        if (death == null)
                        {
                            return DateTime.Now - birth;
                        }
                        else
                        {
                            return death.Value - birth;
                        }
                    }
                }
    
                public Person(string name,DateTime birth, DateTime? death)
                {
                    this.birth = birth;
                    this.death = death;
                    this.name = name;
                }
            }
        }
    }
  • 相关阅读:
    php Windows系统 wamp集成环境下redis的使用
    IO流文件拷贝
    IO流框架
    Map集合
    泛型
    Deque(队列)
    List接口
    Iterator接口(迭代器)
    Java中的异常详解
    Java中的正则表达式
  • 原文地址:https://www.cnblogs.com/liuslayer/p/6963947.html
Copyright © 2011-2022 走看看