Java:类型转换问题

Hei*_*sis 0 java casting

有没有办法在一行中做到这一点:

    TextView tv = (TextView) findViewById(R.id.lbSightName);
    tv.setText("Some Text");
Run Code Online (Sandbox Code Playgroud)

我想做声明中介电视,像这样:

(TextView) findViewById(R.id.lbSightName).setText("Some Text");
Run Code Online (Sandbox Code Playgroud)

不可能?

Sta*_*lin 5

你可以,但这不是最好的做法

((TextView) findViewById(R.id.lbSightName)).setText("Some Text");
TextView.class.cast(findViewById(R.id.lbSightName).setText("Some Text");
Run Code Online (Sandbox Code Playgroud)