Java基础 - 面向对象 - 类的定义
1 package mingri.chapter_6;
2
3 import java.util.Scanner;
4
5 public class Person {
6
7 /*
8 * 类变量
9 * 定义方法:
10 * 数据类型 变量名称 [ = 值]; // 定义类变量时可以赋值,也可以不赋值
11 * */
12
13 private String name; // 姓名
14 private String sex; // 性别
15 private String age; // 年龄
16 private String cardId; // 身份证号
17
18
19 /*
20 * 类方法
21 * 定义方法:
22 * [权限修饰符] [返回值类型] 方法名([参数类型 参数名]) [throws 异常类型] {
23 * 方法体;
24 * return 返回值;
25 * }
26 * 权限修饰符:
27 * 可以是 private、public、protected 中的任意一个,也可以不写,主要用来控制方法的访问权限
28 * 返回值类型:
29 * 用来指定方法返回数据的类型,可以是任何类型,如果方法不需要返回值,则使用void关键字
30 * 参数:
31 * 类方法既可以有参数,也可以没有参数,参数可以是对象,也可以是基本数据类型的变量
32 * */
33
34 // 输出生日
35 public void showBir(String cardId) {
36 System.out.println("cardId: " + cardId);
37 System.out.println("用户的生日是:" + cardId.substring(6, 14));
38 }
39
40
41 public static void main(String[] args) {
42 Person person = new Person();
43
44 Scanner sc = new Scanner(System.in);
45
46 System.out.println("请输入用户姓名:");
47 person.name = sc.nextLine();
48
49 System.out.println("请输入用户性别:");
50 person.sex = sc.nextLine();
51
52 System.out.println("请输入用户年龄:");
53 person.age = sc.nextLine();
54
55 System.out.println("请输入用户身份证号:");
56 person.cardId = sc.nextLine();
57
58 person.showBir(person.cardId);
59
60 }
61
62
63
64 }