feat(spring): 实现基于注解的简单IoC容器

创建了一个简单的IoC容器,能够通过注解扫描包并创建对象。该容器支持依赖注入,使用反射机制来创建对象实例和管理它们的生命周期。此外,还实现了一个示例Service类和服务的注册与检索机制。

fix(redoAll): 解决LeetCode题目存在的问题- 修正LeetCode11, 21, 22, 23, 29, 31, 32, 42, 121, 337, 474, 494, 2602题目的解决方案。
- 确保所有新增的测试用例通过验证。

refactor(meituan824): 重构美团秋招题目代码- 重构`meituan824`类,提高解题代码的质量和可读性。
- 修正`mtp1`和`mtp2`方法,使其能够正确解决对应的问题。
This commit is contained in:
whaifree 2024-08-25 23:45:01 +08:00
parent ffa1d4435b
commit bd1472ccf2
16 changed files with 1100 additions and 2 deletions

View File

@ -0,0 +1,143 @@
package cn.whaifree.interview.qiuzhao;
import cn.whaifree.leetCode.model.ListNode;
import java.util.Scanner;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/24 9:57
* @注释
*/
public class meituan824 {
}
class mtp1{
/**
* 小美初始位于ab)位置二维平面上有n个瓶子每个瓶子的位置为xy小美每次可以向上石移动一格每次移动的代价为 1
* 小美需要每次移动到一个瓶子的位置上然后拿起瓶子把它放到(c,d) 位置每次最多只能拿一个瓶子请问最少需要多少代价才能把所有瓶子都放到c,d位置上
* @param args
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext hasNextLine 的区别
int startX = in.nextInt();
int startY = in.nextInt();
int targetX = in.nextInt();
int targetY = in.nextInt();
int num = in.nextInt();
int res = 0;
for (int i = 0; i < num; i++) {
int x = in.nextInt();
int y = in.nextInt();
res += go(startX, startY, x, y);
res += go(x, y, targetX, targetY);
startX = targetX;
startY = targetY;
}
System.out.println(res);
}
public static int go(int startX, int startY, int endX, int endY) {
int res = 0;
res += Math.abs(endX - startX);
res += Math.abs(endY - startY);
return res;
}
}
class mtp2{
/**
* 小美有三个数字ab,c他每次操作可以选择一个数字将其加一k次小美想知道a×bxc的最大值是多少
* @param nums
* @return
*/
public static int findMin(int[] nums) {
int min = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] < nums[min]) {
min = i;
}
}
return min;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext hasNextLine 的区别
int[] nums = new int[3];
for (int i = 0; i < nums.length; i++) {
nums[i] = in.nextInt();
}
int k = in.nextInt();
for (int i = 0; i < k; i++) {
int min = findMin(nums);
nums[min] += 1;
}
System.out.println(((long) nums[0] * nums[1] * nums[2])%1000000007);
// int tmpMax = 1;
// for (int i = 0; i < nums.length; i++) {
// tmpMax *= nums[i];
// }
//
// for (int j = 0; j < k; j++) {
// int tmp = tmpMax;
// int indexIncr = 0;
// for (int i = 0; i < nums.length; i++) {
// int num = nums[i];
// int b = (tmpMax / num) * (num + 1);
// if (tmp < b) {
// tmp = b;
// indexIncr = i;
// }
// }
// tmpMax = Math.max(tmpMax, tmp);
// nums[indexIncr] += 1;
// }
//
// System.out.println(tmpMax);
}
}
class test {
public static void main(String[] args) {
ListNode listNode = ListNode.listNodeFromArray(new int[]{1, 2, 3, 4, 5, 6});
ListNode.printList(rm(listNode, 6));
}
public static ListNode rm(ListNode head, int n) {
ListNode pre = new ListNode(-1, head);
ListNode after = pre;
for (int i = 0; i < n; i++) {
after = after.next;
}
ListNode preTmp = pre;
while (after.next != null) {
after = after.next;
preTmp = preTmp.next;
}
preTmp.next = preTmp.next.next;
return pre.next;
}
}

