forked from CPU-Code/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_mem.java
More file actions
37 lines (32 loc) · 883 Bytes
/
Copy pathobject_mem.java
File metadata and controls
37 lines (32 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
* @Author: cpu_code
* @Date: 2020-07-11 10:48:13
* @LastEditTime: 2020-07-11 10:49:51
* @FilePath: \java\object\object_mem.java
* @Gitee: https://gitee.com/cpu_code
* @CSDN: https://blog.csdn.net/qq_44226094
*/
/**
* 访问对象成员
* 大家好,我叫韩强,我今年-30岁!
*/
public class object_mem
{
public static void main(String[] args)
{
Student stu = new Student(); // 创建学生对象
stu.name = "韩强"; // 为对象的name属性赋值
stu.age = -30; // 为对象的age属性赋值
stu.introduce(); // 调用对象的方法
}
}
class Student
{
String name; //定义一个姓名属性
int age; //定义一个年龄属性
public void introduce()
{
// 方法中打印属性name和age的值
System.out.println("大家好,我叫" + name + ",我今年" + age + "岁!");
}
}