黑马程序员技术交流社区
标题:
求反射和泛型的定义和具体用法 最好有例子
[打印本页]
作者:
转达小朋友
时间:
2013-6-8 19:30
标题:
求反射和泛型的定义和具体用法 最好有例子
老是分不清 求高人点化
作者:
无__畏
时间:
2013-6-8 19:57
http://blog.csdn.net/ghfsusan/article/details/3702314
作者:
胡章诚
时间:
2013-6-8 22:47
大哥诶,真问题问得有点太大了吧,这两个,估计任何一个都可以上一节课了,呵呵呵
作者:
转达小朋友
时间:
2013-6-8 23:18
额 只是要 写两个 简单的实例 就可以了
作者:
曾大鹏
时间:
2013-6-8 23:22
反射
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
namespace 练习1
{
class Program
{
static void Main(string[] args)
{
Person p = new Person("zdp", 19, "男");
Console.WriteLine("{0},{1},{2}", p.Age, p.Name, p.Sex);
//p.sayhi();
Student stu = new Student();
stu.sayhi();
Type type = typeof(Person);
#region 获取类中的构造函数
Console.WriteLine("获取类中的构造函数=============");
ConstructorInfo[] ci = type.GetConstructors();//获取类中所有构造函数
foreach (ConstructorInfo item in ci)//遍历每一个构造函数
{
ParameterInfo[] ps = item.GetParameters();//取出每个构造函数的所有参数
foreach (ParameterInfo pi in ps)//输出每个构造函数的参数
{
Console.WriteLine(pi.ParameterType.ToString() + " " + pi.Name + " ");
}
Console.ReadKey();
}
#endregion
#region 查看类中的属性:
Console.WriteLine("查看类中的属性=============");
PropertyInfo[] pis = type.GetProperties();
foreach (PropertyInfo pi in pis)
{
Console.WriteLine(pi.Name);
}
#endregion
#region 查看类中的public方法
Console.WriteLine("查看类中的public方法=============");
MethodInfo[] mis = type.GetMethods();
foreach (MethodInfo mi in mis)
{
Console.WriteLine(mi.ReturnType + " " + mi.Name);
}
#endregion
#region 查看类中的public字段
Console.WriteLine("查看类中的public字段 =============");
FieldInfo[] fis = type.GetFields();
foreach (FieldInfo fi in fis)
{
Console.WriteLine(fi.Name);
}
#endregion
#region 用反射生成对象,并调用属性、方法和字段进行操作
Console.WriteLine("用反射生成对象,并调用属性、方法和字段进行操作 =============");
//动态生成一个实例
object obj = Activator.CreateInstance(type);
FieldInfo fi2 = type.GetField("id");
fi2.SetValue(obj, 2);
//取得Age属性
PropertyInfo fi1 = type.GetProperty("Age");
//给Age属性赋值
fi1.SetValue(obj, 18, null);
//取得Name属性
PropertyInfo pi1 = type.GetProperty("Name");
//给Name属性赋值
pi1.SetValue(obj, "grayworm", null);
PropertyInfo pi2 = type.GetProperty("Sex");
pi2.SetValue(obj, "男", null);
//取得sayhi方法
MethodInfo m = type.GetMethod("sayhi", BindingFlags.NonPublic | BindingFlags.Instance);
//调用sayhi方法
m.Invoke(obj, null);
//输出Name,Sex
Person pp = (Person)obj;
Console.WriteLine("姓名={0},性别={1},年龄={2},id={3}",pp.Name,pp.Sex,pp.Age,pp.id);
#endregion
}
}
class Person
{
private string name;//字段
public string Name//属性
{
get;
set;
}
private int age;
public int Age
{
get;
set;
}
private string sex;
public string Sex
{
get;
set;
}
public Person()
{
Console.WriteLine("dgdfg");
}
public Person(string name, int age, string sex)
{
this.Name = name;
this.Age = age;
this.Sex = sex;
}
private void sayhi()
{
Console.WriteLine("我是zdp");
}
public int id;
public int ID
{
get;
set;
}
}
class Student : Person
{
private double score;
public double Score
{
get { return score; }
set { score = value; }
}
public Student(double score, string name, int age, string sex)
: base(name, age, sex)
{
this.score = score;
Console.WriteLine("zilei");
}
public void sayhi()
{
Console.WriteLine("我是学生第三页");
}
public Student()
{
Console.WriteLine("我是学生");
}
}
}
复制代码
泛型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace List泛型
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[] { 3, 4, 5, 6 };
int[] b = { 3, 5, 67 };
int[] c ;
c=new int[]{234,5,5676};
Person p1 = new Person("zdp", 21);
Person p2 = new Person("zdp2", 22);
Person p3 = new Person("zdp3", 23);
List<Person> list = new List<Person>();
list.Add(p1);
list.Add(p2);
list.Add(p3);
///List<Person> list = new List<Person>() { new Person("zdp", 21), new Person("zdp2", 22), new Person("zdp3", 23) };
List<int> list1 = new List<int>() { 4, 6, 7 };
List<int> list2 = new List<int>() { 4,3, 1, 2 };
List<int> list3 = new List<int>();
list3.AddRange(list1);
//创建一个List集合
List<int>[] listArray = { list1, list2, list3 };
Console.WriteLine("List数组"+listArray[0][0]);
for (int i = 0; i < list2.Count; i++)
{
if (!list3.Contains(list2[i]))
{
list3.Add(list2[i]);
}
}
Console.WriteLine("{0}:{1}",list[0].Name,list[0].Age);
Console.WriteLine(list3[5].ToString());
string[] array=new string[]{"1","3","0"};
//ArrayList list4 = new ArrayList();
ArrayList list4 = new ArrayList(array);
//list4.CopyTo(array);
ArrayList list5 = ArrayList.Adapter(array);
//ArrayList list6 = new ArrayList();
//list6.CopyTo(array);
for (int i = 0; i < list5.Count; i++)
{
Console.WriteLine(list5[i]);
}
}
}
class Person
{
private string _name; //姓名
private int _age; //年龄
//创建Person对象
public Person(string Name, int Age)
{
this._name = Name;
this._age = Age;
}
//姓名
public string Name
{
get { return _name; }
set { _name = value; }
}
//年龄
public int Age
{
get { return _age; }
set { _age = value; }
}
}
}
复制代码
以前写的 你可以参考一下
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2