View File

@ -14,8 +14,8 @@ public class LeetCode494 {
public void test() public void test()
{ {
Solution solution = new Solution(); Solution solution = new Solution();
int[] nums = new int[]{1, 1, 1, 1, 1}; int[] nums = new int[]{1,0};
int target = 3; int target = 1;
int i = solution.findTargetSumWays(nums, target); int i = solution.findTargetSumWays(nums, target);
System.out.println(i); System.out.println(i);
} }

View File

@ -0,0 +1,45 @@
package cn.whaifree.redo.redoAll;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/24 2:14
* @注释
*/
public class LeetCode11 {
@Test
public void test() {
int[] height = {1,1};
System.out.println(new Solution().maxArea(height));
}
class Solution {
/**
*
* 不断移动短线
*
* @param height
* @return
*/
public int maxArea(int[] height) {
int left = 0;
int right = height.length - 1;
int cap = Integer.MIN_VALUE;
while (left <= right) {
cap = Math.max(cap, (right - left) * Math.min(height[right], height[left]));
if (height[left] < height[right]) {
left++;
}else {
right--;
}
}
return cap;
}
}
}

View File

@ -0,0 +1,70 @@
package cn.whaifree.redo.redoAll;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/25 15:09
* @注释
*/
public class LeetCode121 {
@Test
public void test() {
Solution1 solution = new Solution1();
int i = solution.maxProfit(new int[]{7, 1, 5, 3, 6, 4});
System.out.println(i);
}
class Solution {
public int maxProfit(int[] prices) {
int maxProfit = 0;
int left = 0;
int right = 0;
while (right < prices.length) {
if (prices[right] < prices[left]) {
left = right;
}
maxProfit = Math.max(maxProfit, prices[right] - prices[left]);
right++;
}
return maxProfit;
}
}
class Solution1 {
/**
*
* 只能选择 某一天 买入这只股票
*
*
* dp[0][i] 表示第i天手头没有有股票的最大利润
* - 第i-1天就没有 dp[0][i-1]
* - 今天刚刚卖出 dp[1][i-1] + price[i]
* dp[1][i] 表示第i天手头有股票的最大利润
* - 前一天就有 dp[1][i-1]
* - 刚刚买入 - price[i] 只能选择 某一天 买入这只股票
*
*
*
* @param prices
* @return
*/
public int maxProfit(int[] prices) {
int[][] dp = new int[2][prices.length];
dp[0][0] = 0;
dp[1][0] = -prices[0];
for (int i = 1; i < prices.length; i++) {
dp[0][i] = Math.max(dp[0][i - 1], dp[1][i - 1] + prices[i]);
dp[1][i] = Math.max(dp[1][i - 1], - prices[i]);
}
return dp[0][prices.length - 1];
}
}
}

View File

@ -0,0 +1,54 @@
package cn.whaifree.redo.redoAll;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/24 2:26
* @注释
*/
public class LeetCode21 {
@Test
public void test() {
new Solution().mergeTwoLists(
ListNode.listNodeFromArray(new int[]{1, 2, 4}),
ListNode.listNodeFromArray(new int[]{1, 3, 4})
).printList();
}
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode tmpHead = new ListNode(-1);
ListNode index1 = list1;
ListNode index2 = list2;
ListNode index = tmpHead;
while (index1 != null && index2 != null) {
if (index1.val < index2.val) {
index.next = index1;
index1 = index1.next;
}else {
index.next = index2;
index2 = index2.next;
}
index = index.next;
}
while (index1 != null) {
index.next = index1;
index1 = index1.next;
index = index.next;
}
while (index2 != null) {
index.next = index2;
index2 = index2.next;
index = index.next;
}
return tmpHead.next;
}
}
}

View File

