C# 如何将引用传递给另一个变量的对象?
我声明了一个名为ShowOne的Car类型变量,在循环体中创建了一个类Car的实例,在循环体之后,我尝试为循环中创建的类分配一个引用,告诉我正确的链接传递实践C# 如何将引用传递给另一个变量的对象?,c#,C#,我声明了一个名为ShowOne的Car类型变量,在循环体中创建了一个类Car的实例,在循环体之后,我尝试为循环中创建的类分配一个引用,告诉我正确的链接传递实践 static void Main(string[] args) { int height = 0; int peoplePlane = 0; int peopleShip = 0; string port = null; string Plane
static void Main(string[] args)
{
int height = 0;
int peoplePlane = 0;
int peopleShip = 0;
string port = null;
string Plane = "Plane";
string Car = "Avto";
string Ship = "Ship";
Console.WriteLine("Specify vehicle parameters:");
Console.WriteLine(new string('-', 10));
Welcome infoShowWelcome = new Welcome();
Vehicle TransportShow = new Vehicle();
Car ShowOne;
Plane ShowTwo;
Ship ShowThree;
for (int i = 0; i <= 2; i++)
{
string nameTransport;
if (i == 0)
{
nameTransport = Car;
infoShowWelcome.ShowInfo(nameTransport);
Car TransportOne = new Car(infoShowWelcome);
ShowOne = TransportOne;
}
else if (i == 1)
{
nameTransport = Plane;
infoShowWelcome.ShowInfo(nameTransport);
Console.WriteLine("height" + " " + nameTransport + ":");
height = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("people" + " " + nameTransport + ":");
peoplePlane = Convert.ToInt32(Console.ReadLine());
Plane TransportTwo = new Plane(infoShowWelcome, height, peoplePlane);
ShowTwo = TransportTwo;
}
else if (i == 2)
{
nameTransport = Ship;
infoShowWelcome.ShowInfo(nameTransport);
Console.WriteLine("port" + " " + nameTransport + ":");
port = Console.ReadLine();
Console.WriteLine("people" + " " + nameTransport + ":");
peopleShip = Convert.ToInt32(Console.ReadLine());
Ship TransportThree = new Ship(infoShowWelcome, port, peopleShip);
ShowThree = TransportThree;
}
else
{
break;
}
ShowOne.ShowInfo();
ShowTwo.ShowInfo();
ShowThree.ShowInfo();
}
Console.ReadKey();
}
}
static void Main(字符串[]args)
{
整数高度=0;
int peoplePlane=0;
int peopleShip=0;
字符串端口=null;
字符串平面=“平面”;
字符串Car=“Avto”;
字符串Ship=“Ship”;
控制台。WriteLine(“指定车辆参数:”);
Console.WriteLine(新字符串('-',10));
欢迎信息showWelcome=新欢迎();
车辆运输展=新车();
汽车展;
飞机显示两个;
三号船;
对于(int i=0;i来说,代码中有很多东西并不是真正的“最佳实践”。但是,如果您特别询问如何将引用分配给变量,那么您已经正确地完成了。您可以缩短它以消除临时变量
尽管您的代码是正确的:
Car TransportOne = new Car(infoShowWelcome);
ShowOne = TransportOne;
它可以缩短为:
ShowOne = new Car(infoShowWelcome);
…假设您以后不会在其他地方使用临时TransportOne
变量
很少看到您的代码以循环的方式编写,但不使用计数器。实际上,您唯一重用的代码是在每次迭代后执行此操作的位:
ShowOne.ShowInfo();
ShowTwo.ShowInfo();
ShowThree.ShowInfo();
但是你会发现1)你没有初始化你的变量,2)你第一次去显示它们时它们是空的。如果你在声明它们的地方显式地将它们初始化为空,你就会明白为什么了
我可能倾向于将其放入一个函数中,然后“展开”循环以完全消除它,除非它会崩溃,因为ShowTwo在第一次迭代后未定义。无论如何,您可能只想在结束时将它们全部显示一次。因此,这也是赞成展开循环的另一点。如下所示:
static void Main(string[] args)
{
int height = 0;
int peoplePlane = 0;
int peopleShip = 0;
string port = null;
string Plane = "Plane";
string Car = "Avto";
string Ship = "Ship";
Console.WriteLine("Specify vehicle parameters:");
Console.WriteLine(new string('-', 10));
Welcome infoShowWelcome = new Welcome();
Vehicle TransportShow = new Vehicle();
Car ShowOne = null;
Plane ShowTwo = null;
Ship ShowThree = null;
string nameTransport;
nameTransport = Car;
infoShowWelcome.ShowInfo(nameTransport);
Car TransportOne = new Car(infoShowWelcome);
ShowOne = TransportOne;
nameTransport = Plane;
infoShowWelcome.ShowInfo(nameTransport);
Console.WriteLine("height" + " " + nameTransport + ":");
height = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("people" + " " + nameTransport + ":");
peoplePlane = Convert.ToInt32(Console.ReadLine());
Plane TransportTwo = new Plane(infoShowWelcome, height, peoplePlane);
ShowTwo = TransportTwo;
nameTransport = Ship;
infoShowWelcome.ShowInfo(nameTransport);
Console.WriteLine("port" + " " + nameTransport + ":");
port = Console.ReadLine();
Console.WriteLine("people" + " " + nameTransport + ":");
peopleShip = Convert.ToInt32(Console.ReadLine());
Ship TransportThree = new Ship(infoShowWelcome, port, peopleShip);
ShowThree = TransportThree;
ShowOne.ShowInfo();
ShowTwo.ShowInfo();
ShowThree.ShowInfo();
Console.ReadKey();
}
虽然从技术上讲这是合法的,但让字符串和类具有相同的非限定名称是非常糟糕的做法。一定要改变这一点
恕我直言,在您的代码中有很多不好的做法,但我不得不在这里停下来,只关注您所问的部分,即对对象的引用
如果其他人需要它,这里有一些空的实现(这是一个完整示例所必需的)
这不是C,甚至C++。C看起来是“代码> CUA/CODE >是一个类的名称。你试图设置<代码> NAMEPATION/COD> >它。我不认为你可以这样做。1.你不能将类(Car)分配给字符串引用(NAMEOTION)。2。你缺少一个关闭括号<代码> } /代码>。3.什么是infoShowWelcome
?4.你想达到什么目的。。。一般来说,请更具体地说明您的问题,您会得到很好的答案:)在这种情况下,您应该重命名Car
变量。即使在本例中这不是一个问题,但将变量命名为与类相同的名称仍然是一种不好的形式。即使是像myCar
这样简单的东西也可以。@Wyck我发布了完整的Main(),非常感谢,它都是关于未初始化的变量,如Car、Ship、Plane。在初始化它们为空之后,一切都正常了,感谢您对不良做法的评论,我很快编写了这段代码,但我听取了您的建议!告诉我为什么对对象的引用没有在初始声明为null的情况下分配给变量当你说“告诉我为什么对对象的引用没有在初始声明为null的情况下分配给变量”时,我必须说引用是正确分配给变量的。您可以从我的示例中删除=null
,它仍然可以工作。我添加它们是为了说明您可能会收到关于showtow2.ShowInfo()的警告代码>在循环的第一次过程中引用未初始化的变量showtow2.ShowInfo()
将在循环的第一次传递时引发null引用异常,因为尚未为其分配任何内容。
internal class Ship
{
private Welcome infoShowWelcome;
private string port;
private int peopleShip;
public Ship(Welcome infoShowWelcome, string port, int peopleShip)
{
this.infoShowWelcome = infoShowWelcome;
this.port = port;
this.peopleShip = peopleShip;
}
internal void ShowInfo()
{
Console.WriteLine(this);
}
}
internal class Plane
{
private Welcome infoShowWelcome;
private int height;
private int peoplePlane;
public Plane(Welcome infoShowWelcome, int height, int peoplePlane)
{
this.infoShowWelcome = infoShowWelcome;
this.height = height;
this.peoplePlane = peoplePlane;
}
internal void ShowInfo()
{
Console.WriteLine(this);
}
}
internal class Car
{
private Welcome infoShowWelcome;
public Car(Welcome infoShowWelcome)
{
this.infoShowWelcome = infoShowWelcome;
}
internal void ShowInfo()
{
Console.WriteLine(this);
}
}
internal class Vehicle
{
public Vehicle()
{
}
}
internal class Welcome
{
public Welcome()
{
}
internal void ShowInfo(string nameTransport)
{
Console.WriteLine(this);
}
}