Java面向对象(定义一个Student类,属性有...方法有....创建对象....)

定义一个Student类,

​ 属性有学号,姓名,年龄,性别,语文成绩,数学成绩,英语成绩,

​ 方法有:学习,考试(打印输出学习,考试即可))

​ 创建两个学生对象,并给属性赋值和调用方法

public class Test1Student {
     
    //主方法
    public static void main(String[] args){
     
        Test1Student stu1 = new Test1Student();
        Test1Student stu2 = new Test1Student();

        stu1.num=110;
        stu1.name="张三";
        stu1.age=18;
        stu1.sex = '男';
        stu1.chinese = 96;
        stu1.math = 97;
        stu1.english = 99;
        stu1.learn();
        stu1.exam();

        stu2.num=111;
        stu2.name="燕妮";
        stu2.age=17;
        stu2.sex = '女';
        stu2.chinese = 94;
        stu2.math = 93;
        stu2.english = 92;
        stu2.learn();
        stu2.exam();

    }

    //属性
    int num;
    String name;
    int age;
    char sex;
    int chinese;
    int math;
    int english;

    //方法--->学习
    public void learn(){
     
        System.out.println("学号:"+num+"\n"+"姓名:"+name+"\n"
                +"性别:"+sex+"\n"+"语数英成绩为"+chinese+"\t"+math+"\t"+english+"\n"+"好好学习!");
    }

    //方法--->考试
    public void exam(){
     
        System.out.println("认真考试!");
    }
}

你可能感兴趣的:(Java代码,java,面向对象编程,类)