@ -0,0 +1,74 @@
package cn.whaifree.redo.redoAll;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/24 2:35
* @注释
*/
public class LeetCode22 {
@Test
public void test() {
System.out.println(new Solution().generateParenthesis(1));
}
class Solution {
List<String> res;
StringBuilder path;
Character[] characters;
public List<String> generateParenthesis(int n) {
// characters = new Character[n * 2];
// for (int i = 0; i < n; i++) {
// characters[i] = '(';
// }
// for (int i = n; i < n * 2; i++) {
// characters[i] = ')';
// }
res = new ArrayList<>();
path = new StringBuilder();
circle(n, n);
return res;
}
/**
* <img src="http://42.192.130.83:9000/picgo/imgs/efbe574e5e6addcd1c9dc5c13a50c6f162a2b14a95d6aed2c394e18287a067fa-image.png">
*
* @param left
* @param right
*/
public void circle(int left, int right) {
if (left == 0 && right == 0) {
res.add(path.toString());
return;
}
if (left < 0) {
return;
}
if (left > right) {
return;
}
path.append("(");
circle(left - 1, right);
path.deleteCharAt(path.length() - 1);
path.append(")");
circle(left, right - 1); // 回溯后左边括号还是可以用的
path.deleteCharAt(path.length() - 1);
}
}
}

View File

@ -0,0 +1,58 @@
package cn.whaifree.redo.redoAll;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test;
import java.util.Comparator;
import java.util.PriorityQueue;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/25 0:51
* @注释
*/
public class LeetCode23 {
@Test
public void test() {
new Solution().mergeKLists(
new ListNode[]{
// [[-2,-1,-1,-1],[]]
ListNode.listNodeFromArray(new int[]{-2,-1,-1,-1}),
ListNode.listNodeFromArray(new int[]{}),
}
).printList();
}
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
PriorityQueue<ListNode> priorityQueue = new PriorityQueue<>(new Comparator<ListNode>() {
@Override
public int compare(ListNode o1, ListNode o2) {
return o1.val - o2.val;
}
});
for (ListNode list : lists) {
ListNode tmp = list;
while (tmp != null) {
priorityQueue.add(tmp);
tmp = tmp.next;
}
}
ListNode pre = new ListNode(-1);
ListNode tmp = pre;
while (!priorityQueue.isEmpty()) {
ListNode poll = priorityQueue.poll();
poll.next = null; // 防止出现循环
tmp.next = poll;
tmp = tmp.next;
}
return pre.next;
}
}
}

View File

@ -0,0 +1,82 @@
package cn.whaifree.redo.redoAll;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/22 23:09
* @注释
*/
public class LeetCode2602 {
class Solution {
public List<Long> minOperations(int[] nums, int[] queries) {
List<Long> res = new ArrayList<>();
for (int query : queries) {
res.add(minOpr(nums, query));
}
return res;
}
public long minOpr(int[] nums, long q) {
long res = 0;
for (int num : nums) {
res += Math.abs(num - q);
}
return res;
}
}
@Test
public void test() {
// [3,1,6,8], queries = [1,5]
int[] nums = new int[]{3, 1, 6, 8};
int[] queries = new int[]{1, 5};
Solution1 solution = new Solution1();
List<Long> res = solution.minOperations(nums, queries);
System.out.println(res);
}
class Solution1 {
public List<Long> minOperations(int[] nums, int[] queries) {
Arrays.sort(nums);
long[] preSum = new long[nums.length + 1];
for (int i = 1; i <= nums.length; i++) {
preSum[i] = preSum[i - 1] + nums[i - 1];
}
List<Long> res = new ArrayList<>();
for (int query : queries) {
res.add(minOpr(preSum, nums, query));
}
return res;
}
public long minOpr(long[] preSum,int[] nums, int q) {
int index = Arrays.binarySearch(nums, q);
if (index < 0) {
// index of the search key, if it is contained in the array; otherwise, (-(insertion point) - 1). The
index = -index - 1;
}
long preSize = preSum[index];
long then = (long) q * index - preSize;
long square = (long)(preSum.length - index - 1) * q;
long up = preSum[preSum.length - 1] - preSize - square;
return then + up;
}
}
}

View File

@ -0,0 +1,68 @@
package cn.whaifree.redo.redoAll;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/25 14:45
* @注释
*/
public class LeetCode29 {
@Test
public void test(){
Solution solution = new Solution();
int i = solution.divide(-14,7 );
System.out.println(i);
}
class Solution {
/**
*
* 17/3 3<2 12 5 3<0
*
* 2^2 + 2^0 = 5
*
*
* 10 / 3
*
* 3<1 6 4 3<0
*
* 2^1+2^0
*
* @param dividend
* @param divisor
* @return
*/
public int divide(int dividend, int divisor) {
if (dividend == 0) {
return 0;
}
if (dividend == Integer.MIN_VALUE && divisor == -1) {
return Integer.MAX_VALUE;
}
boolean negative;
negative = (dividend ^ divisor) <0;//用异或来计算是否符号相异
long Ldividend = Math.abs((long) dividend);
long Ldivisor = Math.abs((long) divisor);
int sumPow = 0;
while (Ldividend >= Ldivisor) {
int pow = 0;
while ((Ldivisor << (pow + 1)) < Ldividend) {
pow++;
}
Ldividend -= Ldivisor << pow;
sumPow += (int) Math.pow(2, pow);
}
return negative ? -sumPow : sumPow;
}
}
}

View File

@ -0,0 +1,65 @@
package cn.whaifree.redo.redoAll;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/25 1:09
* @注释
*/
public class LeetCode31 {
@Test
public void test() {
// 123546321
// 找到递增在右边找到能够让其变大的最靠右的此时右边部分一定递减替换逆转右半部分
int[] nums = {1, 2, 3, 5, 4, 6,5,2,1};
new Solution().nextPermutation(nums);
for (int num : nums) {
System.out.print(num);
}
}
class Solution {
public void nextPermutation(int[] nums) {
// 123465 下一个排列 123546321
// 从后往前找到第一个升序的[a,x1,x2,b]
// 从b那部分找到第一个大于x1的c
// 替换x1 和c
// reverse x2--
int i;
for (i = nums.length - 2; i >= 0; i--) {
if (nums[i] < nums[i + 1]) {
break;
}
}
if (i == -1) {
reverse(nums, 0, nums.length - 1);
return;
}
int j;
for (j = nums.length - 1; j > i; j--) {
if (nums[j] > nums[i]) {
break;
}
}
swap(nums, i, j);
reverse(nums, i + 1, nums.length - 1);
}
public void reverse(int[] nums, int start, int end) {
while (start < end) {
swap(nums, start, end);
start++;
end--;
}
}
public void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}

View File

@ -0,0 +1,59 @@
package cn.whaifree.redo.redoAll;
import org.junit.Test;
import java.util.Deque;
import java.util.LinkedList;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/25 14:22
* @注释
*/
public class LeetCode32 {
@Test
public void test(){
String s = "()(()";
Solution solution = new Solution();
int i = solution.longestValidParentheses(s);
System.out.println(i);
}
class Solution {
public int longestValidParentheses(String s) {
char[] charArray = s.toCharArray();
Deque<Integer> stack = new LinkedList<>();
boolean[] dp = new boolean[s.length()];
for (int i = 0; i < charArray.length; i++) {
char c = charArray[i];
if (c == '(') {
stack.push(i);
}else if (!stack.isEmpty()){
Integer pop = stack.pop();
dp[pop] = true;
dp[i] = true;
}
}
// 计算flag中最长连续出现1的次数
int len = 0;
int maxLen = Integer.MIN_VALUE;
for (int i = 0; i < dp.length; i++) {
if (dp[i]) {
len += 1;
}else {
maxLen = Math.max(maxLen,len);
len = 0;
}
}
return Math.max(maxLen, len); // (() 这个用例
}
}
}

View File

@ -0,0 +1,48 @@
package cn.whaifree.redo.redoAll;
import cn.whaifree.leetCode.model.TreeNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/25 15:45
* @注释
*/
public class LeetCode337 {
@Test
public void test() {
System.out.println(
new Solution()
.rob(TreeNode.constructTree(new Integer[]{3, 2, 3, null, 3, null, 1})));
}
class Solution {
public int rob(TreeNode root) {
int[] ints = robOrNot(root);
return Math.max(ints[0], ints[1]);
}
public int[] robOrNot(TreeNode root) {
if (root == null) {
return new int[2];
}
int[] res = new int[2];
int[] right = robOrNot(root.right);
int[] left = robOrNot(root.left);
res[0] = Math.max(right[0], right[1]) + Math.max(left[0], left[1]);
res[1] = right[0] + left[0] + root.val;
return res;
}
}
}

View File

@ -0,0 +1,47 @@
package cn.whaifree.redo.redoAll;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/25 17:29
* @注释
*/
public class LeetCode42 {
@Test
public void test(){
System.out.println(new Solution().trap(new int[]{0,1,0,2,1,0,1,3,2,1,2,1}));
}
class Solution {
public int trap(int[] height) {
int[] leftHeight = new int[height.length];
int tmpLeftHeight = 0;
for (int i = 0; i < height.length; i++) {
int n = height[i];
tmpLeftHeight = Math.max(tmpLeftHeight, n);
leftHeight[i] = tmpLeftHeight;
}
int[] rightHeight = new int[height.length];
int tmpRightHeight = 0;
for (int i = height.length - 1; i >= 0; i--) {
int n = height[i];
tmpRightHeight = Math.max(tmpRightHeight, n);
rightHeight[ i ] = tmpRightHeight;
}
int res = 0;
for (int i = 0; i < height.length; i++) {
int left = leftHeight[i];
int right = rightHeight[i];
res += (Math.min(left, right) - height[i]);
}
return res;
}
}
}

View File

@ -0,0 +1,133 @@
package cn.whaifree.redo.redoAll;
import org.junit.Test;
/**
* @Author whai文海
* @Date 2024/8/22 21:00
* @version 1.0
* @注释
*
*/
public class LeetCode474 {
@Test
public void test() {
String[] strs = {"10", "0001", "111001", "1", "0"};
int m = 5;
int n = 3;
Solution solution = new Solution();
System.out.println(solution.findMaxForm(strs, m, n));
}
class Solution {
/**
*
* 物品 ["10", "0001", "111001", "1", "0"]
*
* 背包
*
* dp[i][j][k] 表示在前i个字符串中放入了j个0和k个1的背包最大子集长度
* 物品 i
* 0 j
* 1 k
*
*
*
*
*
*
*
* @param strs
* @param m
* @param n
* @return
*/
public int findMaxForm(String[] strs, int m, int n) {
int[][][] dp = new int[strs.length][m + 1][n + 1];
String index0 = strs[0];
int index0Zero = calculateZero(index0);
int index0One = index0.length() - index0Zero;
for (int i = index0Zero; i <= m; i++) {
for (int j = index0One; j <= n; j++) {
dp[0][i][j] = 1;
}
}
for (int i = 1; i < strs.length; i++) {
String str = strs[i];
int zero = calculateZero(str);
int one = str.length() - zero;
for (int j = 0; j <= m; j++) {
for (int k = 0; k <= n; k++) {
if (zero > j || one > k) {
dp[i][j][k] = dp[i - 1][j][k];
}else {
dp[i][j][k] = Math.max(dp[i - 1][j][k], dp[i - 1][j - zero][k - one] + 1);
}
}
}
}
return dp[strs.length-1][m][n];
}
public int calculateZero(String str) {
int zero = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '0') {
zero++;
}
}
return zero;
}
}
class Solution1 {
public int findMaxForm(String[] strs, int m, int n) {
// 三维 dp[i][j][k] 表示在前i个字符串中包含了了j个0和k个1的最大子集长度
// String[i]为物品 j k 为背包
// 初始化 当物品i=0 表示在前0个字符串中没有字符串可以使用dp[0][j][k]=0
// 递推公式
// 获取0的数量numZero 获取1的数量numOne
// 当j<numZero k<numOne时dp[i][j][k]=dp[i-1][j][k] 放不下
// else
// - dp[i-1][j-numZero][k-numOne]+1
// - 不放 dp[i-1][j][k]
int length = strs.length;
int[][][] dp = new int[length + 1][m + 1][n + 1];
for (int i = 1; i < strs.length + 1; i++) {
String str = strs[i-1];
int zeroNumber = calculateZeroNumber(str);
int oneNumber = str.length() - zeroNumber;
for (int j = 0; j <= m; j++) {
for (int k = 0; k <= n; k++) {
if (j < zeroNumber || k < oneNumber) {
dp[i][j][k] = dp[i - 1][j][k];
} else {
dp[i][j][k] = Math.max(dp[i - 1][j - zeroNumber][k - oneNumber] + 1, dp[i - 1][j][k]);
}
}
}
}
return dp[length][m][n];
}
public int calculateZeroNumber(String str)
{
int num = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '0')
{
num++;
}
}
return num;
}
}
}

