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