类Students和对象

来源:互联网 发布:head first java 编辑:程序博客网 时间:2024/06/04 00:45
package yg;import java.util.Scanner;public class Students {//创建students    String name;//数据类型的定义    String sex;//数据类型的定义    int age;//数据类型的定义    int xuehao;    float score;    String yuanxi;    public Students() {};//定义无参构造方法    public Students(String n,String s,int a,int f,float b,String h)//定义有参构造方法,参数如下    {        name=n;        sex=s;        age=a;        xuehao=f;        score=b;        yuanxi=h;    }    void getInfo(String n,String s,int a,int f,float b,String h)//定义方法getInfo(),获取学生基本信息    {        name=n;        sex=s;        age=a;        xuehao=f;        score=b;        yuanxi=h;    }    void showInfo()//定义方法showInfo(),显示学生信息    {        System.out.print("姓名:"+name+"\t");        System.out.print("性别:"+sex+"\t");        System.out.print("年龄:"+age+"\t");        System.out.println("学号:"+xuehao+"\t");        System.out.println("成绩:"+score+"\t");        System.out.println("院系:"+yuanxi+"\n");    }    public static void main(String[] args) {        // TODO Auto-generated method stub        Students stu1;//定义属于类Students的对象stu1        stu1=new Students();//实例化对象stu1        System.out.println("第一个学生的信息");        stu1.getInfo("穆迎","男",20,175001,99,"计算机3G/4G");//调用方法getInfo(),获取stu1学生的信息        stu1.showInfo();//调用方法showInfo(),显示出stu1学生信息        System.out.println("第三个学生的信息");        Students stu3=new Students("校龄","女",19,175003, 99,"计算机软件");//定义和实例化对象stu3,并初始化stu2的学生信息        stu3.showInfo();//调用方法showInfo(),显示出stu3学生信息        Students stu2=new Students();        System.out.println("第二个学生的信息");        stu2.name="源源";        stu2.sex="女";        stu2.age=19;        stu2.xuehao=175002;        stu2.score=98;        stu2.yuanxi="计算机科学";        System.out.println("姓名:"+stu2.name);        System.out.println("性别:"+stu2.sex);        System.out.println("年龄:"+stu2.age);        System.out.println("学号:"+stu2.xuehao);        System.out.println("成绩:"+stu2.score);        System.out.println("院系:"+stu2.yuanxi);    }}运行结果:![这里写图片描述](http://img.blog.csdn.net/20171010212247856?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFubHV5YW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
阅读全文
0 0
原创粉丝点击