View File

@ -0,0 +1,81 @@
package cn.whaifree.redo.redoAll;
import org.junit.Test;
import java.util.Arrays;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/8/22 22:05
* @注释
*/
public class LeetCode494 {
@Test
public void test()
{
Solution solution = new Solution();
int[] nums = new int[]{1,0};
int target = 1;
int i = solution.findTargetSumWays(nums, target);
System.out.println(i);
}
class Solution {
/**
*
* sum =
* x需要变负数的数量
*
* sum = x*2+target
* x = ( sum - target ) / 2
*
*
* 背包容量 x
* 物品nums
*
* dp[i][j] 表示 从0-i中取满足背包容量j的数量
*
*
* nums[1,2,3,1,1] target 5
* 0 1 2 3
* 0 1 1 1 1 1
* 1 2 1 1 2 2
* 2 3 1 1 2 3
* 3 1 1 2 3
* 4 1 1
*
*
* @param nums
* @param target
* @return
*/
public int findTargetSumWays(int[] nums, int target) {
int sum = Arrays.stream(nums).sum();
int packageSize = (sum - target) / 2;
if (Math.abs(target) > sum) return 0; // 此时没有方案
if ((sum - target) % 2 == 1) return 0; // 此时没有方案
int[][] dp = new int[nums.length + 1][packageSize + 1];
for (int i = 0; i < dp.length; i++) {
dp[i][0] = 1;
}
for (int i = 1; i <= nums.length; i++) {
for (int j = 0; j <= packageSize; j++) {
if (j >= nums[i - 1]) {
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i - 1]];
}else {
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[nums.length][packageSize];
}
}
}

View File

@ -0,0 +1,71 @@
package cn.whaifree.tech.spring;
import java.util.HashMap;
import java.util.Map;
/**
*
*
* 1. 容器能够扫描包获取Bean
* 能够使用反射创建对象
* 能够DI反射获取参数并从容器中获取
*
* 2. 注解
*
*
* @version 1.0
* @Author whai文海
* @Date 2024/8/25 11:16
* @注释
*/
public class IOCDemo {
}
// 定义容器接口
interface Container {
<T> T getBean(Class<T> clazz);
}
// 容器实现类
class SimpleIoCContainer implements Container {
private final Map<Class<?>, Object> beans = new HashMap<>();
@Override
public <T> T getBean(Class<T> clazz) {
return clazz.cast(beans.get(clazz));
}
public <T> void registerBean(Class<T> clazz, T instance) {
beans.put(clazz, instance);
}
}
// 示例类
class Service {
public String hello() {
return "Hello, World!";
}
}
class Main {
public static void main(String[] args) {
// 创建容器实例
SimpleIoCContainer container = new SimpleIoCContainer();
// 注册服务
Service service = new Service();
container.registerBean(Service.class, service);
// 从容器中获取服务
Service retrievedService = container.getBean(Service.class);
// 使用服务
System.out.println(retrievedService.hello());
}
}