`
baobeituping
  • 浏览: 1043242 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

c#(三)

    博客分类:
  • .NET
阅读更多

c#中的继承或实现都采用的是:

public class A:B

顺序是必须先继承后实现。

 

析构函数的申明方式:

class MyClass

{

   ~MyClass(){

//执行对象回收前的动作。

}

}

类的析构函数由带有~前缀的类名来声明,当进行垃圾回收时,就执行析构函数中的代码,释放资源。在调用这个析构函数后,还将隐式的调用基类的析构函数。,包括System.Object根类中的Finalize()调用。

 

JAVA中采用的是super和this来调用父类或本类的构造函数。

而C#中是用base,this,而且是写在构造函数的旁边。

class MyClass :MyBaseClass

{

   public MyClass():base(1)//调用父类的带一个int参数的构造函数

  {

   }

   public MyClass(int i)this()//调用本类的无参数的构造函数。

   {

    

   }

}

 

在C#程序中类似JAVA导入JAR包的步骤。

首先创建一个class library类型的项目。然后新建一个MyClass.cs的类。

然后新建一个工程项目,选择Project|Add Reference菜单项,单击Browse选项卡,然后选择class library工程目录下的xxx.dll文件。然后就可以在Solution Explorer窗口查找到。

然后通过:

using 项目的namespace

MyClass mc = new MyClass()

就可以引用到文件了。

同时也可以把XX.DLL文件复制到你的工程目录下。

 

STRUTS和CLASS的区别:

CLASS 是引用类型,在给对象赋值的时候,实际上是把带有一个指针的变量赋给了该指针所指向的对象。在实际代码中,指针是内存中的一个地址。在这种情况下,地址是内存中该对象所在的位置,在进行对象赋值的时候,实际上是复制了这个地址。

结构是值类型,包含的是结构本身,在结构赋值的时候,是将第一个结构中的所有信息复制到另一个结构中。

 

 

readonly修饰的字段的赋值地方:

1.构造函数中赋值。

2.在初始化的时候赋值。

public readonly int myInt=-1;

 

由static所修饰的属性或方法只能以类名。属性(方法名)来调用,不能通过该类的实例变量来调用。(跟JAVA不同)。

 

定义属性

当类中的一个属性为private的时候外部类是不可以访问的。

但可以通过指定get,set代码块来获得它的引用。

例如:

class MyClass

 private int intVal;

        public int Val
        {
            get
            {
                return intVal;
            }
            set
            {
                if (value >= 0 && value <= 10)
                {
                    intVal = value;

                //value是一个关键字,等于类型与属性相同的一个值。

可理解为public void setIntVal(int value)中的VALUE。表示传入的变量。
                }
                else
                {
                    throw (new ArgumentOutOfRangeException("Val",value,"Val must be 0 and 10"));
                }
            }
        }

 

如果外部类要访问该类的intVal.可以通过

MyClass mc = new MyClass();

mc.Val

来访问。

 

 

文件流的操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FileReadAndWrite
{
    class Program
    {
        static void Main(string[] args)
        {
            /*FileInfo aFile = new FileInfo(@"D:\Log.txt");
            if (aFile.Exists)
            {
                Console.WriteLine("文件已存在");
            }
            else
            {
                aFile.Create();
            }*/
            /*
            byte[] byData = null;
            char[] charData = null;

            try
            {
                FileStream aFile = new FileStream(@"d:\儿子名字.txt", FileMode.Open);
                byData = new byte[aFile.Length];
                charData = new char[aFile.Length];
                aFile.Seek(0, SeekOrigin.Begin);
               
                aFile.Read(byData, 0, (int)aFile.Length);
           
          
            Decoder d = Encoding.Default.GetDecoder();
            d.GetChars(byData,0,byData.Length,charData,0);

            Console.WriteLine(charData);

            //开始写到另外一个文件中
            FileStream outFile = new FileStream(@"d:\test.txt",FileMode.Create);
            outFile.Seek(0,SeekOrigin.Begin);
            outFile.Write(byData,0,byData.Length);

             }
             catch (Exception e)
            {
                //Console.WriteLine(e.Message);
                //Console.ReadKey();
            }
             * */
            try
            {
                FileStream outFile = new FileStream(@"d:\test.txt", FileMode.Create);
                StreamWriter output = new StreamWriter(outFile, System.Text.Encoding.Default);


                FileStream aFile = new FileStream(@"d:\儿子名字.txt", FileMode.Open);
                //该构造函数有两个参数,一个是读取文件的流,一个是文件是以什么编码来读取,该写法是告诉编译器按照当前系统默认字符编码来读取
                StreamReader reader = new StreamReader(aFile, System.Text.Encoding.Default);

                String line = reader.ReadLine();

                while (line != null)
                {
                    Console.WriteLine(line);
                    output.WriteLine(line);
                    line = reader.ReadLine();
                }
                output.Close();
                reader.Close();
                //如果流没有关闭,那么读取的内容将不完整。
            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
                Console.ReadKey();
            }
            finally
            {
               
            }
            Console.ReadKey();

          
        }
    }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics