基类初始化 [英] Base Class Initialization

查看:99
本文介绍了基类初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Text;

namespace Example
{
    class Program
    {
        private int x, y;
        public Program(int a, int b)
        {
            x = a;
            y = b;
        }
        void stex(int a)
        {
            x = a;
        }
        void sety(int b)
        {
            y = b;
        }
        int getx()
        {
            return x;
        }
        int gety()
        {
            return y;
        }

        ~Program()
        {
            x = y = 0;
        }
    }
    class mina:Program
    {
        public mina(int x1, int y1, int x2, int y2): base(10, 20)
        {
        }



        static void Main()
        {
            mina m = new mina();
            Console.ReadLine();
        }
    }
}




请帮我找出错误
错误为方法"mina"没有重载,接受参数"0""
请更正代码




Please Help me Find out the error
the error is "No overload for method ''mina'' takes ''0'' argument"
please Correct the code

推荐答案

您好,您正在为Mina调用空构造函数.

但是没有人.

因此,您要么需要创建一个空的构造函数.记住将默认值传递给Parogram.
或者您需要调用现有的构造函数.

这些是基于您的原始要求.
Hi, you are calling empty constructor for mina.

But there is no any one.

So either you need to create empty constructor. Remeber to pass default values to Parogram.
Or you need to call existing constructor.

Those are based on your original requirement.




您仅使用参数创建了一个构造函数.初始化对象时,它正在使用no参数搜索构造函数

Hi,

You have created only one constructor with the parameters. While initializing the object it is searching for the constructor with the no parameter

public mina(): base(10, 20)
       {
       }



将上面的代码块放入您的类文件中,即可使用



put the above block of code in your class file it will work




经验法则是在类中声明自己的构造函数时,需要编写自己的默认构造函数(如果需要)"

在这里,您为类声明了参数化构造函数,因此,如果要使用默认构造函数,则必须编写自己的构造函数.
谢谢

Milind
Hi,

The rule of thumb is "when you declare your own constructor in the class, you need write your own default contstructor (if you need it)"

Here you declared a parameterized constructor for your class, hence if you want to use default constructor, you will have to write your own.
Thanks

Milind


这篇关于基类初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