[School Experiment] Definition and Use of Student Class (Title)

Goal: Define student classes and use

Object-oriented 3 steps:
   1. Define the class
   2. Create the object
   3. Use the object

Student:
   attributes: name, age
   behavior: eating, doing homework

code show as below:

// 1.定义学生类
public class Student {
    
    
    // 属性: 姓名,年龄
    String name;
    int age;

    // 行为: 吃饭,做作业
    public void eat() {
    
    
        System.out.println("吃饭");
    }

    public void doHomeWork() {
    
    
        System.out.println("做作业");
    }
}
public static void main(String[] args) {
    
    
        // 2.创建对象: 类名 对象名 = new 类名();
        Student s = new Student();

        // 3.使用对象
        // 使用成员变量
        System.out.println(s.name + "--" + s.age); // null--0
        s.name = "二丫";
        s.age = 18;
        System.out.println(s.name + "--" + s.age); // 二丫--18

        // 使用成员方法: 对象名.方法名();
        s.eat();
        s.doHomeWork();
    }

The result of the operation is:

Insert picture description here

Guess you like

Origin blog.csdn.net/maikotom/article/details/108819272