作业帮 > 综合 > 作业

会c#的麻烦进来帮下忙

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/05/01 05:24:10
会c#的麻烦进来帮下忙
using System;
using System.Collections.Generic;
using System.Text;
namespace 类
{
class Dog
{
/* static void Main()
{
Dog Haba = new Dog();
Haba.eat();
Haba.sound();
}*/
public void eat()
{
Console.WriteLine("吃啊!");
}
public void sound()
{
Console.WriteLine("叫啊!");
}
}
class Car
{
static void Main()
{
Car benz = new Car();
Dog Haba = new Dog();
benz.run();
Haba.eat();
Haba.sound();
}
void run()
{
Console.WriteLine("好快!");
}
public Car()
{
Console.WriteLine("ABC!");
}
}
}
我是初学者,我想问问 public car(){} 为什么“ABC!”会在最上面出来而不是最下面?
public car(){}是car这个类得构造函数,执行时首先执行入口函数 static void Main(),第一句Car benz = new Car();
当你new出一个car的实体对象的时候 就会走他的构造函数,所以他会出现在最上面