简体   繁体   English

Java'this'关键字

[英]Java 'this' keyword

I'm just beginning in programming and I'd like to make exercise from a book, but I can't. 我刚开始编程,我想从书中练习,但我不能。 That's my problem: 那是我的问题:

public class increment {
    int increment() {
        return this + 1; // aka this++
    }

    public static void main(String[] args) {
        int a = 0;
        System.out.println(a.increment());
    }
}

As you for sure guessed already, that it doesn't works, I want to ask you how to get outputed integer a incremented by one, but using keyword 'this'. 正如你肯定已经猜到的那样,它不起作用,我想问你如何将输出的整数增加1,但使用关键字'this'。

Regards and sorry for stupid questions. 对愚蠢的问题表示遗憾和抱歉。

It is strange to name a class like a method. 将类命名为方法很奇怪。 I guess you wanted this: 我想你想要这个:

public class Counter {

int val;

 public Counter (int start) {
   val = start;
 }
 public void increment() {
    val ++;
 }
 public String toString () {
   return Integer.toString (val);
 }

 public static void main(String[] args) {
    Counter counter = new Counter (0);
    counter.increment ();
    System.out.println(counter.toString ());
 }
}

this is an object (the current object). this是一个对象(当前对象)。 You cannot "increment" it. 你不能“增加”它。

A way to do it is: 一种方法是:

public class Increment {
    int a = 0;
    int increment() {
        return a + 1; 
        // or: return this.a + 1;
        // or: a++; return a; if you want a to be incremented from now on
    }

    public static void main(String[] args) {
        Increment inc = new Increment();
        System.out.println(inc.increment());
    }
}

The this keyword in Java refers to the current scope's object instance. Java中的this关键字指的是当前作用域的对象实例。 I don't think it's what you're looking for in this case. 在这种情况下,我认为这不是你想要的。

In your example, a isn't an object of the class increment , it is a primitive int . 在您的示例中, a不是类increment的对象,它是原始int In order to use the .increment() function you defined, it would have to be an object of type increment . 为了使用你定义的.increment()函数,它必须是一个increment类型的对象。

One option that may be what you're looking for would be the following. 您可能正在寻找的一个选项如下。

public class Increment { //Java likes capitalized class names
    private int myInt;    

    public Increment(int a) { //constructor
        myInt = a;
    }

    public int increment() {
        return ++myInt;
    }    

    public static void main(String[] args) {
        Increment a = new Increment(0);
        System.out.println(a.increment());
    }

}

In this example, we make a new class of type increment , which internally contains an integer. 在这个例子中,我们创建了一个类型increment的新类,它在内部包含一个整数。 Its increment method increments that internal integer, and then returns the number. 它的增量方法递增内部整数,然后返回数字。

You cannot increment a class like this. 你不能像这样增加一个类。 You have to use a member variable that you can increment. 您必须使用可以递增的成员变量。

public class Test {
    private int var;

    public Test(int i) {
        this.var = i;
    }

    int increment() {
       this.var++;
    }
}

public static void main(String[] args) {
    Test t = new Test(0);
    System.out.println(t.increment());

}

you are using operator + for your current object ( this ). 您正在使用operator +作为当前对象( this )。 Operator overloading is not supported in java. java中不支持运算符重载。

Something like this will work: 这样的东西会起作用:

class MyInteger {
    private int internal;

    public MyInteger( int value ){
        this.internal = value;
    }

    public int incerment(){
        return ++this.internal;
    }
}

public class Increment {

    public static void main(String[] args) {
        MyInteger a = new MyInteger(0);
        System.out.println(a.increment());
    }
}

You see, you can only implement methods for your own classes, not for existing classes, or for primitives like int . 您可以看到,您只能为自己的类实现方法,不能为现有类实现方法,也不能为int这样的原语实现方法。

i don't think you can use this to return the value, except if you're making a new class like this: 我不认为你可以使用它来返回值,除非你正在创建一个像这样的新类:

class Increment1
{
    private int a;
    public int increment2(int a)
    {
        this.a=a;
        return this.a + 1;
    }
}

public class Increment
{

    static Increment1 b = new Increment1();

    public static void main(String[] args)
    {
        int a = 0;
        System.out.println(b.increment2(a));
    }
}

This refers to the current instance of the class, not a particular member. 这指的是类的当前实例,而不是特定成员。

You want to increment a property (I'm guessing of type long or int), and not the instance of your increment class (should be Increment, by the way). 你想增加一个属性(我猜的是long或int类型),而不是你的增量类的实例(顺便说一下,应该是Increment)。

Something like this would work: 像这样的东西会起作用:

public class increment {

private int innerValue = 0;

int increment() {
    innerValue+=1
    return innerValue; // aka this++
}

public static void main(String[] args) {
    increment a = new increment()
    System.out.println(a.increment());
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM