为什么不抽象成员自动标记为公开? [英] why don't abstract members be marked as public automatically?

查看:58
本文介绍了为什么不抽象成员自动标记为公开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  abstract   class  mybase 
{
public int a,b,result;

public int Multi()
{
return 0 ;
}
}

class 派生:mybase
{
static void Main()
{
派生de = new derived();

Console.WriteLine( 结果3 * 5 = {0},de.Multi());
Console.ReadLine();
}

public int Multi()
{
a = 3 ;
b = 5 ;
return (result = a * b);
}
}





如果换行:

  public   int  a,b,result; 





to

  int  a,b,结果; 





它不起作用。



这是我的问题:

为什么变量a,b,结果不被标记为公共抽象成员?

解决方案

不仅严重限制功能是愚蠢的,而且让我们认为访问什么修饰符最有可能与虚拟成员,至少有一些合理的代码设计?当然,受保护。但你的建议是什么, public ?令人惊讶的是,它是最不可能的,也是迄今为止。此外,如果有可能,它可能是一个非常强烈的迹象表明可能滥用技术,在这种情况下,滥用非常糟糕。所以,从这个意义上说,你的想法似乎与合理的正好相反。



我必须解释原因吗?这是非常明显的,但从一开始就需要学习OOP。至于访问修饰符,这个主题非常简单:

https:// msdn.microsoft.com/en-us/library/ms173121.aspx [ ^ ],

https: //msdn.microsoft.com/en-us/library/wxh6fsc7.aspx [ ^ ]。



-SA


< blockquote>还有另一个访问修饰符,在这种情况下是适当的: protected



受保护成员可以访问声明类及其子类,但不能从外部访问。



所以这将起作用:

 protected int a,b,result; 







编辑:



如果未明确指定访问修饰符,则private是默认值(对于类成员)。所以:

 int a,b,result; 

相当于

 private int a,b,result; 

和私有成员子类无法访问。



我的建议:始终明确指定所有访问修饰符,包括成员和类。


abstract class mybase
{
    public int a, b, result;

    public int Multi()
    {
        return 0;
    }
}

class derived : mybase
{
    static void Main()
    {
        derived de = new derived();

        Console.WriteLine("Result of 3 * 5 = {0}", de.Multi());
        Console.ReadLine();
    }

    public int Multi()
    {
        a = 3;
        b = 5;
        return (result = a * b);
    }
}



If i change the line:

public int a, b, result;



to

int a, b, result;



it will not work.

And here is my question:
Why don't variables a, b, result be marked as "public abstract members"?

解决方案

Not only it would be silly to limit the functionality that badly, but let's aloso think access what modifier would be most likely with virtual members, with any at least a bit reasonable code design? Of course, protected. But what would be about your suggestion, public? Amazingly, it is the least likely, also by far. Moreover, if it could be possible, it could be a pretty strong indication of likely abuse of technology, and in this, cases, quite bad abuse. So, your idea appeared to be, in this sense, "directly opposite" to the reasonable.

Do I have to explain why? It is very obvious, but takes learning OOP, from the very beginning. As to the access modifiers, this topic is pretty trivial:
https://msdn.microsoft.com/en-us/library/ms173121.aspx[^],
https://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx[^].

—SA


There's another access modifier, which would be the appropriate one in this case: protected

"Protected" members are accessible to the declaring class and to its subclasses, but not from outside.

So this will work:

protected int a, b, result;




edit:

If you don't specify an access modifier explicitly, private is the default (for class members). So this:

int a, b, result;

is equivalent to

private int a, b, result;

and private members aren't accessible to subclasses.

My suggestion: Always specify all access modifiers explicitly, for members as well as for classes.


这篇关于为什么不抽象成员自动标记为公开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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