如何在java中将变量设置为传入参数?

时间:2016-12-24 20:24:18

标签: java

import java.io.BufferedReader; import java.io.InputStreamReader;

//完成下面的模板

公共课问题3_1 {

//complete this class, called Problem3_1, with the following items: 

//1. Declare four attributes, name, age, height, and weight of types String and int-s.  
//Write a constructor for this class that initializes ONLY the name, age, and height to three incoming arguments,  
//and sets the weight to always be -1 (the latter is not an incoming argument).
String name;
int age;
int height;
int weight;
Address address;
public Problem3_1(String name, int age, int height) {
    this.name = name;
    this.age = age;
    this.height = height;
    weight = -1;
}

void setAddress(int number, String street) {
    address = new Address(number, street);
}




//2. Imagine there is a class called Address that you have access to (it's below).  
//Its constructor takes an integer street number and a String street. Add an attribute called address to  
//the Problem3_1 class, and create a method called setAddress that sets the attribute to the incoming argument.  

public static class Address{ 

    int number; 
    String street; 

    public Address(int number, String street){ 
        this.number = number; 
        this.street = street; 
    }
} 

}

那么为什么代码说我在错误的位置有这个无效信息?它的位置在哪里?或者为什么不能“应用”?

PS:我得到的错误:

  问题3_1.java:82:问题3_1中的setAddress(int,java.lang.String)   无法应用于(Problem3_1.Address)           p.setAddress(一);

测试用例:

public static void main(String[] args){ 

    /*Below are tests that will check if you completed the code above correctly; if  
      your code doesn't compile, you'll need to fix those errors first. 

      DO NOT WRITE CODE BELOW THIS POINT 
    */ 

    int failed = 0; 

    Problem3_1 p = new Problem3_1("Jane", 22, 65); 

    if (p.name.compareTo("Jane") == 0 && p.age == 22 && p.height == 65 && p.weight == -1) 
        System.out.println("Test 1 passed!"); 
    else{ 
        failed ++; 
        System.out.println("Please check your code for question 1! "); 
    } 

    Address a = new Address(12, "Fairfax Dr"); 
    p.setAddress(a); 

    if (p.address == a) 
        System.out.println("Test 2 passed!"); 
    else{ 
        failed ++; 
        System.out.println("Please check your code for question 2! "); 
    }         


    if (failed > 0) 
        System.out.println(systemCall("Failed")); 
    else{ 
        System.out.println("GREAT WORK! EVERYTHING PASSED!"); 
        System.out.println(systemCall("Nice")); 
    } 

} 

3 个答案:

答案 0 :(得分:0)

类似的东西:

void setAddress(Address a) {
    this.address = a;
}

目前,您没有将任何值传递到setAddress,因此setAddress无法做任何事情。

答案 1 :(得分:0)

public class Problem3_1{

String name;
int age;
int height;
int weight;    
static Address address;

public Problem3_1(String name, int age, int height,int weight) {
    this.name = name;
    this.age = age;
    this.height = height; //cm
    this.weight = weight; //kg
}

void setAddress(int number,String street) {
    address = new Address(number,street);   
}

public static void main(String [] args){
     Problem3_1 problem=new Problem3_1("Tom",28,180,78);
     problem.setAddress(4460,"new Delhi");
     System.out.println(address.getAddressNumber() +" number of street : "+address.getAddressString());

   }


 public static class Address{ 

    private int number; 
    private String street; 

    public Address(int number, String street){ 
        this.number = number; 
        this.street = street ; 
     }
    int getAddressNumber(){
      return number;
    }
    String getAddressString(){
        return street;
    }

  } 

}

答案 2 :(得分:0)

首先,将Address类作为参数传递。

void setAddress(Address s)

由于静态嵌套类中的Address,但您已在Problem3_1类文件中将Address声明为String。因此,您需要手动连接值,如

void setAddress(Address s) {
        //address = s;

        address = s.number + " , " + s.street;
    }

如果您正确地创建和传递值,这应该可行。

示例:

    Problem3_1 a = new Problem3_1("name1",20,5);
    Problem3_1.Address a_address= new Problem3_1.Address(20,"1st street");
    a.setAddress(a_address);

    System.out.println(a.address);

如果你在外部类和内部类中重写了toString()方法,那么应该打印你想要的输出。

<强>更新

http://ideone.com/rS20uZ - 点击此处查看完整代码和输出。