Possible Duplicate:
this and super in java
我给发展带来新的影响。 我仍然无法理解的一点是,这一词与超级关键词之间的区别。 如果有答案,将非常感谢。 感谢。
Possible Duplicate:
this and super in java
我给发展带来新的影响。 我仍然无法理解的一点是,这一词与超级关键词之间的区别。 如果有答案,将非常感谢。 感谢。
在 Java,关键词(this
)是指某一类别当前的物体,例如:
class Point {
public int x = 0;
public int y = 0;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + "," + y;
}
}
class Point3D extends Point {
public int z = 0;
public Point(int x, int y, int z) {
super(x,y);
this.z = z;
}
public String toString() {
return super.toString() + "," + z;
}
}
在该类别中定义的<代码>x>系指该类别中定义的<代码>x,其中<代码>x为向施工人传递的参数。 这里使用的是<代码>ts,以解决名称的模糊性。 <代码>y也是如此。
在<条码>中 这种方法toString(
) 采用超级类别(Point
)的回报价值来产生自己的回报价值。
http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html>rel=“noreferer”>this
在一种实例方法或构造中,这是指当前物体——其方法或构造被称作物体。 您可以通过采用一种审查方法或一种构造来指目前物体的任何成员。
http://docs.oracle.com/javase/tutorial/java/IandI/super.html>rel=“noreferer”>super/code>
如果你的方法优于其超等级的方法之一,你可以通过使用关键词超级来援引压倒性的方法。 你们也可以使用超级手段来指隐藏的领域(尽管藏匿场被阻止)。
<代码>super系指现值班的基类。 <代码>ts系指目前的班级。
因此,如果<代码>Parent > 扩展Child
,且你创建new Child()
系指以<代码>new/code>创建的实际。,Parent
类别(如super(
>,在构造者中,
<代码>Super系指等级延伸的超级等级。 http://www.un.org/Depts/DGACM/index_french.htm
These concepts can be confusing for new developers, they will be more clear when you learn about extending classes (inheritance). Sometimes when you refer to a variable or method, you might be being ambiguous for example if you repeated a class variable name in a method, the compiler won t know which variable you are referring to, so you can use this to specify you are referring to the current class s variable (not the local variable). The following would be ambiguous (and WRONG):
class Bike
{
int speed = 10;
public setSpeed(int speed)
{
speed = speed;
}
}
The compiler would have no idea what you intended, and will probably insult you with a cryptic (for a new developer) error message. Using this
in the following way tells the compiler "I am referring to the class level variable, NOT the method level variable"
class Bike
{
int speed = 10;
//Constructors and shiz
public void setSpeed(int speed)
{
this.speed = speed;
}
}
(Although in practice you shouldn t duplicate variable names in this way!)
So to summarise, this
tells the compiler that you re referring to the CURRENT class. Further ambiguity can arise when you extend classes (inherit functionality for a parent or super
class), because the option of overriding the parent method arrises.
class Bike
{
public Bike()
{}
public void printSpeed()
{
System.out.println("This generic bike can go 10 m/s!!");
}
}
现在,如果我们通过引入一种更具体的双管子来扩大双管,我们可能要放弃印刷方法,以使我们的光辉新双ke的速度。
class MountainBike extends Bike
{
public MountainBike() {}
public void printSpeed()
{
System.out.println("WHOAAAH!! MOUNTAIN BIKE GOES 100 m/s!!");
}
public void printGenericSpeed()
{
super.printSpeed();
}
}
super.printSpeed()
> 系指汇编者从母类别中操作这种方法,因此,请打电话到super.printSpeed(<>
>将实际称作Bike等舱中的Speed(
)。 The following Code:
public static void main(String args[])
{
MountainBike mb = new MountainBike();
mb.printSpeed();
mb.printGenericSpeed();
}
印刷
WHOAAAH!! MOUNTAIN BIKE GOES 100 m/s!!
This bike can go 10 m/s!!
请注意,如果我们不凌驾于<代码>(Speed)()方法之上,请打电话到Bike等级的<编码>(Speed()。
public static void main(String args[])
{
MountainBike mb = new MountainBike();
mb.printSpeed();
}
印刷
This bike can go 10 m/s!!
因此,最后,我们使用<条码>这一条条码>来指我们目前采用的类别,并用<条码><<>>>指我们所复的类别中的母。
这是指其类别方法中的当前物体。 通过本关键词指目前物体的任何成员。
倒数:当你的班级通过延长关键词加以扩展时,是衍生的班子,而且你可以通过使用关键词超级来援引压倒性的方法。 你们也可以使用超级手段来指保护区。
this keyword refers to the current instance at that point in time. super keyword refers to the parent/super class of the current class.
EX:
class Test
{
Test()
{
}
Test(int i)
{
System.out.println("i=" + i);
}
}
class Sample extends Test
{
int i;
void Sample(int i) //constructor
{
this.i=i; // referring class version of the i using this
super(i); // passing parameter to Super/Parent class constructor.
}
}
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...