黑马程序员技术交流社区

标题: 好算法 [打印本页]

作者: NGQ    时间: 2017-4-3 20:47
标题: 好算法
2. 定义一个类Student,属性为学号、姓名和成绩;方法为增加记录SetRecord和得到记录GetRecord。SetRecord给出学号、姓名和成绩的赋值,GetRecord通过学号得到考生的成绩。
public class Student {

        /**
         * @param args
         */
        private int ID;
        private String name;
        private float score;
       
        public void SetRecord(int ID,String name,float score){               
                this.ID=ID;
                this.name=name;
                this.score=score;
        }
        public float getRecord(int ID){
                if(ID==this.ID)
                return this.score;
                else
                return -1;
                }
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Student s=new Student();
                s.SetRecord(0,"alex",100);
                float Sco=s.getRecord(0);
                System.out.print(Sco);
        }
}














给出上题中设计类的构造函数,要求初始化一条记录(学号、姓名、成绩)。
public class Student {

        /**
         * @param args
         */
        private int ID;
        private String name;
        private float score;
       
        Student(int ID,String name,float score){
                this.ID=0;
                this.name="666";
                this.score=65;
        }
        public void SetRecord(int ID,String name,float score){               
                this.ID=ID;
                this.name=name;
                this.score=score;
        }
        public float getRecord(int ID){
                if(ID==this.ID)
                return this.score;
                else
                return -1;
                }

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                Student s=new Student(0,"sdfs",12);
                //s.SetRecord(0,"alex",100);
                float Sco=s.getRecord(0);
                System.out.print(Sco);
        }
}













欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2