【问题标题】:How to override base class constructor in C#如何在 C# 中重写基类构造函数
【发布时间】:2021-05-30 16:55:29
【问题描述】:

如何使用 Visual Studio 覆盖 C# 中的基类构造函数,我尝试调用它,但编译器报告错误 MyGLSurfaceView 不包含采用 0 个参数的构造函数,但我在构造函数实现中有一个参数请帮忙..

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)
        {
            //
        }
    }
}

【问题讨论】:

  • 为什么微软让人们在做android开发时将java转换为C#,而构造函数覆盖逻辑却有巨大的差异?
  • 我的意思是在java中完成的所有其他事情都可以用C#完成,除了构造函数覆盖?
  • 你试过public MyGLSurfaceView(Context context) : base (context) { ...}
  • 不,我没有让我尝试
  • 这能回答你的问题吗? Is it possible to override a constructor in C#?

标签: c# android constructor base


【解决方案1】:

您不能覆盖基类构造函数。

【讨论】:

    【解决方案2】:

    @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) { ...}
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-25
      • 2014-07-26
      • 2017-02-04
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 2012-09-28
      • 2020-09-22
      相关资源
      最近更新 更多