Python self 和 Java this 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21694901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Python self and Java this
提问by Bayko
I had done a bit of Python long back. I am however moving over to Java now. I wanted to know if there were any differences between the Python "self" method and Java "this".
很久以前我做过一些 Python。然而,我现在正在转向 Java。我想知道 Python “self” 方法和 Java “this” 之间是否有任何区别。
I know that "self" is not a keyword while "this" is. And that is pretty much what I could figure out. Am I missing anything else?
我知道“自我”不是关键字,而“这个”是。这几乎是我能弄清楚的。我还缺什么吗?
采纳答案by 0__1
About self
in Python (here is the source: Python self explanation):
关于self
在 Python 中(这里是来源:Python 自我解释):
The reason you need to use self. is because Python does not use the @ syntax to refer to instance attributes. Python decided to do methods in a way that makes the instance to which the method belongs be passed automatically, but not received automatically: the first parameter of methods is the instance the method is called on. That makes methods entirely the same as functions, and leaves the actual name to use up to you (although selfis the convention, and people will generally frown at you when you use something else.) selfis not special to the code, it's just another object.
您需要使用self的原因。是因为 Python 不使用 @ 语法来引用实例属性。Python 决定以一种方式执行方法,使方法所属的实例自动传递,但不会自动接收:方法的第一个参数是调用方法的实例。这使得方法与函数完全相同,并将实际名称留给你使用(尽管self是约定俗成的,当你使用其他东西时,人们通常会对你皱眉。)self对代码来说并不特殊,它只是另一个对象。
Python could have done something else to distinguish normal names from attributes -- special syntax like Ruby has, or requiring declarations like C++ and Java do, or perhaps something yet more different -- but it didn't. Python's all for making things explicit, making it obvious what's what, and although it doesn't do it entirely everywhere, it does do it for instance attributes. That's why assigning to an instance attribute needs to know what instance to assign to, and that's why it needs self..
Python 可以做一些其他事情来区分普通名称和属性——像 Ruby 那样的特殊语法,或者像 C++ 和 Java 那样需要声明,或者可能是更不同的东西——但它没有。Python 就是为了让事情变得明确,让什么是什么变得显而易见,虽然它没有在所有地方都这样做,但它确实在实例属性方面做到了这一点。这就是为什么分配给实例属性需要知道要分配给哪个实例,这就是为什么它需要self..
About this
in Java being explained by Oracle (here is the source: Java this explanation):
关于this
在 Java 中被 Oracle 解释(这里是来源:Java 这个解释):
Within an instance method or a constructor, thisis a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this. The most common reason for using the thiskeyword is because a field is shadowed by a method or constructor parameter.
在实例方法或构造函数中,this是对当前对象的引用——正在调用其方法或构造函数的对象。您可以使用this从实例方法或构造函数中引用当前对象的任何成员。使用this关键字的最常见原因是因为字段被方法或构造函数参数遮蔽。
回答by Rohit Jain
First of all, let me correct you - self
is not a method. Moving further:
首先,让我纠正你 -self
不是一种方法。更进一步:
Technically both self
and this
are used for the same thing. They are used to access the variable associated with the current instance. Only difference is, you have to include self
explicitly as first parameter to an instance method in Python, whereas this is not the case with Java. Moreover, the name self
can be anything. It's not a keyword, as you already know. you can even change it to this
, and it will work fine. But people like to use self
, as it has now become a bit of a convention.
从技术上讲两者self
并this
用于同样的事情。它们用于访问与当前实例关联的变量。唯一的区别是,您必须self
在 Python 中显式包含作为实例方法的第一个参数,而 Java 则不是这种情况。此外,名称self
可以是任何东西。正如您已经知道的那样,它不是关键字。您甚至可以将其更改为this
,它会正常工作。但是人们喜欢使用self
,因为它现在已经成为一种惯例。
Here's a simple instance method accessing an instance variable in both Python and Java:
这是访问 Python 和 Java 中的实例变量的简单实例方法:
Python:
Python:
class Circle(object):
def __init__(self, radius):
# declare and initialize an instance variable
self.radius = radius
# Create object. Notice how you are passing only a single argument.
# The object reference is implicitly bound to `self` parameter of `__init__` method
circle1 = Circle(5);
Java:
爪哇:
class Circle {
private int radius;
public Circle(int radius) {
this.radius = radius;
}
}
Circle circle1 = new Circle(5);
See also:
也可以看看:
回答by Fruit
Be careful super can keep its own version of this.i in Java, but self.i always refer to the child in Python.
小心super 可以在 Java 中保留它自己的 this.i 版本,但是self.i 在 Python 中总是指 child。
Main.java:
主.java:
class Parent {
int i;
Parent() {
this.i = 5;
}
void doStuff() {
System.out.println(this.i);
}
}
class Child extends Parent {
int i;
Child() {
this.i = 7;
}
}
class Main {
public static void main(String[] args) {
Child m = new Child();
System.out.println(m.i); //print 7
m.doStuff(); //print 5
}
}
Main.py:
主要.py:
class Parent(object):
i = 5;
def __init__(self):
self.i = 5
def doStuff(self):
print(self.i)
class Child(Parent, object):
def __init__(self):
super(Child, self).__init__()
self.i = 7
class Main():
def main(self):
m = Child()
print(m.i) #print 7
m.doStuff() #print 7
m = Main()
m.main()
Output:
输出:
$ java Main
7
5
$ python Main.py
7
7
[Update]
[更新]
The reason is because Java's int i
declaration in Child
class makes the i
become class scope variable, while no such variable shadowing in Python subclassing. If you remove int i
in Child
class of Java, it will print 7 and 7 too.
原因是因为 Javaint i
在Child
class 中的声明使得i
成为类作用域变量,而在 Python 子类化中没有这样的变量阴影。如果int i
在Child
Java 类中删除,它也会打印 7 和 7。
回答by Jason Cheng
From my perspective, the most obvious difference is that in java class, in the constructor, you need to specify the field
在我看来,最明显的区别是在java类中,在构造函数中,需要指定字段
this.radius = radius
While in the python code, you don't have to do so, it's implicit.
在 python 代码中,您不必这样做,它是隐式的。