C#引用方法出现非静态的字段方法或属性要求对象引用错误
在Program中调用CanDo的时候出现错误:非静态的字段方法或属性要求对象引用classProgram{staticvoidMain(string[]args){Pe...
在Program中调用CanDo的时候出现错误:非静态的字段方法或属性要求对象引用
class Program
{
static void Main(string[] args)
{
Person youHongHui = new Person();
youHongHui._age = 15;
youHongHui._name = "张三";
youHongHui._gender = '男';
Person.CanDo();
}
}
class Person
{
public string _name;
public char _gender;
public int _age;
public void CanDo()
{
Console.WriteLine("我是{0},我是{1}生,我今年{2}岁了", this. _name,this. _gender,this. _age);
}
}
如果Cando加上static,后面的占位符又报错 展开
class Program
{
static void Main(string[] args)
{
Person youHongHui = new Person();
youHongHui._age = 15;
youHongHui._name = "张三";
youHongHui._gender = '男';
Person.CanDo();
}
}
class Person
{
public string _name;
public char _gender;
public int _age;
public void CanDo()
{
Console.WriteLine("我是{0},我是{1}生,我今年{2}岁了", this. _name,this. _gender,this. _age);
}
}
如果Cando加上static,后面的占位符又报错 展开
1个回答
展开全部
你要改成下面这样:
class Program
{
static void Main(string[] args)
{
Person youHongHui = new Person();
youHongHui._age = 15;
youHongHui._name = "张三";
youHongHui._gender = '男';
youHongHui.CanDo();//Person.CanDo(); 要使用对象youHongHui的CanDo方法。
}
}
class Person
{
public string _name;
public char _gender;
public int _age;
public void CanDo()
{
Console.WriteLine("我是{0},我是{1}生,我今年{2}岁了", this. _name,this. _gender,this. _age);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询