C#关于类(2)

来源:互联网 发布:剑网3刘亦菲捏脸数据 编辑:程序博客网 时间:2024/05/23 01:33

函数的重载

与C++重载类似


构造函数的重载

与C++类似


运算符重载

重载运算符由关键字operator声明,必须定义为静态

public static <类型1> operator + (<类型1> <变量1> , <类型1> <变量2>){......}

//如上是重载“+”运算符,其中返回类型表示此函数要返回的类型(该类型必须与所在的类相同),其中变量个数灵活设计

如:

class Calculate 

{

private double a;

private double b;

public Calculate()

{......}

public static Calculate operator +(Calculatex, Calculate y) //此处参数和返回值必须与类相同

{

......

return <Calculate类型变量>;

}

}

在进行使用时:

Calculate num1 = new Calculate();

Calculate num2 = new Calculate();

Calculate result = num1 + num2;

double a1,a2;

double a3 = a1+a2; //此处a1+a2的值仍然是正常的加法运算

在重载运算符时要注意左右操作数,上面例子中左操作数num1就是x,右操作数num2,就是y


this关键字

this关键字相当于类的指针,在类中使用,指代本类,可以理解为"这个类中的......"或"这个对象的......"

class Cat

{

private int age;

public Cat(int age)

{

this.age = age;//此处this.age中的age表示本类中的age也就是private int age

//而等号右边的age就是传过来的参数age

}

}


数组在方法中的传递

class At

{

public int[] arr = new int[9];

public int[] Arr(int[] arr1)

{

arr = arr1;

return arr;

}

}

在main函数中:

At ced = new At();

int[] arr0 = new int[] {1,2,3,4,5,6,7,8,9};

ced.Arr(arr0); //此时就把arr0的内容传给了ced中的arr


数组在属性中的传递

class At

{

public int[] arr = new int[9];

public int[] Arr

{

set { arr = value; }//此时只有set没有get,所以是只写的属性,相反只有get没有set是只读的属性

}

}

在main函数中:

At ced = new At();

int[] arr0 new int[] {1,2,3,4,5,6,7,8,9};

ced.Arr = arr0;


索引器

属性可以理解为一个特殊的函数,而索引器可以理解为一个特殊的属性

索引器的格式:

class MyIndexer

{

private int[] arr = new int[5];

public int this[int index]//索引器

{

get 

{

if(index >= 0 && index < 5) return arr[index];

else return 0;

}

set

{

if(index >=0 && index < 5)

arr[index ] = value;

}

}

}

在主函数中:

MyIndexer myindexer = new MyIndexer();

myindexer[2] = 22;

myindexer[4] = 44;


继承

比如脊椎动物是一个类,其他动物是一个类,鱼类、哺乳动物等都具有脊椎动物的特性,但是他们独自还有自己的特性,但都继承了脊椎动物的特性

所以成鱼类、哺乳动物类继承了脊椎动物类,成脊椎动物类为基类,成鱼类、哺乳动物类为派生类

基类、派生类是相对而言的,如图中人类、虎类继承了哺乳动物类,此时哺乳动物类是基类,人类、虎类是派生类

啊

假如说定义了一个

class Vertebrate

{

  protected int weight;

  protected int temperature;

  ......

  public Vertebrate()

  {......}

  public void Run() {Console.WriteLine("跑");}

  public void Breathe() {Console.WriteLine("呼吸");}

}

再定义一个类继承Vertebrate,如下:

class Mammal : Vertebrate //Vertebrate是基类,Mammal是派生类

{

  private string color;

  ......

  public Mammal()

  {

    weight = 10;

    temperature = 36;

    color = "Blue";

  }

  public void Lactation()

  {Console.WriteLine("哺乳"); }

  public void BreatheMammal()

  {Console.WriteLine("用肺呼吸"); }

}

在使用的时候,可以用派生类定义一个对象:

Mannal cat;

这个cat继承了基类中所有的对象和函数,如可以使用:

cat.Run();

cat.Lactation();


保护对象protected

protected修饰符修饰的对象,在本类和派生类中可以直接使用,在外部不能直接使用


虚方法的重写

如上面的Vertebrate类,在其中的Breath()方法前virtual关键字,就成了虚方法:

public virtual void Breathe() {......}

在哺乳动物类中用override关键字定义相关虚方法:

public override void Breathe()

{Console.WriteLine("肺呼吸");}

可以在定义一个鱼类:

class Fish : Vertebrate

{

  public override void Breathe()

  {Console.WriteLine("鰓呼吸");}

}

在使用时:

Vertebrate animal = new Vertebrate();

Mammal mammal = new Mammal();

Fish fish = new Fish();

animal.Breathe(); //打印出呼吸

mammal.Breathe(); //打印出肺呼吸

fish.Breathe(); //打印出鳃呼吸


并不是所有方法都能重写,静态的方法不可以被重写

跟重载不同,重写在继承关系中才会发生,重写是根据所在类的不同而区分调用的






0 0