mirror of
https://github.com/Estom/notes.git
synced 2026-04-09 13:58:58 +08:00
multi-class-demo
This commit is contained in:
54
JavaDemo/multiclass-demo/Makefile
Normal file
54
JavaDemo/multiclass-demo/Makefile
Normal file
@@ -0,0 +1,54 @@
|
||||
# Makefile for multiclass-demo Java project
|
||||
|
||||
# Java compiler and options
|
||||
JAVAC = javac
|
||||
JAVA = java
|
||||
JFLAGS = -g
|
||||
|
||||
# Source and build directories
|
||||
SRC_DIR = src
|
||||
BUILD_DIR = build
|
||||
|
||||
# Find all Java source files
|
||||
SOURCES = $(shell find $(SRC_DIR) -name "*.java")
|
||||
CLASSES = $(SOURCES:$(SRC_DIR)/%.java=$(BUILD_DIR)/%.class)
|
||||
|
||||
# Main class
|
||||
MAIN_CLASS = com.example.Application
|
||||
|
||||
# Default target
|
||||
.PHONY: all
|
||||
all: compile
|
||||
|
||||
# Compile all Java source files
|
||||
.PHONY: compile
|
||||
compile:
|
||||
@mkdir -p $(BUILD_DIR)
|
||||
$(JAVAC) $(JFLAGS) -sourcepath $(SRC_DIR) -d $(BUILD_DIR) $(SOURCES)
|
||||
@echo "Compilation completed successfully!"
|
||||
|
||||
# Run the application
|
||||
.PHONY: run
|
||||
run: compile
|
||||
$(JAVA) -cp $(BUILD_DIR) $(MAIN_CLASS)
|
||||
|
||||
# Clean compiled classes
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -rf $(BUILD_DIR)/*
|
||||
@echo "Clean completed!"
|
||||
|
||||
# Rebuild the project
|
||||
.PHONY: rebuild
|
||||
rebuild: clean compile
|
||||
|
||||
# Help target
|
||||
.PHONY: help
|
||||
help:
|
||||
@echo "Available targets:"
|
||||
@echo " all - Compile all source files (default)"
|
||||
@echo " compile - Compile all source files"
|
||||
@echo " run - Compile and run the application"
|
||||
@echo " clean - Remove all compiled classes"
|
||||
@echo " rebuild - Clean and compile all source files"
|
||||
@echo " help - Display this help message"
|
||||
29
JavaDemo/multiclass-demo/src/com/example/Application.java
Normal file
29
JavaDemo/multiclass-demo/src/com/example/Application.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package com.example;
|
||||
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
// 创建PersonService实例
|
||||
PersonService service = new PersonService();
|
||||
|
||||
// 添加一些Person对象,注意名字的大小写会被自动规范化
|
||||
service.addPerson(new Person("alice", 25));
|
||||
service.addPerson(new Person("BOB", 30));
|
||||
service.addPerson(new Person("cHaRlIe", 35));
|
||||
|
||||
// 显示所有人员
|
||||
System.out.println("All persons (with names properly capitalized):");
|
||||
for (Person person : service.getAllPersons()) {
|
||||
System.out.println(person);
|
||||
}
|
||||
|
||||
// 查找特定人员
|
||||
Person found = service.findPersonByName("Bob");
|
||||
System.out.println("\nFound person: " + found);
|
||||
|
||||
// 测试修改人员名字
|
||||
if (found != null) {
|
||||
found.setName("david");
|
||||
System.out.println("After name change: " + found);
|
||||
}
|
||||
}
|
||||
}
|
||||
35
JavaDemo/multiclass-demo/src/com/example/Person.java
Normal file
35
JavaDemo/multiclass-demo/src/com/example/Person.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package com.example;
|
||||
|
||||
import com.example.util.StringUtils;
|
||||
|
||||
public class Person {
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public Person(String name, int age) {
|
||||
// 使用工具类方法
|
||||
this.name = StringUtils.capitalize(name);
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = StringUtils.capitalize(name);
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person{name='" + name + "', age=" + age + "}";
|
||||
}
|
||||
}
|
||||
27
JavaDemo/multiclass-demo/src/com/example/PersonService.java
Normal file
27
JavaDemo/multiclass-demo/src/com/example/PersonService.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package com.example;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PersonService {
|
||||
private List<Person> persons;
|
||||
|
||||
public PersonService() {
|
||||
this.persons = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addPerson(Person person) {
|
||||
persons.add(person);
|
||||
}
|
||||
|
||||
public List<Person> getAllPersons() {
|
||||
return new ArrayList<>(persons);
|
||||
}
|
||||
|
||||
public Person findPersonByName(String name) {
|
||||
return persons.stream()
|
||||
.filter(p -> p.getName().equals(name))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.util;
|
||||
|
||||
public class StringUtils {
|
||||
public static boolean isEmpty(String str) {
|
||||
return str == null || str.length() == 0;
|
||||
}
|
||||
|
||||
public static String capitalize(String str) {
|
||||
if (isEmpty(str)) {
|
||||
return str;
|
||||
}
|
||||
return str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user