如何在C#中调用方法 [英] How to call a method in C#
本文介绍了如何在C#中调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何调用在此类代码中定义的方法。需要在此代码中调用方法一个演示文稿
private class MapPanel :Control
{
public void demo()
{
// 此处代码
}
}
i无法通过简单地创建类的对象来获得。
解决方案
我创建了类像这样,并使用System在类
中调用;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
namespace ConsoleApplication3
{
class MapPanelOuter
{
public MapPanelOuter()
{
MapPanel mp = new MapPanel() ;
mp.demo();
}
私有类MapPanel:Control
{
public void demo()
{
Console.WriteLine(Called MapPanel Demo Method );
}
}
}
}
和另一个班级我打电话给这个
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
命名空间ConsoleApplication3
{
class程序
{
static void Main(string [] args)
{
MapPanelOuter mo = new MapPanelOuter();
Console.ReadKey();
}
}
}
1.Because你的demo()
方法是公共的,您的类MapPanel
是private
您无法从此类外部访问此方法。为了能够访问它,首先你必须使你的类public
,然后使用MapPanel $ c $类型的对象c>你可以访问它。
2.如果你想访问你的demo()
此类中的方法,不使用类型为MapPanel
的对象,您必须声明demo()
方法public static
,但在第二种情况下,首先你必须让你的班级公开
。
你可以在类中调用这个类Mappanel
如果下面的类是内部类,那么你可以在父类中为内部类创建一个对象,你也可以使用Mappanel调用内部类方法对象
How to call a method which is define inside this type of the code. need to call method a demo present inside the this code
private class MapPanel : Control
{
public void demo()
{
// Code here
}
}
i am not able to get by simply creating object of the Class.
解决方案
I have created class like this and called with in the class using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace ConsoleApplication3 { class MapPanelOuter { public MapPanelOuter() { MapPanel mp = new MapPanel(); mp.demo(); } private class MapPanel : Control { public void demo() { Console.WriteLine("Called MapPanel Demo Method"); } } } } and in another class i have called like this using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { MapPanelOuter mo = new MapPanelOuter(); Console.ReadKey(); } } }
1.Because yourdemo()
method is public and your classMapPanel
isprivate
you cannot access this method from outside of this class. In order to can have access to it, first you have to make your classpublic
, then by using an object of typeMapPanel
you will have access to it.
2.If you want to have access to yourdemo()
method from this class without to use an object of typeMapPanel
you have to declare yourdemo()
methodpublic static
, but also in this 2nd case first you have to make your classpublic
.
you can call this class Mappanel out side the class
if the following class is inner class then you can create an object for inner class in parent class and you can also call inner class method using Mappanel object
这篇关于如何在C#中调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文