将对象作为参数传递给方法JAVA

时间:2015-11-03 08:02:43

标签: java oop object

我获得了主要编码来完成子课程。 但我被困在这些编码中,如下所示:

z.setName(z.obj1);
z.setID(z.obj2);

**********它应该是插入输入的方式。 **************

==========我在下面展示的子类是我自己写的==========

主要编码如下:

public static void main(String[] args) {
    StudReg z = new StudReg();

    z.setName(z.obj1);
    z.setID(z.obj2);  

    System.out.println(z.getName());
    System.out.println(z.getID());
    System.out.println(z.getJava());
    System.out.println(z.getDatabase());

    StuComputing obj3,obj4;
    obj4 = new StuComputing();
    obj4.setStudReg(z);
    System.out.println(obj4.GPA().getGPA());
}

子类(StudReg)我做了如下:

public class StudReg {

//Data Member 
String Name;
String ID;
double Java,Database;

//Constructor
public StudReg(){};
public StudReg(String a,String b){
    Name = a;
    ID = b;
};



//Name
public void setName(String n){
    Name = n;
}
public String getName() {
    return Name;
}

//Id
public void setID (String i){
    ID = i;
}
public String getID (){
    return ID;
}

//Java
public void setJava (double j){
    Java = j;
}
public double getJava (){
    return Java;
}

//Database
public void setDatabase (double d){
    Database = d;
}
public double getDatabase (){
    return Database;
}



//FUNCTION
public StudReg (StudReg gg){
    double aa,bb;

    //refer to data from MAIN
    aa = this.getJava();
    bb = this.getDatabase();
}

另一个SubClass - StuComputing:

public class StuComputing {

//DATA MEMBER
public StudReg ss;
double GPA;

//CONSTRUCTOR
public StuComputing (){};
public StuComputing (double a1){       
    GPA = a1;              
};



//StudReg
public void setStudReg (StudReg st){
    ss = st;
}
public StudReg getStudReg(){
    return ss;
}


//GPA
public void setGPA(double g){
    GPA = g;
}
public double getGPA(){
    return GPA;
}

除了回答我的问题之外,你们都能展示一个简单的例子吗? 所以,我可以轻松地理解它><

谢谢

2 个答案:

答案 0 :(得分:0)

你一定忘记了什么。您的班级StudReg中没有“obj1”或“obj2”属性。

那么我们怎样才能理解你的问题呢?我想你需要用其他措辞再次提出你的问题。

答案 1 :(得分:0)

我仍然不知道你真正想做什么...试试这个

class StudReg {

String obj1 ;
String obj2 ;
double java ;
double database ;
String name ;
String id ;

public double getJava() {
    return java;
}

public double getDatabase() {
    return database;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getID() {
    return id;
}

public void setID(String id) {
    this.id = id;
}

public String getObj1() {
    return obj1;
}

public void setObj1(String obj1) {
    this.obj1 = obj1;
}

public String getObj2() {
    return obj2;
}

public void setObj2(String obj2) {
    this.obj2 = obj2;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

}

class StuComputing {

StudReg studReg;
GPA gpa;

public StuComputing() {
    gpa = new GPA();
}


public StudReg getStudReg() {
    return studReg;
}

public void setStudReg(StudReg sr) {
    this.studReg = sr;
}

public GPA GPA() {
 return gpa;  
}

}

public class GPA {
double gpa;

public GPA() {
}


public double getGPA(){
    return gpa;
}

}