1 /*
2 通常情况下,一个类并不能直接使用,需要根据类创建一个对象,才能使用。
3
4 1. 导包:也就是指出需要使用的类,在什么位置。
5 import 包名称.类名称;
6 import cn.itcast.day06.demo01.Student;
7 对于和当前类属于同一个包的情况,可以省略导包语句不写。
8
9 2. 创建,格式:
10 类名称 对象名 = new 类名称();
11 Student stu = new Student();
12
13 3. 使用,分为两种情况:
14 使用成员变量:对象名.成员变量名
15 使用成员方法:对象名.成员方法名(参数)
16 (也就是,想用谁,就用对象名点儿谁。)
17
18 注意事项:
19 如果成员变量没有进行赋值,那么将会有一个默认值,规则和数组一样。
20 */
21 public class Demo02Student {
22
23 public static void main(String[] args) {
24 // 1. 导包。
25 // 我需要使用的Student类,和我自己Demo02Student位于同一个包下,所以省略导包语句不写
26
27 // 2. 创建,格式:
28 // 类名称 对象名 = new 类名称();
29 // 根据Student类,创建了一个名为stu的对象
30 Student stu = new Student();
31
32 // 3. 使用其中的成员变量,格式:
33 // 对象名.成员变量名
34 System.out.println(stu.name); // null
35 System.out.println(stu.age); // 0
36 System.out.println("=============");
37
38 // 改变对象当中的成员变量数值内容
39 // 将右侧的字符串,赋值交给stu对象当中的name成员变量
40 stu.name = "赵丽颖";
41 stu.age = 18;
42 System.out.println(stu.name); // 赵丽颖
43 System.out.println(stu.age); // 18
44 System.out.println("=============");
45
46 // 4. 使用对象的成员方法,格式:
47 // 对象名.成员方法名()
48 stu.eat();
49 stu.sleep();
50 stu.study();
51 }
52
53 }