I need help creating an instance variable and cons

2020-04-12 12:50发布

Sorry, I know this answer is obvious but I suppose I'm just slow. Can anyone give me clear definitions of an instance variable and a constructor?

Instance variables:
Your class will need an instance variable named numbers that is a one-dimensional array. The size of the array will be determined by the constructor. You may, or may not, find it useful to have other instance variables.

Constructors:
You will write two constructors. One constructor will be a default constructor that has no arguments. It will create an array that holds 10 int's and will set each element of the array to be 42.

The second constructor will take one argument, which will be an array of int. This constructor will create an instance array of the same size as the argument and then copy the integers from the argument to the instance array.


I have no idea how to even start on that.

3条回答
The star\"
2楼-- · 2020-04-12 13:12
public class MyClass{
    int numbers[];
    MyClass(){
        numbers = new int[10];
        for (int i = 0; i < numbers.length; i++) {
                numbers[i] = 42;
        }
    }
    MyClass(int[] array){
        numbers = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            numbers[i] = array[i];
        }
    }
}
查看更多
放我归山
3楼-- · 2020-04-12 13:27

Instance members are simply variables that belong to a class object, as long as it is not a static variable! A static variable, strictly speaking belongs to the class not an object, Constructors is just a special method you call to create and initialize an object. It is also the name of your class.

So what you want is

class WhateverYourClassIs
{
   int[] members;
   public WhateverYourClassIs()//constructor. 
   {
    members = new int[10] ;
    }
   public WhateverYourClassIs(int n) //overloaded constructor.
   {
     members = new int[n] ;
   }
}

So as you can see in the above example, constructors like methods, can be overloaded. By overloaded it means the signature is different. One constructor takes no argument, another takes a single argument that is an int.

查看更多
Deceive 欺骗
4楼-- · 2020-04-12 13:28

A constructor is the part of a class which generates instances of that class. It's named the same thing as the class and has no return type. For example:

public class Foo{
  public Foo(){
      System.out.println("Hi from the constructor!!");
   }
}

An instance field is a variable local to each instance of the class. You can either have public, protected, or private instance fields. A private instance field is "hidden" from the outside world and only the instance itself can access it. A public one is accessed using the . operator. For example:

public class Foo{ public int x; private int y; }

Foo foo = new Foo(); //Thats a call to the constructor Foo()
foo.x = 1;
foo.y; //Error can't access private variables from outside the class

For your case, you'll want

class Name{
   int[] numbers;
   public Name(){
      numbers = new int[10];
   }
   public Name(int n){
      numbers = new int[n];
   }
}

Here you overload the constructors (just like for methods) and create an array, which is a list of, in this case, ints of a fixed length.

查看更多
登录 后发表回答