GVKun编程网logo

CS0120:非静态字段,方法或属性'foo'需要对象引用(非静态字段方法或属性要求对象引用)

22

对于CS0120:非静态字段,方法或属性'foo'需要对象引用感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍非静态字段方法或属性要求对象引用,并为您提供关于.Python面对对象(北京赛车源码

对于CS0120:非静态字段,方法或属性'foo'需要对象引用感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍非静态字段方法或属性要求对象引用,并为您提供关于.Python 面对对象( 北京赛车源码开发反射,构造方法,静态字段,静态方法)、Android:非静态方法无法从静态上下文中引用。困惑?、Android:非静态方法无法从静态上下文中引用困惑?、ASP.NET MVC 错误“非静态字段、方法或属性需要对象引用”的有用信息。

本文目录一览:

CS0120:非静态字段,方法或属性'foo'需要对象引用(非静态字段方法或属性要求对象引用)

CS0120:非静态字段,方法或属性'foo'需要对象引用(非静态字段方法或属性要求对象引用)

考虑:

namespace WindowsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            //int[] val = { 0, 0};            int val;            if (textBox1.Text == "")            {                MessageBox.Show("Input any no");            }            else            {                val = Convert.ToInt32(textBox1.Text);                Thread ot1 = new Thread(new ParameterizedThreadStart(SumData));                ot1.Start(val);            }        }        private static void ReadData(object state)        {            System.Windows.Forms.Application.Run();        }        void setTextboxText(int result)        {            if (this.InvokeRequired)            {                this.Invoke(new IntDelegate(SetTextboxTextSafe), new object[] { result });            }            else            {                SetTextboxTextSafe(result);            }        }        void SetTextboxTextSafe(int result)        {            label1.Text = result.ToString();        }        private static void SumData(object state)        {            int result;            //int[] icount = (int[])state;            int icount = (int)state;            for (int i = icount; i > 0; i--)            {                result += i;                System.Threading.Thread.Sleep(1000);            }            setTextboxText(result);        }        delegate void IntDelegate(int result);        private void button2_Click(object sender, EventArgs e)        {            Application.Exit();        }    }}

为什么会发生此错误?

非静态字段,方法或属性’WindowsApplication1.Form1.setTextboxText(int)需要对象引用

答案1

小编典典

看起来您正在setTextboxText从静态方法(特别是SumData)调用非静态成员(特别是属性或方法)。您将需要:

  1. 也将被叫成员设为静态:

    static void setTextboxText(int result)

    {
    // Write static logic for setTextboxText.
    // This may require a static singleton instance of Form1.
    }

  2. Form1在调用方法中创建一个实例:

    private static void SumData(object state)

    {
    int result = 0;
    //int[] icount = (int[])state;
    int icount = (int)state;

    for (int i = icount; i > 0; i--){    result += i;    System.Threading.Thread.Sleep(1000);}Form1 frm1 = new Form1();frm1.setTextboxText(result);

    }

Form1也可以选择传入的实例。

  1. 将调用方法设为(Form1)的非静态实例方法:
    private void SumData(object state)

    {
    int result = 0;
    //int[] icount = (int[])state;
    int icount = (int)state;

    for (int i = icount; i > 0; i--){    result += i;    System.Threading.Thread.Sleep(1000);}setTextboxText(result);

    }

可以在MSDN上找到有关此错误的更多信息。

.Python 面对对象( 北京赛车源码开发反射,构造方法,静态字段,静态方法)

.Python 面对对象( 北京赛车源码开发反射,构造方法,静态字段,静态方法)

一、构造方法

在使用类创建对象的时候(北京赛车源码开发【话仙源码论坛】hxforum.com【木瓜源码论坛】papayabbs.com 就是类后面加括号)就自动执行__init__方法。

class A:
    def __init__(self):
        print(''A'')


class B:
    def __init__(self):
        print(''B'')

obj = A()


#虽然只是创建了obj对象,但是执行了__init__方法,输出了A

复制代码

 

Python 中派生类可以继承父类的构造方法

1. 基于 super ()

遇到 super () 就表示去执行父类的 xxx 属性

复制代码

class A:
    def __init__(self):
        print(''A'')
        self.tp = ''annimal''


class B(A):
    def __init__(self):
        print(''B'')
        super(B,self).__init__()
        #执行B父类中的__init__方法,这里的self是obj
     #super里的self不是传入后面的__init__方法里,而是直接传入A中的__init__,这里super会帮你传递参数

obj = B()

输出结果:
B
A

复制代码

2. 通过父类的名称执行父类的构造方法。

复制代码

class A:
    def __init__(self):
        print(''A'')
        self.tp = ''annimal''


class B(A):
    def __init__(self):
        print(''B'')
        A.__init__(self)
        #父类的名称跟上__init__()

obj = B()    


输出结果:
B
A

复制代码

  这两种方式中推荐 super,使用第二中方法的时候,进行继承父类的方法的时候,是你指定父类的方法,看起来比较乱,但是使用 super 的时候,没有让你指定父类的名称,这种继承时候就按照正常的继承规则(上一节所讲)来进行。

二、反射

利用反射查看面向对象的成员

根据字符串的形式去对象(某个模块)操作其成员

复制代码

class Foo:
    def __init__(self,name):
        self.name = name

    def show(self):
        print(''Fshow'')

r = hasattr(Foo,''show'')
print(r)
#查看Foo类中是否有show函数
obj = Foo(''alexsel'')
s = hasattr(obj,''name'')
print(s)
#查看obj对象里name是否存在
t = hasattr(obj,''show'')
print(t)
#通过对象查看是否存在show这个函数

输出结果:
True
True
True

复制代码

  我们通过类进行查询的时候,仅仅只能找到类中的属性,但是我们通过类创建的对象查询的时候,我们不但可以找到对象中的属性(例如,self.name,直接在类中查询找不到),还可以找到对象中的方法(例如 show 函数),因为对象中有一个指向类的指针,当通过对象进行查询的时候,可以通过指针找到类中的属性。

利用反射导入模块、查找类、创建对象、查找对象中的字段

1. 首先使用__import__导入文件

函数功能用于动态的导入模块,主要用于反射或者延迟加载模块。

 __import__(module) 相当于 import module

2. 使用 getattr (所导入的文件名,类名) 导入类

getattr 根据字符串的形式去某个模块中寻找东西

3. 通过找到的类创建对象

4. 获取对象中的属性

 

s2.py 文件中的代码

复制代码

class Foo:
    def __init__(self,name):
        self.name = name

    def show(self):
        print(''Fshow'')

复制代码

s1.py 文件中的代码

复制代码

m = __import__(''zp'')

class_name = getattr(m,''Foo'')

obj = class_name(''Alexsel'')

val = getattr(obj,''name'')

print(val)


输出结果:
Alexsel

复制代码

三、静态字段

静态字段的作用,将每个对象里存在的重复的东西,使用静态字段在类中只需写一份。

复制代码

class Foo:

    annimal = ''Cat''
    #这个是静态字段,是在类中保存的

    def __init__(self,name):
        temp = ''Alexsel''
        #普通字段,存放在对象中

    #普通方法,存放在类中
    def show(self):
        print(''SH'')

print(Foo.annimal)


输出结果:
Cat

复制代码

使用静态字段的时候,优先使用类名访问静态字段。

 

四、静态方法

  静态方法是类中的函数,不需要实例。静态方法主要是用来存放逻辑性的代码,主要是一些逻辑属于类,但是和类本身没有交互,即在静态方法中,不会涉及到类中的方法和属性的操作。

复制代码

class Foo:
    annimal = ''Cat''

    def __init__(self):
        pass

    def show(self):
        print(''SH'')

    #装饰器,使用这个装饰器装饰类中的一个方法,这个方法就变为静态方法
    @staticmethod
    def out():
        print(''out'')


Foo.out()
#静态方法中不需要传递self,所以访问静态方法优先使用类去访问
obj = Foo()
obj.out()


输出结果:
out
out

复制代码

静态方法可以让我们不需要创建对象就可以执行类中的方法。 

五、类方法

  类方法是将类本身作为对象进行操作的方法。他和静态方法的区别在于:不管这个方式是从实例调用还是从类调用,它都用第一个参数把类传递过来

复制代码

class Foo:
    annimal = ''Cat''

    def __init__(self):
        pass

    def show(self):
        print(''SH'')

    #装饰器,使用这个装饰器装饰类中的一个方法,这个方法就变为类方法
    @classmethod
    def out(cls):
        print(''out'',cls)


Foo.out()
#类方法,会自动将的类传递到类方法的cls中


输出结果:
out <class ''__main__.Foo''>

Android:非静态方法无法从静态上下文中引用。困惑?

Android:非静态方法无法从静态上下文中引用。困惑?

我对Java和一般编程非常陌生。我为一个基本程序编写了此代码,以将用户输入的2个数字相加,并将它们加起来并显示在输出框中,但是我得到了Non-static method 'setText(java.lang.CharSequence)' cannot be referenced from a static context,但是我不知道什么是静态的

private void onClick(View v) {
    EditText input1 = (EditText) findViewById(R.id.input1);
    double calc1 =  Double.parseDouble(String.valueOf(input1));
    EditText input2 = (EditText) findViewById(R.id.input2);
    double calc2 = Double.parseDouble(String.valueOf(input2));
    double total = calc1 + calc2;
    String result = Double.toString(total);
    EditText output1 = (EditText) findViewById(R.id.output);
    EditText.setText(result);
}

给出错误的行:

EditText.setText(result);

抱歉,如果我非常不称职,但我搜索了一下,但我真不明白该如何解决。谢谢。

Android:非静态方法无法从静态上下文中引用困惑?

Android:非静态方法无法从静态上下文中引用困惑?

我对Java和一般编程非常陌生。我为一个基本程序编写了此代码,以将用户输入的2个数字相加,并将它们加起来并显示在输出框中,但是我得到了Non-static method 'setText(java.lang.CharSequence)' cannot be referenced from a static context,但是我不知道什么是静态的

private void onClick(View v) {
    EditText input1 = (EditText) findViewById(R.id.input1);
    double calc1 =  Double.parseDouble(String.valueOf(input1));
    EditText input2 = (EditText) findViewById(R.id.input2);
    double calc2 = Double.parseDouble(String.valueOf(input2));
    double total = calc1 + calc2;
    String result = Double.toString(total);
    EditText output1 = (EditText) findViewById(R.id.output);
    EditText.setText(result);
}

给出错误的行:

EditText.setText(result);

抱歉,如果我非常不称职,但我搜索了一下,但我真不明白该如何解决。谢谢。

ASP.NET MVC 错误“非静态字段、方法或属性需要对象引用”

ASP.NET MVC 错误“非静态字段、方法或属性需要对象引用”

如何解决ASP.NET MVC 错误“非静态字段、方法或属性需要对象引用”?

在 ASP.NET MVC 项目中,我有一个如下所示的变量:

string test = "Main"

我有一个属性,它是一个常量,如下所示:

[Privilege(SysCaption = "Main")]
public const string View_ExampleDetailReport_Index = "View ExampleDetailReport Index";

我必须用 SysCaption 变量的值填充 test,如下所示:

[Privilege(SysCaption = test)]
public const string View_ExampleDetailReport_Index = "View ExampleDetailReport Index";

但我收到此错误:

非静态字段、方法或属性需要对象引用

我必须解决这个问题。任何帮助将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

我们今天的关于CS0120:非静态字段,方法或属性'foo'需要对象引用非静态字段方法或属性要求对象引用的分享就到这里,谢谢您的阅读,如果想了解更多关于.Python 面对对象( 北京赛车源码开发反射,构造方法,静态字段,静态方法)、Android:非静态方法无法从静态上下文中引用。困惑?、Android:非静态方法无法从静态上下文中引用困惑?、ASP.NET MVC 错误“非静态字段、方法或属性需要对象引用”的相关信息,可以在本站进行搜索。

本文标签: