黑马程序员技术交流社区
标题:
写到这没有编程思路了啊大侠帮帮忙
[打印本页]
作者:
张龙跃
时间:
2013-4-26 21:43
标题:
写到这没有编程思路了啊大侠帮帮忙
本帖最后由 张龙跃 于 2013-4-26 21:59 编辑
定义一个学生类, 需要有姓名, 年龄, 考试成绩三个成员属性. 属性(成员变量)需要私有并提供get, set方法, 可以通过构造函数进行初始化.
class xuesheng
{
private String name;
private int age;
private double chengji;
}
class xuesheng2
{
public static void main(String[] args)
{
}
}
谁能帮帮我啊 写上注释啊
好人一生平安
作者:
殇_心。
时间:
2013-4-26 21:52
测试代码如下:
class Student
{
private String name;
private int age;
private double cj;
Student( String name, int age, double cj )
{
this.name = name;
this.age = age;
this.cj = cj;
}
public void setName()//设置名字
{
this.name = name;
}
public void setAge()//设置年纪
{
this.age = age;
}
public void cj()//设置成绩
{
this.cj = cj;
}
public String getName()//获取name
{
return name;
}
public int getAge()//获取age
{
return age;
}
public double getCj()//获取成绩
{
return cj;
}
}
作者:
刘胜寒
时间:
2013-4-26 21:53
本帖最后由 刘胜寒 于 2013-4-26 21:55 编辑
<blockquote>class xuesheng
复制代码
作者:
932759732
时间:
2013-4-26 21:53
本帖最后由 杨玄文 于 2013-4-26 21:55 编辑
public class Student {
private String name;
private int age;
private double score;
//定义私有变量接收传入的参数
Student(String name,int age,double score){
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 double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
复制代码
希望能够帮助你。不过你的基础还有待加强咯。
如果要对学生进行排序的话还需要复写equals和hashCode方法
作者:
337091921
时间:
2013-4-26 21:59
class Student {
private String name;
private int age;
public void setName(String name) {
this.name=name;
}
public String getName() {
return name;
}
public void setAge(int a) {
if (a < 0 || a > 200){
System.out.println(a + "岁 ?!哥们......你是地球人么!!!");
}else
age = a;
}
public int getAge() {
return age;
}
}
class Test{
public static void main(String[] args) {
Student s = new Student();
s.setName("小明");
System.out.println(s.getName());
s.setAge(33);
System.out.println(s.getAge());
s.setAge(998);
}
}
作者:
黑马-雷钊
时间:
2013-4-26 22:01
编程思想不能急的,慢慢来就可以了。初学都这样
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2