zoukankan      html  css  js  c++  java
  • C#的一个小例子

    今天去面试C#,直接机考,题目很简单,大致如下:

               建立一个person类,包含属性name和birthday。建立一个enum类,性别从里面取得。并把这个类加入到person类中 。person类中包含一个getAge函数。

               建立一个family类,类中包含一个list列表,包含函数getMembersCount()和重载函数getMembersCount(sex)。   

               建立一个console app名字为FamilyLib,创建一个family实体,并加入5个成员,2女3男。

               要求算出这个家庭的平均年龄和男性成员的平均年龄。

    实现代码如下:

    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace familyLib
     7 {
     8     class Program
     9     {
    10         static void Main(string[] args)
    11         {
    12             Family familyEx = new Family();
    13             Person perChild=new Person("Tom",DateTime.Parse("2009-02-03"),Sex.male);
    14             Person perMum = new Person("Lily", DateTime.Parse("1984-08-04"), Sex.female);
    15             Person perPapa = new Person("Bill", DateTime.Parse("1981-04-02"), Sex.male);
    16             Person perGrandma = new Person("momo", DateTime.Parse("1957-06-19"), Sex.female);
    17             Person perGrandPa = new Person("John", DateTime.Parse("1954-08-10"), Sex.male);
    18             familyEx.FamilyMembers.Add(perChild);
    19             familyEx.FamilyMembers.Add(perMum);
    20             familyEx.FamilyMembers.Add(perPapa);
    21             familyEx.FamilyMembers.Add(perGrandma);
    22             familyEx.FamilyMembers.Add(perGrandPa);
    23             float allAge  = 0;
    24             foreach (Person per in familyEx.FamilyMembers)
    25             {
    26                 allAge = allAge + per.getAge();
    27             }
    28             float allAveAge = allAge / familyEx.getMembersCount();
    29             float allMaleAge = 0;
    30             foreach (Person per in familyEx.FamilyMembers)
    31             {
    32                 if (per.Sex == Sex.male)
    33                 {
    34                     allMaleAge = allMaleAge + per.getAge();
    35                 }
    36             }
    37             float allMaleAveAge = allMaleAge/familyEx.getMembersCount(Sex.male);
    38        
    39             Console.WriteLine("所有成员的平均年龄为:{0}",allAveAge);
    40             Console.WriteLine("所有男性成员的平均年龄为:{0}", allMaleAveAge);
    41             Console.Read();
    42         }
    43     }
    44     enum Sex { male, female };
    45     class Person
    46     {
    47         private string name;
    48         private DateTime birthday;
    49         private Sex sex;
    50 
    51         
    52         
    53         public Person(string name, DateTime birthday,Sex sex)
    54         {
    55              this.name=name;
    56              this.birthday=birthday;
    57              this.sex = sex;
    58         }  
    59         public int getAge()
    60         {
    61             int age = DateTime.Now.Year - birthday.Year;
    62             return age;
    63         }
    64         internal Sex Sex
    65         {
    66             get { return sex; }
    67             set { sex = value; }
    68         }
    69     }
    70     class Family
    71     {
    72         private List<Person> familyMembers = new List<Person>();
    73 
    74         internal List<Person> FamilyMembers
    75         {
    76             get { return familyMembers; }
    77             set { familyMembers = value; }
    78         }   
    79         public int getMembersCount()
    80         {
    81             return familyMembers.Count;
    82         }
    83         public int getMembersCount(Sex sex)
    84         {
    85             int count = 0;
    86             foreach(Person per  in familyMembers)
    87             {
    88                 if (per.Sex==sex)
    89                 {
    90                     count = count + 1;
    91                 }
    92             } 
    93             return count;
    94         }
    95     }
    96 }

    这个题目中包含的内容其实挺多的:enum,list,函数重载等等。

    做个记录吧。

  • 相关阅读:
    beta冲刺3/7
    案例分析
    beta冲刺2/7
    beta冲刺1/7
    烟头的待办项
    Spring框架原理概述
    Spring源码环境搭建
    BeanFactory的启动流程
    Spring容器的原理
    Spring Framework 概述
  • 原文地址:https://www.cnblogs.com/huang1990/p/3065291.html
Copyright © 2011-2022 走看看