C#(二十一)之派生类中的构造函数 object类

简介: 今天看下派生类中的有参数和无参数的构造函数以及object类。

QQ图片20220426141845.jpg

今天看下派生类中的有参数和无参数的构造函数以及object类:


1:无参数构造函数:


/* C#主要的运行函数,就是main函数 */
        static void Main(string[] args)
        {
            // 五参数构造函数
            C obj = new C();
            /*
             * 输出:
             * 我是A的构造函数
             * 我是B的构造函数
             * 我是C的构造函数
             * 我是C的析构函数
             * 我是B的析构函数
             * 我是A的析构函数
             */
        }
/*定义A类*/
        class A {
            private int a;
            // 构造函数
            public A()
            {
                a = 1;
                Console.WriteLine("我是A的构造函数");
            }
            // 析构函数
            ~A(){Console.WriteLine("我是A的析构函数");}
        }
        /*定义B类*/
        class B:A
        {
            private int b;
            // 构造函数
            public B()
            {
                b = 1;
                Console.WriteLine("我是B的构造函数");
            }
            // 析构函数
            ~B() { Console.WriteLine("我是B的析构函数"); }
        }
        /*定义C类*/
        class C : B
        {
            private int c;
            // 构造函数
            public C()
            {
                c = 1;
                Console.WriteLine("我是C的构造函数");
            }
            // 析构函数
            ~C() { Console.WriteLine("我是C的析构函数"); }
        }


2:有参数构造函数


/* C#主要的运行函数,就是main函数 */
        static void Main(string[] args)
        {
            // 有参数构造函数
            F fff = new F(23,34);
            /*
            * 输出:
            * 我是D34
            * 我是F23
            */
        }
        /*定义D类*/
        class D
        {
            public int d;
            // 构造函数
            public D(int d1)
            {
                d = d1;
                Console.WriteLine("我是D{0}",d);
            }
        }
        /*定义F类*/
        class F : D
        {
            public int f;
            // 构造函数
            // 派生类的构造函数除了需要给自身的构造函数传值以外,还需要给上一级传值
            // 基类中若是构造函数需要多个参数,派生类构造函数中的base也要穿多个参数
            public F(int f1,int d1):base(d1)
            {
                f = f1;
                Console.WriteLine("我是F{0}",f);
            }
        }


3:万恶之源object类:


C#中的类都会直接或者间接的继承object类:如果定义类的时候没有指定基类,那么C#默认继承object类。


QQ图片20220426141848.png

以上为object类中的虚方法。可在使用的时候重写


static void Main(string[] args)
{
     int n = 123456;
     string s = n.ToString();  // string s "123456"
     Console.WriteLine(s);
    AA aa = new AA();
    aa.a = 100;
    string old = aa.a.ToString();  //这里调用的还是object中的方法
    string news = aa.ToString();  // 这里调用的是AA中的ToString方法
}
// 重写ToString类
class AA {
     public int a;
     public override string ToString()
     {
        return "我是" + a;
     }
}


这里只是简单看了一下object类中的一点小知识。



目录
相关文章
|
1月前
|
C#
C#学习相关系列之数据类型类的三大特性(二)
C#学习相关系列之数据类型类的三大特性(二)
|
1月前
|
C#
58.c#:directory类
58.c#:directory类
12 0
|
1月前
|
C#
57.c#:directorylnfo类
57.c#:directorylnfo类
13 0
|
1月前
|
监控 C#
55.c#:file类
55.c#:file类
16 1
|
1月前
|
算法 C#
54.c#:random类
54.c#:random类
14 1
|
1月前
|
C#
51.c#:string类的静态方法
51.c#:string类的静态方法
20 1
|
1月前
|
C#
27.c#关键字sealed修饰类
27.c#关键字sealed修饰类
12 0
|
29天前
|
C#
深入C#中的String类
深入C#中的String类
11 0
|
1月前
|
C#
C#学习系列相关之多线程(二)----Thread类介绍
C#学习系列相关之多线程(二)----Thread类介绍
|
1月前
|
C#
C#学习相关系列之数据类型类----嵌套类和嵌套方法(三)
C#学习相关系列之数据类型类----嵌套类和嵌套方法(三)