如何传递对另一个变量的对象的引用?

时间:2019-01-31 19:58:05

标签: c#

我声明一个名为ShowOne的Car类型的变量,在循环的主体中创建Car类的实例,然后在循环的主体中尝试分配对在循环中创建的类的引用,告诉我正确的链接传递练习?

public static void main(String[] args) {

    Scanner sc=new Scanner(System.in);
    String A=sc.next();

    char[] test1 = A.toCharArray();
    int i = 0;
    int j = 0;

    char[] test2 = test1;

    for (i=0; i<test1.length;i++){

        j = test1.length-(i+1);
        test2[i]=test1[j];         
    }
    System.out.print("\n");
    for ( i=0; i<test2.length; i++){
        System.out.print(test2[i]+" ");
    }            
    System.out.print("\n");
}

VS强调文本ShowOne:使用未分配值的局部变量。

1 个答案:

答案 0 :(得分:0)

关于代码的很多事情并不是真正的“最佳实践”。但是,如果您特别询问如何将引用分配给变量,那么您就正确地做到了。您也许可以缩短它以消除临时变量。

尽管您的代码是正确的:

Car TransportOne = new Car(infoShowWelcome);
ShowOne = TransportOne;

可以缩短为:

ShowOne = new Car(infoShowWelcome);

...假设您以后不打算在其他地方使用临时TransportOne变量。

看到您的代码以循环的方式编写但不使用计数器是很不寻常的。确实,您重复使用的唯一代码是在每次迭代后执行此操作的位:

ShowOne.ShowInfo();
ShowTwo.ShowInfo();
ShowThree.ShowInfo();

但是您会发现1)您尚未初始化变量,并且2)首次访问变量时它们为null。如果您在声明它们的位置将它们显式初始化为null,则会明白原因。

我可能倾向于将其放入函数中,然后“展开”循环以完全消除它,除了它会崩溃的事实是,因为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();
}

尽管在技术上是合法的。这样的字符串和类具有相同的不合格名称,这确实是不好的做法。绝对改变它。

恭喜,您的代码中还有许多不良做法,但是我不得不在这里停止,只关注您所询问的部分,即对对象的引用。


万一其他人需要它,这里是空的实现(需要使它成为一个完整的示例)。

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);

    }
}