黑马程序员技术交流社区
标题:
请教一个问题
[打印本页]
作者:
分解式
时间:
2014-2-25 08:04
标题:
请教一个问题
声明类Student,包含3个成员变量:name、age、score,要求可以通过 new Student("张三", 22, 95) 的方式创建对象,并可以通过set和get方法访问成员变量
大黑马面试题。有些不懂的地方,求编程
作者:
kkopopo
时间:
2014-2-25 08:12
class Stu{
private String name;
private int age;
private int score;
Stu(String name, int age, int score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
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;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
复制代码
作者:
syw02014
时间:
2014-2-25 09:01
给你写一个完整的代码吧,希望能帮到你:
public class Student{
private String name;
private Integer age;
private Integer score;
public Student(String name, Integer age, Integer score)
{
super();
this.name = name;
this.age = age;
this.score = score;
}
public String getName(){return name;}
public void setName(String name){this.name = name;}
public Integer getAge(){return age;}
public void setAge(Integer age){this.age = age;}
public Integer getScore(){return score;}
public void setScore(Integer score) {this.score = score;}
public static void main(String[] args)
{
//1、通过“通过 new Student("张三", 22, 95) 的方式创建对象”
Student student = new Student("张三",22,95);
System.out.println(student.getName());
System.out.println(student.getAge());
System.out.println(student.getScore());
//2、通过“可以通过set和get方法访问成员变量” 设置变量并且访问变量
student.setName("李四");
student.setAge(23);
student.setScore(96);
//3、通过get访问
System.out.println(student.getName());
System.out.println(student.getAge());
System.out.println(student.getScore());
}
}
复制代码
作者:
王飞163
时间:
2014-6-25 00:02
点个赞!!!!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2