A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘建华 中级黑马   /  2015-1-9 16:51  /  1373 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.itheima;
/**
声明类Student,包含3个成员变量:name、age、score,
要求可以通过 new Student("张三", 22, 95) 的方式创建对象,
并可以通过set和get方法访问成员变量
@author Administrator
*/
public class Test7
{
        //建立主函数
        public static void main(String[] args)
        {
                Student sd=new Student("张三", 22, 95);
                sd.setAge(18);
                sd.show();
        }
}
class Student  //定义一个学生的类
{
        private String name;
        private int age;
        private int score;
        //set 学生的变量
        public void setName(String newname)
        {
                name=newname;
        }
        public void setAge(int newage)
        {
                if(newage>=0)
                        age=newage;
                else
                        System.out.println("年龄错误");
        }
        public void setScore(int newscore)
        {
                if(newscore>=0&newscore<=150)
                        score=newscore;
                else
                        System.out.println("分数错误");
                       
        }
        //get 学生的变量
        public String getName()
        {
                return name;
        }
        public int getAge()
        {
                return age;
        }
        public int getScore()
        {
                return score;
        }
        //学生初始数据
        Student(String name,int age,int score)
        {
        this.name = name;
        this.age = age;
        this.score = score;
        }
        //打印学生的信息
        public void show()
        {
        System.out.println("Name: " + name + ", Age: "+ age  + ", Score:" + score);
    }
}


2 个回复

倒序浏览
最好是思路,分析也写上去
回复 使用道具 举报
这样会被扣分,最起码要有思路,我觉得这是必须的,你没思路,你题怎么做的?背出来的???:lol
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马