为什么这不可能? [英] why this is not possible ?

查看:94
本文介绍了为什么这不可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public   class  Sa {
int rollno;
字符串名称;
void insertRecord( int r, String n){ // 方法
rollno = r;
name = n;
}
void displayInformation(){System。 out .println(rollno + < span class =code-string> + name);} // 方法
public static < span class =code-keyword> void main( String args []){
Sa s2,s1 = new Sa();
// Sa s2 = new Sa();

s1.insertRecord( 111 Karan);
s2.insertRecord( 222 雅利安);
s1.displayInformation();
s2.displayInformation();
}
}

解决方案

我想你在这里谈论这个部分:

 Sa s2,s1 = new Sa(); 





并希望a。)s2和s1指向同一个Sa对象或b。)每个s1和s2指向两个不同的Sa实例。 b)根本没有任何意义,因为你写的行中只有一个实例化调用(new),但是你可以把它重写为



 Sa s2 =  new  Sa(),s1 =  new  Sa(); 





a)无法工作,因为每次初始化都需要自己的赋值运算符。见上文。



你可以通过阅读语言规范自己找到这个。



干杯!


public class Sa {
     int rollno;
     String name;
     void insertRecord(int r, String n){  //method
      rollno=r;
      name=n;
     }
     void displayInformation(){System.out.println(rollno+" "+name);}//method
     public static void main(String args[]){
      Sa s2,s1=new Sa();
      //Sa s2=new Sa();

      s1.insertRecord(111,"Karan");
      s2.insertRecord(222,"Aryan");
      s1.displayInformation();
      s2.displayInformation();
     }
}

解决方案

I suppose you're talking about this part here:

Sa s2,s1=new Sa();



and would like to a.) s2 and s1 point to the same Sa object or b.) have each s1 and s2 point to two different Sa instances. b) would not make any sense at all since there is only one instantiation call (new) in the line you wrote, but you could rewrite it as

Sa s2 = new Sa(), s1 = new Sa();



a) wouldn't work either since each initialization needs its own assignment operator. See above.

You could have found this out by yourself though by reading the language specification.

Cheers!


这篇关于为什么这不可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