以下代码运行结果class Af int m=0; public void setM(int n) { m=n; n++ ; } public int getM () { return m; } } public class Test void mod (A ra) { ra.m=20; } public static void main (String args []) { Test ts=new Test () ; A a=new A(); int t=10; a. setM (t); System.out.print (a.m+", "+t); ts.mod (a); System.out.printin (", "+a.m); } }
时间: 2024-01-15 20:03:39 浏览: 25
运行结果为:
```
10, 10, 20
```
解析:
- `A` 类中有一个实例变量 `m`,并且有两个方法,`setM` 方法用于给 `m` 赋值,`getM` 方法用于获取 `m` 的值。
- `Test` 类中有一个 `mod` 方法,用于修改传入的 `A` 类对象的 `m` 值为 20。
- 在 `main` 方法中,创建了一个 `Test` 类对象 `ts` 和一个 `A` 类对象 `a`,并且将变量 `t` 赋值为 10。
- 调用 `a.setM(t)` 方法,将 `a` 对象的 `m` 值设置为 `t` 的值,即为 10。
- 输出 `a.m`, `t` 的值,结果为 `10, 10`。
- 调用 `ts.mod(a)` 方法,将 `a` 对象的 `m` 值设置为 20。
- 输出 `a.m` 的值,结果为 `20`。
相关问题
class E{ int x; public void f(int n){ int m; int t=x+n; int y=x+m; m=10; y=x+m; } }
There is a syntax error in the code. The variable "m" is being used before it is declared. The corrected code is as follows:
class E{
int x;
public void f(int n){
int m = 0; // declare and initialize m
int t = x + n;
int y = x + m;
m = 10;
y = x + m;
}
}
public+class+Test+{ public+static+void+main(String[]+args)+{ int+a+=+0; int+b+=+0; for+(int+i+=
以下是填充代码中的空白的答案:
```java
public class TestOR {
public static void main(String[] args) {
int a=111111;
int b=222222;
//以下代码将a和b值互换
int temp = a;
a = b;
b = temp;
System.out.println("a="+a+" b="+b);
}
}
```
以下是下列程序的输出结果的答案:
```java
public class Test {
public static void main(String[] args) {
int i=0;
for(i=0;i<4;i++) {
if(i==3) break;
System.out.print(i);
}
System.out.println(i);
}
}
```