using System;
using System.Collections.Generic;
using System.Text; namespace WindowsApplication
{
class MyClass
{
public MyClass(string name)
{
this.name = name;
this.students = new Student[3];
students[0] = new Student("Jack", Genders.Male, 62, "上网");
students[1] = new Student("Rose", Genders.Female, 59, "做饭");
students[2] = new Student("Eric", Genders.Male, 33, "打游戏");
}
private string name; public string Name
{
get { return name; }
set { name = value; }
}
public Student[] students; public Student this[string stringname]
{
get
{
int i;
bool found = false;
for (i = 0; i < students.Length; i++)
{
if (students[i].Name == stringname)
{
found = true;
break;
}
}
if (found)
{
return students[i];
}
else
{
return null;
}
}
} }
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms; namespace WindowsApplication
{
public enum Genders
{
Male, Female
}
class Student
{
public Student(string name, Genders gender, int age, string hobby)
{
this.name = name;
this.gender = gender;
this.age = age;
this.hobby = hobby;
this.popularity = 100;
} private string name; public string Name
{
get { return name; }
set { name = value; }
}
private Genders gender; public Genders Gender
{
get { return gender; }
set { gender = value; }
} private int age; public int Age
{
get { return age; }
set { age = value; }
}
private string hobby; public string Hobby
{
get { return hobby; }
set { hobby = value; }
}
private int popularity; public int Popularity
{
get { return popularity; }
set { popularity = value; }
} public void SayHi()
{
string MessageText = string.Format("大家好,我是{0}同学,今年{1}岁了,我喜欢{2}。我的人气值高达{3}", Name, Age.ToString(), Hobby, Popularity);
MessageBox.Show(MessageText, "提示信息", MessageBoxButtons.OK);
} }
}
using System;
using System.Collections.Generic;
using System.Windows.Forms; namespace WindowsApplication
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread] static void Main()
{
MyClass myClass = new MyClass("T01");
myClass.students["Eric"].SayHi();
} C# 编译提示错误 无法将类型“string”隐式转换为“int” 求教各位高手!!
using System.Collections.Generic;
using System.Text; namespace WindowsApplication
{
class MyClass
{
public MyClass(string name)
{
this.name = name;
this.students = new Student[3];
students[0] = new Student("Jack", Genders.Male, 62, "上网");
students[1] = new Student("Rose", Genders.Female, 59, "做饭");
students[2] = new Student("Eric", Genders.Male, 33, "打游戏");
}
private string name; public string Name
{
get { return name; }
set { name = value; }
}
public Student[] students; public Student this[string stringname]
{
get
{
int i;
bool found = false;
for (i = 0; i < students.Length; i++)
{
if (students[i].Name == stringname)
{
found = true;
break;
}
}
if (found)
{
return students[i];
}
else
{
return null;
}
}
} }
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms; namespace WindowsApplication
{
public enum Genders
{
Male, Female
}
class Student
{
public Student(string name, Genders gender, int age, string hobby)
{
this.name = name;
this.gender = gender;
this.age = age;
this.hobby = hobby;
this.popularity = 100;
} private string name; public string Name
{
get { return name; }
set { name = value; }
}
private Genders gender; public Genders Gender
{
get { return gender; }
set { gender = value; }
} private int age; public int Age
{
get { return age; }
set { age = value; }
}
private string hobby; public string Hobby
{
get { return hobby; }
set { hobby = value; }
}
private int popularity; public int Popularity
{
get { return popularity; }
set { popularity = value; }
} public void SayHi()
{
string MessageText = string.Format("大家好,我是{0}同学,今年{1}岁了,我喜欢{2}。我的人气值高达{3}", Name, Age.ToString(), Hobby, Popularity);
MessageBox.Show(MessageText, "提示信息", MessageBoxButtons.OK);
} }
}
using System;
using System.Collections.Generic;
using System.Windows.Forms; namespace WindowsApplication
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread] static void Main()
{
MyClass myClass = new MyClass("T01");
myClass.students["Eric"].SayHi();
} C# 编译提示错误 无法将类型“string”隐式转换为“int” 求教各位高手!!
改成myClass.Student["Eric"].SayHi();