会c#的麻烦进来帮下忙
来源:学生作业帮 编辑:搜搜做题作业网作业帮 分类:综合作业 时间:2024/08/06 11:47:53
会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!”会在最上面出来而不是最下面?
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!”会在最上面出来而不是最下面?
![会c#的麻烦进来帮下忙](/uploads/image/z/3375399-39-9.jpg?t=%E4%BC%9Ac%23%E7%9A%84%E9%BA%BB%E7%83%A6%E8%BF%9B%E6%9D%A5%E5%B8%AE%E4%B8%8B%E5%BF%99)
public car(){}是car这个类得构造函数,执行时首先执行入口函数 static void Main(),第一句Car benz = new Car();
当你new出一个car的实体对象的时候 就会走他的构造函数,所以他会出现在最上面
当你new出一个car的实体对象的时候 就会走他的构造函数,所以他会出现在最上面