您的位置:首页 > 编程语言 > C#

C#

2004-11-18 14:28 381 查看
//
/*本例的作用
 * 设计者:翁富国 QQ:171716420 
 * 1.分析自定义异常
 * 2。分析Serializable过程
 * 3。属性分析的例子
 * 4。索引的用法
 * 5。静态数据,方法,属性的定义及使用
 */

using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

// 用于分析自定义异常AAA
namespace wfgA002
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 ///
  
  public class MyException:System.Exception
  {
   public MyException(int a):base(a+"年龄不可能超出0--100")
   {

          
   }
   public void show()
   {
    System.Console.WriteLine("Message:"+base.Message);
    System.Console.WriteLine("ToString:"+base.ToString());
    System.Console.WriteLine("Target:"+base.TargetSite);
    System.Console.WriteLine("soure:"+base.Source);
    System.Console.WriteLine("Stact:"+base.StackTrace);

   }
  
  }
 
 // 普通人类
 [System.Serializable]
 public class Person
 {
  // 数据表示学号
   private int iNo;
  // 数据表示姓名
   private string strName;
    // 数据表示年龄
  private int iAge;
   // 构造函数
  public Person(int no,string name,int age)
  {
   iNumber++;

   iNo=no;
   strName=name;
   iAge=age;
  }
  // 属性表示学号
  public int No
  {
   get{return iNo;}
   
  }
  // 属性表示姓名
  public string Name
  {
   get{return Name;}
   
  }
  // 属性表示年龄
  public int Age
  {
   get{ return iAge;}
   set
   {
    if(value>0 && value<100)
    { // 写入的年龄>0 且<100
     iAge=value;

    }
    else
    {
     // 写入的年龄<0且>100则发生异常
     
                   iAge=-1;

     throw new MyException(value);

    }
   }// end iAge-set
  }
  // --------------------从object 类中继承并重载-----------------
  // 对象的toString 方法
  public override string ToString()
  {
   return "No:"+iNo+"/nName:"+strName+"/nAge:"+iAge;

  }
    // 比两个类是不是相等
  public override bool Equals(object obj)
  {
   return base.Equals (obj);
  }
  public override int GetHashCode()
  {
   return base.GetHashCode ();
  }
  // -----------------------静态区----------------
  // 总人数
   private static int iNumber;
  // 静态构造方法
  static Person()
  {
        iNumber=0;

  }
  //静态属性返回总人数,(只读)
  public static int Number
  {
   get{return iNumber;}
  }
  // 静态方法:显示总人数。
  public  static void ShowShare()
     {
      System.Console.WriteLine("现在有人数总共为:{0}",iNumber);

     }

 }
 [System.Serializable]
 public class Student:Person
 {
  private double []dblScroce;
  // 内部处理方法。
  /// <summary>
  /// 方法 private void Prcess()
  /// 功能:计算平均值和总分
  /// </summary>
  private void Prcess()
  {
   dblScroce[dblScroce.Length-2]=0.0d;

   for(int i=0;i<dblScroce.Length-2;i++)
   {
    dblScroce[dblScroce.Length-2]+=dblScroce[i];

   }
   dblScroce[dblScroce.Length-1]=dblScroce[dblScroce.Length-2]/(dblScroce.Length-2) ;

  }
  public double this[int index]
  {
   set
   {
    System.Console.WriteLine("value1:{0} value2:{1}",index,dblScroce.Length -2);

    if(index<0 || index>=dblScroce.Length-2)
    {
     System.Console.Out.WriteLine("sss");

     throw new IndexOutOfRangeException("超出了。不行了。小子");

    }
    else
    {
     dblScroce[index]=value;
     this.Prcess ();

    }

   }
   get
   {
    if(index<0 || index>=dblScroce.Length)
    {
     throw new IndexOutOfRangeException("wf 超出了。不行了,小子");

    }
              return dblScroce[index];
   }
  }
  public Student(int no,string name,int age,double []scroce):base(no,name,age)
  {
   dblScroce=new double[scroce.Length+2];
    // 计算总分和平均成绩
    for(int i=0;i<scroce.Length;i++)
    {
     dblScroce[i]=scroce[i];
       dblScroce[scroce.Length]+=dblScroce[i];

    
    }
    dblScroce[scroce.Length +1]=dblScroce[scroce.Length]/scroce.Length;

   

  }
  

  public double []Scroce
  {
   
   get
   {
    //重新计算平均成绩
    this.Prcess();

            return dblScroce;

   }//end get_Scroce

  }
  public override string ToString()
  { 
   string temp="/n";
   for(int i=0;i<dblScroce.Length;i++)
    temp+="分数为"+i.ToString()+":"+dblScroce[i]+"/n";

   return base.ToString()+temp;

  }

 }
 class Class1
 {
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  public static void Serializable(object obj)
  {
   if(null==obj)
   {
    throw new System.NullReferenceException("obj 没有实例",new System.Exception("翁富国sss"));

   }
   System.Runtime.Serialization.IFormatter  foramtter =new BinaryFormatter();
   System.IO.Stream stream =new FileStream("D://JFC//a.bin",FileMode.Create,FileAccess.Write,FileShare.None);
   foramtter.Serialize(stream,obj);
   stream.Close();
  }
 
  public static void DeSerializable(out object obj)
  {
   System.Runtime.Serialization.IFormatter formatter=new BinaryFormatter();
   System.IO.Stream stream=new FileStream("D://JFC//a.bin",FileMode.Open ,FileAccess.Read,FileShare.Read);
   obj=formatter.Deserialize(stream);
   if(null==obj)
   {
   throw new System.NullReferenceException("这个返顺列失败");
   }
  }
  public static void Show(object obj)
  {
   // 若obj是类Student 的一个实例强制类型转化
  Student obj1=obj is Student ? (Student)obj:null;
      System.Console.WriteLine(obj1.ToString());
   System.Console.WriteLine("---------------score---------------");
   ((Student)obj).Scroce[0]=123;
   
  

   for(int i=0;i<((Student)obj).Scroce.Length;i++)
   System.Console.WriteLine("分数:"+((Student)obj).Scroce[i].ToString());
   System.Console.WriteLine("---------
9341
------this[index]-------------------");
   try
   {
    //((Student)obj)[4]=88;
      ((Student)obj)[1]=70;

   }
   catch(System.Exception ex)
   {
      System.Console.WriteLine(ex);

   }
    for(int i=0;i<((Student)obj).Scroce.Length;i++)

    System.Console.WriteLine("分数:"+((Student)obj).Scroce[i].ToString());

  }
  public static Person GetStuent()
  {
   return new Person(1,"ss",2);

   
  }
  [STAThread]
  static void Main(string[] args)
  {
   //
   // TODO: 在此处添加代码以启动应用程序
   //
   
   
   object obj,obj1;
   double []scroce={60,78,90};
   obj=new Student(2005,"翁富国",200,scroce);

       
       

       
   obj1=null;
   try
   {//((Person)obj).Age =200;
    Serializable(obj);
    DeSerializable(out obj1);
    Show(obj1);
    
    Student.ShowShare();

   }
   
   catch(System.IndexOutOfRangeException ex)
   {
    System.Console.WriteLine(ex);

   }
   catch(MyException ex)
   {
    System.Console.WriteLine(ex);
    ex.show();

   }
   
   

  }
 }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息