构造方法

package OOPDemo2;

public class Student
{
   //属性:姓名 成绩
    String name;
    int score;

    //构造方法
    //(1)该方法没有返回值,(2)该方法名字必须和类名一样
    //方法:显示个人成绩的信息
    //每个类都有默认无参构造方法
    //但是一旦手工添加了带参构造方法,那么系统默认的无参构造方法就被覆盖掉了,你想使用,必须显示的写出来

    public Student()
    {

    }

    public Student(String name, int score)
    {
        //this是什么?this是当前对象
        this.name = name;
        this.score = score;
    }


    public void showInfo()
    {
        System.out.println(name + "的成绩是:" + score);
    }

}

package OOPDemo2;

public class ModifyScore
{
    public void modifyScore(Student[] stus)
    {
        for (int i = 0; i < stus.length ; i++)
        {
            if(stus[i].score < 60)
            {
                stus[i].score += 2;
            }
        }
    }
    //输出学员信息
    public void showInfo(Student[] stus)
    {
        for (Student stu : stus)
        {
            stu.showInfo();
        }
    }

    public static void main(String[] args)
    {
        ModifyScore ms = new ModifyScore();
        Student[] stus = new Student[3];

//        Student s1 = new Student();
//        s1.name = "张三";
//        s1.score = 40;
        Student s1 = new Student("张三",40);

//        Student s2 = new Student();
//        s2.name = "李四";
//        s2.score = 90;
        Student s2 = new Student("李四",90);

//        Student s3 = new Student();
//        s3.name = "王五";
//        s3.score = 50;
        Student s3 = new Student("王五",50);

        stus[0] = s1;
        stus[1] = s2;
        stus[2] = s3;
        ms.modifyScore(stus);
        ms.showInfo(stus);

        Student s4 = new Student();
        s4.name = "赵六";

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值