class Person {
int age;
public Person(int age) {
______________//让局部变量的age给成员变量的age赋值
}
public int getAge() {
return this.age;
}
}
在横线处填入正确的代码,可以让局部变量的age给成员变量的age赋值
int age;
public Person(int age) {
______________//让局部变量的age给成员变量的age赋值
}
public int getAge() {
return this.age;
}
}
在横线处填入正确的代码,可以让局部变量的age给成员变量的age赋值
举一反三
- 分析下面的程序,输出的结果是? public class Test { public static void main(String[] args) { final Person p = new Person("张三", 23); p.setName("李四"); p.setAge(24); System.out.println(p); } } class Person { private String name; private int age; public Person() { super(); } public Person(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "我的姓名是:" + name + ",我的年龄是:" + age ; } }
- 下面哪个程序变量age的定义是正确的() A: public class Employee{ public void show(){ System.out.println(age); } public int age; } B: public class Employee{ public void show(){ System.out.println(age); int age = 20; } } C: public class Employee{ public void show(){ System.out.println(age); } }
- 在C#中,下列结构或者类定义正确的是(选一项) A: public struct Person{ string name; int age; public void ShowName(){ Console.WriteLine(name); }} B: public struct Person{ string name; int age; public Person(){ Conssole.WriteLine(name); }} C: public class Person{ string name; int age; public Person(){ Conssole.WriteLine(name); }} D: public class Person{ string name; int age; public Person(string name){ Conssole.WriteLine(name); }}
- 请阅读下面的代码:class Example{int x;static int y;void fac(){int age=30;System.out.println("我的年龄是"+age);}} A: x和y都是成员变量 B: fac是成员方法 C: age是局部变量 D: 以上都不对
- 对于Animal类,如果给成员变量age赋值