将一个类别的对象调用到另一个类别 [英] call objects of one class into another

查看:77
本文介绍了将一个类别的对象调用到另一个类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,

我正在.aspx.cs文件中创建一个具有属性和方法的类.我在第一次发布页面时调用此方法,并设置属性值.现在,我想在App_Code中存在的文件中使用这些属性.可以做到这一点.如果是这样,该怎么办.

等待回复.
谢谢



I am creating a class in .aspx.cs file, which has properties and a method. I call this method when the page is posted for the first time and set the propery values. Now i want to use these properties in a file which is present in App_Code. Can this be done. if so how can it be done.

Waiting for the reply.
Thanks

推荐答案

没有调用对象的概念.您只能调用方法;好吧,属性也是如此,因为可以分别通过getter或setter方法完成读取或赋值.

方法可以是静态方法(在某些语言中称为类方法",因为它与类本身有关,而与特定实例无关)或实例方法.仅有一个区别:实例方法需要一个实例来调用,实例方法的主体接收对在调用过程中传递的实例的引用.此引用通过名为"this"的隐藏"参数传递.

现在我要解释一下语法.不标记问题中的语言是不好的.如何解释语法?由于通常是.NET默认使用的C#,因此我将在C#中显示它:

语法也有所不同:
There is no such concept as calling an object. You can only call method; well, property too, as reading or assignment can be done via getter or setter methods, respectively.

A method can be a static method (in some languages called "class method" as it related to the class itself and not to any instance in particular) or instance method. There is only one difference: instance method require an instance to call, and the body of instance method receives a reference to the instance passed during a call; this reference is passed via a "hidden" parameter called "this".

Now I have to explain the syntax. It''s was not nice not to tag the language in the question. How can I explain the syntax? As it''s usually C# which is assumed by default for .NET, I''ll show it in C#:

The syntax is a bit different, too:
class MyClass {
    internal static void ClassMethod() { //cannot address members of any instance, only other static members
    } //ClassMethod
    internal void InstanceMethod() { /* ... */ this.Field = /* ... */ }
    int Field;
}

MyClass ClassMethod(); //no instance needed, no "this" reference passed

MyClass instance = new MyClass( /* ... */ );
instance.InstanceMethod(); // during execution of the body of the method this == instance



剩下的方面是访问修饰符.默认情况下,在private中访问.访问修饰符internal(请参见上面的示例)打开来自任何其他类的访问,并将任何代码放置在同一程序集中.该程序集可以被其他程序集引用,但是如果internalpublic替换,它们可以访问上面显示的方法.
请注意,访问修饰符不提供任何保护(但是对于避免意外错误和隔离代码的部分非常有帮助,因此最好不要过度发布"任何内容以提供尽可能少的访问权限).使用反射,可以访问每个成员,而无论访问修饰符是什么.

也可以使用Reflection进行呼叫,但这是一个完全不同的故事.

—SA



Remaining aspect is access modifiers. By default, access in private. The access modifier internal (see the sample above) opens up access from any other class, any code placed in the same assembly. This assembly can be references by other assemblies, but they can get access to the methods shown above is internal is replaced with public.

Note, that access modifiers does not provide any protection (but very helpful to avoid accidental mistakes and isolate parts of code, so it''s the best not to "over-publish" anything providing minimal possible access). With Reflection, every member can be accessed regardless access modifier.

A call also can be done with Reflection, but this is quite a different story.

—SA


这篇关于将一个类别的对象调用到另一个类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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