c#,. net中的访问方法 [英] acessing method in c#,.net

查看:90
本文介绍了c#,. net中的访问方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Hide
{
    public void method1()
    {
     Console.WriteLine("method 1 called");
    
    }
    public void method2()
    {
     Console.WriteLine("method 2 called");
    }
}
public class Base : Hide
{
//here i have to only acess method1()
}
public class Child : Hide
{
//here i have to only acess method2()
}





Is it possible or not??

推荐答案

不是您编写它的方式.您想做的是在思想上有些落后.
基类中的公共方法将对所有继承自该基类的类可用.
如果需要只对特定孩子可用的方法,则必须仅在该类中而不是在基类中定义该方法.
请注意,您的命名约定有点奇怪.请参阅我的示例,了解我的目标.
例如:
Not the way you have written it. What you want to do is a little backwards in your thinking.
Public methods in your base class will be available to all classes which inherit from it.
If you need a method to be available only to a specific child, then you must define that method only in that class and not the base.
Note that your naming convention is a little strange. See my example for what I think you are aiming at.
For example:
public class Base
{
   public void Method1(){ Console.WriteLine("Method 1 called."); }
   abstract void Method2();
}
public class Hide : Base
{
   // Method1 is available because of inheritance
   // Method2 must be overriden in this class because it has been defined in base as abstract
   public override void Method2(){ Console.WriteLine("Method 2 called in Hide."); }
   // Method3 is only available in Hide
   public void Method3(){ Console.WriteLine("Method 3 called."); }
}
public class Child : Base
{
   // Method1 is available because of inheritance
   // Method2 must be overriden in this class because it has been defined in base as abstract
   public override void Method2(){ Console.WriteLine("Method 2 called in Child."); }
   // Method4 is only available in Child
   public void Method4(){ Console.WriteLine("Method 3 called."); }
}


这篇关于c#,. net中的访问方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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