class Car
{
public Car() { }
public Car(string name,string color,string productplace) //定义构造方法
{
this.Name = name;
this.Color = color;
this.Productplace = productplace;
}
Car daBen = new Car("奔驰","黑色","德国"); //报错位置
string color; //颜色 public string Color
{
get { return color; }
set { color = value; }
}
编译器报错:未处理的“System.StackOverflowException”类型的异常出现在 chapter2.exe 中。
有谁知道是什么原因吗?
{
public Car() { }
public Car(string name,string color,string productplace) //定义构造方法
{
this.Name = name;
this.Color = color;
this.Productplace = productplace;
}
Car daBen = new Car("奔驰","黑色","德国"); //报错位置
string color; //颜色 public string Color
{
get { return color; }
set { color = value; }
}
编译器报错:未处理的“System.StackOverflowException”类型的异常出现在 chapter2.exe 中。
有谁知道是什么原因吗?
using System;
using System.Collections.Generic;
using System.Text;namespace chapter2
{
class Program
{
static void Main(string[] args)
{
Car car = new Car();
car.Run(); //调用方法
Console.ReadLine();
}
}
class Car
{
public Car() { }
public Car(string name,string color,string productplace) //定义构造方法
{
this.Name = name;
this.Color = color;
this.Productplace = productplace;
}
Car daBen = new Car("奔驰","黑色","德国"); //创建对象
string color; //颜色 public string Color
{
get { return color; }
set { color = value; }
}
string name; //车名 public string Name
{
get { return name; }
set { name = value; }
}
string productplace; //产地 public string Productplace
{
get { return productplace; }
set { productplace = value; }
}
//方法
public void Run()
{ string color2 = this.Color;
string name2 = this.Name;
string productname2 = this.Productplace;
Console.WriteLine("我是一辆{0}车,{1}颜色,产地是{2}", name2, color2, productname2);
} }
}
car是成员变量,在构造函数之前,会先按你定义的去初始化,那么就会先执行
Car daBen = new Car("奔驰","黑色","德国"); //报错位置
这一行,然后才会调用构造函数,但这一行同时也是创造了一个新对象,新对象又要先执行新对象的
Car daBen = new Car("奔驰","黑色","德国"); //报错位置
就无限循环进去了。
public class B
{
private int i;
public B b = new B();
public B()
{
i = 100;
}
public B(int a)
{
i = a;
}
}例如你外面执行一行:
B obj = new B();
类初始化的过程:
1.创建一个该类型引用:
B类型引用obj
2.创建成员变量或成员变量的引用
int a;
B b = new B();//这里,比构函数执行的早。又一次调用了new,也就让代码递归进入到新的step 1中了。
3.执行构造函数进行初始化操作
4.将创建好的对象赋值给obj.
Car daBen = new Car("奔驰","黑色","德国"); //创建对象不知道LZ这样写是何用意,是想默认Color,Name,Productplace的值吗?如果是这个意思,建议楼主修改成如下代码,using System;
using System.Collections.Generic;
using System.Text;namespace chapter2
{
class Program
{
static void Main(string[] args)
{
Car car = new Car();
car.Run(); //调用方法
Console.ReadLine();
}
}
class Car
{
public Car()
{
this.Name = "奔驰";
this.Color = "黑色";
this.Productplace = "德国";
}
public Car(string name,string color,string productplace) //定义构造方法
{
this.Name = name;
this.Color = color;
this.Productplace = productplace;
}
//Car daBen = new Car("奔驰","黑色","德国"); //这行代码就是报错的地方了
string color; //颜色
public string Color
{
get { return color; }
set { color = value; }
} string name; //车名
public string Name
{
get { return name; }
set { name = value; }
} string productplace; //产地
public string Productplace
{
get { return productplace; }
set { productplace = value; }
} //方法
public void Run()
{
string color2 = this.Color;
string name2 = this.Name;
string productname2 = this.Productplace;
Console.WriteLine("我是一辆{0}车,{1}颜色,产地是{2}", name2, color2, productname2);
} }
}
using System.Collections.Generic;
using System.Text;namespace chapter2
{
class Program
{
static void Main(string[] args)
{
int aa = 3;
Car car = new Car("奔驰", "黑色", "德国");
//Car car = new Car();
car.Run(); //调用方法
Console.ReadLine();
}
}
class Car
{
public Car() { }
public Car(string name, string color, string productplace) //定义构造方法
{
this.Name = name;
this.Color = color;
this.Productplace = productplace;
}
//Car daBen = new Car("奔驰", "黑色", "德国"); //创建对象
string color; //颜色 public string Color
{
get { return color; }
set { color = value; }
}
string name; //车名 public string Name
{
get { return name; }
set { name = value; }
}
string productplace; //产地 public string Productplace
{
get { return productplace; }
set { productplace = value; }
}
//方法
public void Run()
{ string color2 = this.Color;
string name2 = this.Name;
string productname2 = this.Productplace;
Console.WriteLine("我是一辆{0}车,{1}颜色,产地是{2}", name2, color2, productname2);
} }
}
这样是成功的,楼主调试一下就什么都明白了
这句前加上 static关键字就可以了