java中怎么在创建对象时一并赋值?

如题,比如在main函数中使用student, student a = new student(a,b,c); a,b,c分别代表不同的变量,那么我在写class student的时候应该怎么写呢?是public class student{ public student(int a,int b,int c){.....}}么,还是其他的方式?求指教

您好,提问者:
    è¿™ä¸ªåŸºæœ¬èƒ½æƒ³åˆ°çš„有两种,如下代码:

public class Student{
    private int a;
    private int b;
    private int c;
    public Student(int a, int b, int c){
        this.a = a;
        this.b = b;
        this.c = c;
    }
}class Person{
    private int a;
    private int b;
    private int c;
    public Person(int a, int b, int c){
        this.a = a;
        this.b = b;
        this.c = c;
    }
}
public class Student{
    public Student(int a, int b, int c){
        super(a,b,c);
    }
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-07-19

    Java创建对象并赋值,示例如下

  Interger i = new Interger(1);
  String s = “我已经被创建了";

     创建对象可以指定构造函数里面的初始化变量,也可以像string对象一样,直接用双引号生成对象,并且是不可以改变的。

第2个回答  2014-06-05
//加一个三参数的构造函数。
public class student(){
    public student(int a,int b,int c){
    }
}

第3个回答  2014-06-06
构造器 没理解
相似回答