c#,类,析构函数,封装,继承,多态

用来描述一个对象的行为,对象是类的实例,我们通过对对象的实例化调用类中的成员变量,描述对象的状态,调用类中的方法用来描述对象的行为。

类的定义和封装
internal/public class Box{}//类的访问修饰符只有两种:internal/public
internal/public class Box{}//当我们把一些数据定义到类中,这个过程就称之为封装
internal/public class Box{
    public int leagth;
    public int width;
    public int height;
    public Box(int length,int width, int height){
        this.length = length;
        this.width = width;
        this.leagth = leagth;
    }//构造函数, 默认无参的构造函数
    public int GetVolume(){
        return a*b;
    }    
}//类不能直接使用,需要创建对象调用类里面的成员变量、方法;
Box b1 = new Box();//通过对象调用成员变量描述对象的状态;Box()//构造函数
b1.xx = xxx;
b2.xx = xxx;
b1GetVolume();//通过类中的方法描述对象的行为

特殊的成员函数:构造函数

public int GetVolume(int length,int width, int height){
     this.length = length;
        this.width = width;
        this.leagth = leagth;
}
//构造函数支持重载
析构函数
~Box(){}//在销毁对象的时候调用;

静态成员

static void 方法名(){}//静态成员属于类,用static修饰符修饰的方法为静态方法
public static int objectCount;//静态变量
Box.objectCount//只能通过类名访问,无法使用对象访问

显示对象数量:

public static int objectCount;   
objectCount++;
Console.WriteLine(Box.objectCount);
类的继承

在一个类中使用另外一个类的属性和方法,可以根据一个类去定义另一个类,能达到对代码重复利用的目的,使得程序维护起来更加的简单。

class Anim{  //基类
    public string name;
    public int age;
    public void Eat(){
        Console.WriteLine(name + "正在吃东西");
    }
}
class Dog:Anim     //继承的声明,Dog是Anim的派生类
{
  
}
class Program
{
    staic void Main(string[] args)
    {
        Dog d = new Dog();
        d.name = "狗狗";
        d.age = 1;
    }

通过构造函数查看对象初始化的顺序

class Anim{  //基类
    public string name;
    public int age;
    public Anim(){
        Console.WriteLine("Anim被创建")
    }
    public void Eat(){
        Console.WriteLine(name + "正在吃东西");
    }
}
class Dog:Anim 
{
  Console.WriteLine("Dog被创建")
}
class Program
{
    staic void Main(string[] args)
    {
        Dog d = new Dog();
        d.name = "狗狗";
        d.age = 1;
    }//创建一个对象先创建他的父类,在C#中一个子类只能有一个父类,不能同事继承多个类,但是我们可以通过接口的方式实现多重继承
类的多态

同一个行为具有不同的表现形式,例如同一个方法实现多个功能

静态多态

函数的重载:同一个函数,我们可以给他不同的参数,进而让它实现不同的功能。

​ 方法的重载:

public int Add(int a,int b){}//方法名相同,方法签名不同
public int Add(string a,int b){}

运算符的重载:

class Box 
{
    public int length;
    public int width;
    public int height;
    public static Box operator +(Box a,Box b)
    {
        Box box = new Box();
        box.width = a.width + b.width;
        box.width = a.length + b.length;
        box.width = a.height + b.height;
        return box;
    }
    public void Display()
    {
        Console.WriteLine("长" + this.length);
        Console.WriteLine("宽" + this.width);
        Console.WriteLine("高" + this.height);
    }
}
Box b1 = new Box();
b1.length = 10;
b1.width = 10;
b1.height = 10;
Box b2 = new Box();
b2.length = 20;
b2.width = 20;
b2.height = 20;
(b1 + b2).Display();
Console.WriteLine(b1 + b2)//若不使用方法的重载则c#会报错,运算符的重载是重新定义了运算符

可重载的运算符

一元运算符(可重载)+,-,~,++,--
    二元运算符(可重载) +,-,*,/,%
    逻辑运算符(不可重载)&&,||
    赋值运算符(不可重载)+=,-=,*=,/=,%=
    (不可重载)=,.,?,new,sizeof,typeof,->
动态多态

​ 通过抽象类或虚方法来实现不同的功能

抽象类:

不提供内容,指提供接口,当一个类继承它并实现它对应接口的时候它才会实现相应的功能

abstract class Shape
{
    abstract public float area();//只有一个名字,没有具体的实现
}//抽象类不能进行实例化
class Rect : Shape
{
    public int width;
    public int height;
    public override float area()
    {
        return width * height;
    }
}
    class Circle : Shape
    {
        return 3.14f * r * r;
    }
class DtDuoTai
{
    static void Main(string[] args)
    {
        Rect r = new Rect();
        r.width = 10;
        r.height = 20;
        Console.WriteLine(r.area)
        Circle circle = new Circle();
        circle.r = 3;
        Console.WriteLine(circle.area());
    }
}

虚方法:

class Shape
{
    public virtual float area()
    {
        return 0;
    };
class Rect : Shape
{
    public int width;
    public int height;
    public override float area()
    {
        return width * height;
    }
}
     public override Circle : Shape
    {
        return 3.14f * r * r;
    }
class DtDuoTai
{
    static void Main(string[] args)
    {
        Rect r = new Rect();
        r.width = 10;
        r.height = 20;
        Console.WriteLine(r.area)
        Circle circle = new Circle();
        circle.r = 3;
        Console.WriteLine(circle.area());
    }
}

区别:静态多态在编译的时候就已经有多重形态了,而动态多态则是在运行中才体现出它的多重形态。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值