-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathStruct.java
More file actions
60 lines (52 loc) · 1.92 KB
/
Struct.java
File metadata and controls
60 lines (52 loc) · 1.92 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Copyright © https://github.com/microwind All rights reserved.
* @author: jarryli@gmail.com
* @version: 1.0
* @description: 结构体数据结构 - Java实现
*/
// Person 类:表示一个人的基本信息
class Person {
String name;
int age;
String address;
// 构造函数:初始化 Person 对象的三个属性
public Person(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public void introduce() {
System.out.println("Hi, I am " + name + ", " + age + " years old, from " + address + ".");
}
}
// Employee 类:继承 Person,增加职位信息
class Employee extends Person {
String position;
// 构造函数:调用父类构造函数初始化 Person 部分,再设置职位
public Employee(String name, int age, String address, String position) {
super(name, age, address); // 调用父类 Person 的构造函数
this.position = position; // 初始化职位
}
// 重写父类的 introduce 方法,输出员工特有的自我介绍
@Override
public void introduce() {
System.out.println("I am " + name + ", a " + position + " at the company, living in " + address + ".");
}
}
// 主类:程序的入口,演示 Person 和 Employee 的使用
public class Struct {
public static void main(String[] args) {
// 创建 Person 对象并调用 introduce 方法
Person p1 = new Person("Alice", 30, "123 Main St");
p1.introduce();
// 创建 Employee 对象并调用重写后的 introduce 方法
Employee e1 = new Employee("Bob", 28, "456 Elm St", "Software Developer");
e1.introduce();
}
}
/*
* jarry@MacBook-Pro struct % javac Struct.java
* jarry@MacBook-Pro struct % java Struct
* Hi, I am Alice, 30 years old, from 123 Main St.
* I am Bob, a Software Developer at the company, living in 456 Elm St.
*/