目录
类的创建
/**
* @Time: 2021/5/22
* @Author: 大海
**/
public class Student {
/*注意事项:
1.成员变量是直接定义在类当中的,在方法外边。
2.成员方法不要写static关键字。
*/
// 成员变量
String name;
int age;
// 成员方法
public void eat() {
System.out.println("吃饭");
}
public void study() {
System.out.println("学习");
}
public void sleep() {
System.out.println("碎觉");
}
}
对象使用
/**
* @Time: 2021/5/22
* @Author: 大海
**/
public class test_03 {
public static void main(String[] args) {
/*
2.创建,格式:
类名称对象名=new类名称();
Student stu=new Student();
3.使用,分为两种情况:
使用成员变量:对象名.成员变量名
使用成员方法:对象名.成员方法名(参数)
(也就是,想用谁,就用对象名点儿谁。)
*/
// 使用类属性
Student stu = new Student();
System.out.println(stu.age);
// 类属性复制
stu.name ="测试小白";
System.out.println(stu.name);
// 调用类方法
stu.study();
}
}