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

53 篇文章 1 订阅
22 篇文章 0 订阅

今天看下派生类中的有参数和无参数的构造函数以及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类。

在这里插入图片描述

以上为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类中的一点小知识。

测试使用全部代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using gc; //使用自己定义类的命名空间
namespace gc
{
    class Program
    {
        /* C#主要的运行函数,就是main函数 */
        static void Main(string[] args)
        {
            // 五参数构造函数
            C obj = new C();
            /*
             * 输出:
             * 我是A的构造函数
             * 我是B的构造函数
             * 我是C的构造函数
             * 我是C的析构函数
             * 我是B的析构函数
             * 我是A的析构函数
             */
            // 有参数构造函数
            F fff = new F(23,34);
            /*
            * 输出:
            * 我是D34
            * 我是F23
            */
            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方法
        }
        class AA {
            public int a;
            public override string ToString()
            {
                return "我是" + 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的析构函数"); }
        }
        /*定义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);
            }
            
        }
    }
}

有好的建议,请在下方输入你的评论。

欢迎访问个人博客
https://guanchao.site

欢迎访问小程序:

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值