【发布时间】:2018-05-15 04:58:51
【问题描述】:
方法CanVotereturns true if Age >=18.
类的构造函数为所有属性分配默认值。
我将对象添加到哈希表中,键为人名。
我需要遍历哈希表对象以打印姓名以及该人是否可以投票。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;*/
namespace HashTable
{
class theClass
{
string name;
string dob;
int age;
public theClass(string name,int age, string dob)
{
this.name = name;
this.age = age;
this.dob=dob;
}
public static string canvote(int age)
{
if (age >= 18)
return "Can Vote";
else
return "Cnnot Vote";
}
}
public class Solution
{
public static void Main()
{
Hashtable h = new Hashtable();
theClass object1 = new theClass("John",16,"Chennai");
theClass object2 = new theClass("Smita",22, "Delhi");
theClass object3 = new theClass("Vincent",25, "Banglore");
theClass object4 = new theClass("Jothi", 10, "Banglore");
h.Add("John", object1);
h.Add("Smita", object2);
h.Add("Vincent", object3);
h.Add("Jothi", object4);
Console.WriteLine("df");
Console.WriteLine(h.canvote());
Console.ReadKey();
}
}
}
【问题讨论】:
-
初始化这些值或将它们创建为
Auto Propertiesstring name; string dob; int age;对于初学者... -
必须解释投反对票
标签: c# object hashmap arguments hashtable