简体   繁体   English

如何覆盖 C# 中的基本 class 构造函数

[英]How to override base class constructor in C#

How do I override the base class constructor in C# with Visual Studio, i have tried calling this but compiler reports an error that MyGLSurfaceView does not contain a constructor that takes 0 arguments and yet I have an argument in the constructor implementation please help..如何使用 Visual Studio 覆盖 C# 中的基本 class 构造函数,我尝试调用它,但编译器报告错误MyGLSurfaceView不包含采用 0 arguments 的构造函数,但在构造函数实现中有一个参数。

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

using Android.App;
using Android.Content;
using Android.Opengl;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace Painting
{
    class MyGLSurfaceView : GLSurfaceView
    {
        Context mycontext;
        private readonly MyGlRenderer render;
        
        // Constructor with one argument
        // Compiler reports an error saying this constructor does not take 0 arguments
        public MyGLSurfaceView(Context context)
        {
            //
        }
    }
}

You can't override the base class constructors.您不能覆盖基本 class 构造函数。

This worked for me after @The General commented an answer Its because certain classes in Android are abstract and cannot be instantiated directly like Android.Os.CountDownTimer, so when it comes to inherting from them, the constructor becomes a problem but this solved it for me for now...在@The General 评论了一个答案之后,这对我有用,因为 Android 中的某些类是抽象的,不能像 Android.Os.CountDownTimer 那样直接实例化,所以当从它们继承时,构造函数成为一个问题,但这解决了它我暂时...

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

using Android.App;
using Android.Content;
using Android.Opengl;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace Painting
{
    class MyGLSurfaceView : GLSurfaceView
    {
        Context mycontext;
        private readonly MyGlRenderer render;
        
        // Constructor with one argument
        // Compiler reports an error saying this constructor does not take 0 arguments
       public MyGLSurfaceView(Context context) : base (context) { ...}
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM