Java 区分变量名
在我的类中,变量名、形式参数名和局部变量名都是相同的。Java 区分变量名,java,Java,在我的类中,变量名、形式参数名和局部变量名都是相同的。 在方法体中,我想将参数分配给实例变量。 如何区分变量 import java.util.Scanner; class Setts { static int a=50; void m1(int a) { int a=100; this.a=a;//here am set the int a value give the solution; } void disp()
在方法体中,我想将参数分配给实例变量。
如何区分变量
import java.util.Scanner;
class Setts
{
static int a=50;
void m1(int a)
{
int a=100;
this.a=a;//here am set the int a value give the solution;
}
void disp()
{
System.out.println(Setts.a);
//System.out.println(ts.a);
}
}
class SetDemo
{
public static void main(String[] args)
{
System.out.println("Hello World!");
Setts ts=new Setts();
Scanner s=new Scanner(System.in);
System.out.println("entet the int value");
int x=s.nextInt();
ts.m1(x);
ts.disp();
//System.out.println(ts.a);
}
}
简言之,不能使用隐藏参数的局部变量。编译器不允许这样做 e、 g 所以,如果您有字段和参数名,您可以只使用参数名 你能得到的是
class A {
int x;
void method(int x) {
int y = x; // the parameter
int z = this.x; // the field above.
请发布一些代码,以便我们更好地理解你的意思。我猜您将不得不使用
this.myVariable=myVariable
,但由于我不完全理解您的问题,我只能猜测。
class A {
int x;
void method(int x) {
int y = x; // the parameter
int z = this.x; // the field above.