【发布时间】:2020-10-23 22:43:42
【问题描述】:
C# 新手想知道是否可以从方法中调用对象
我必须继续打字吗
Console.WriteLine(James.name); Console.WrtieLine(James.age);
对于我制作的每个新对象?
对不起,如果这是一个简单的问题。 :(
示例:
https://i.stack.imgur.com/6GVXV.png
命名空间示例 {
class Dog
{
public string name;
public int age;
public Dog(string _name, int _age)
{
name = _name;
age = _age;
}
}
class Program
{
public static void Main()
{
Dog James = new Dog("James", 4);
Dog Daniel = new Dog("Daniel", 2);
}
//I know from thispart it does not work but is there a way to make a similar result?
status(James);
status(Daniel);
}
public static void status(thisdog)
{
Console.WriteLine(thisdog.name);
Console.WrtieLine(thisdog.age);
}
【问题讨论】:
-
应该是
void status(Dog thisdog)- 你错过了类型 -
了解如何在
Dog上覆盖ToString -
非常感谢你们,我会调查的