本文目录导读:

我来通过几个典型案例,教你如何编写Java类定义:
基础学生类
/**
* 学生类 - 基础案例
*/
public class Student {
// 成员变量(属性)
private String name;
private int age;
private String studentId;
// 构造方法
public Student() {
// 无参构造
}
public Student(String name, int age, String studentId) {
this.name = name;
this.age = age;
this.studentId = studentId;
}
// 成员方法
public void study() {
System.out.println(name + "正在学习");
}
public void showInfo() {
System.out.println("姓名:" + name + ",年龄:" + age + ",学号:" + studentId);
}
// getter和setter方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age > 0 && age < 150) {
this.age = age;
} else {
System.out.println("年龄不合法");
}
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
}
继承案例 - 员工管理系统
// 父类
public class Employee {
protected String name;
protected double salary;
protected String department;
public Employee(String name, double salary, String department) {
this.name = name;
this.salary = salary;
this.department = department;
}
public void work() {
System.out.println(name + "正在工作");
}
public double calculateBonus() {
return salary * 0.1; // 基础奖金
}
}
// 子类 - 经理
public class Manager extends Employee {
private int teamSize;
public Manager(String name, double salary, String department, int teamSize) {
super(name, salary, department);
this.teamSize = teamSize;
}
@Override
public void work() {
System.out.println(name + "正在管理" + teamSize + "人的团队");
}
@Override
public double calculateBonus() {
return super.calculateBonus() + teamSize * 1000;
}
public void holdMeeting() {
System.out.println(name + "正在主持会议");
}
}
// 子类 - 程序员
public class Programmer extends Employee {
private String programmingLanguage;
public Programmer(String name, double salary, String department, String language) {
super(name, salary, department);
this.programmingLanguage = language;
}
@Override
public void work() {
System.out.println(name + "正在使用" + programmingLanguage + "编写代码");
}
@Override
public double calculateBonus() {
return super.calculateBonus() + 5000; // 程序员额外奖金
}
public void debug() {
System.out.println(name + "正在调试程序");
}
}
接口实现案例
// 定义接口
public interface Animal {
void eat();
void sleep();
void move();
}
// 实现类 - 狗
public class Dog implements Animal {
private String name;
private String breed;
public Dog(String name, String breed) {
this.name = name;
this.breed = breed;
}
@Override
public void eat() {
System.out.println(name + "在吃狗粮");
}
@Override
public void sleep() {
System.out.println(name + "在狗窝睡觉");
}
@Override
public void move() {
System.out.println(name + "在奔跑");
}
// 自己的特有方法
public void bark() {
System.out.println(name + "在汪汪叫");
}
}
// 实现类 - 鸟
public class Bird implements Animal, Flyable {
private String name;
public Bird(String name) {
this.name = name;
}
@Override
public void eat() {
System.out.println(name + "在吃虫子");
}
@Override
public void sleep() {
System.out.println(name + "在树上睡觉");
}
@Override
public void move() {
System.out.println(name + "在跳跃");
}
@Override
public void fly() {
System.out.println(name + "在飞翔");
}
}
// 飞行接口
public interface Flyable {
void fly();
}
抽象类案例
// 抽象类
public abstract class Shape {
protected String color;
public Shape(String color) {
this.color = color;
}
// 抽象方法
public abstract double calculateArea();
public abstract double calculatePerimeter();
// 具体方法
public void displayInfo() {
System.out.println("颜色:" + color);
System.out.println("面积:" + calculateArea());
System.out.println("周长:" + calculatePerimeter());
}
// 静态方法
public static void showShapeType() {
System.out.println("这是一个几何图形");
}
}
// 子类 - 圆形
public class Circle extends Shape {
private double radius;
public Circle(String color, double radius) {
super(color);
this.radius = radius;
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
@Override
public double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}
// 子类 - 矩形
public class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(String color, double width, double height) {
super(color);
this.width = width;
this.height = height;
}
@Override
public double calculateArea() {
return width * height;
}
@Override
public double calculatePerimeter() {
return 2 * (width + height);
}
}
单例模式案例
/**
* 懒汉式单例
*/
public class Singleton {
private static Singleton instance;
private Singleton() {
// 私有构造方法
}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
public void showMessage() {
System.out.println("单例模式示例");
}
}
/**
* 饿汉式单例
*/
public class EagerSingleton {
private static final EagerSingleton instance = new EagerSingleton();
private EagerSingleton() {}
public static EagerSingleton getInstance() {
return instance;
}
}
综合测试类
public class TestMain {
public static void main(String[] args) {
// 测试学生类
System.out.println("=== 学生测试 ===");
Student student = new Student("张三", 20, "2024001");
student.study();
student.showInfo();
// 测试继承
System.out.println("\n=== 员工测试 ===");
Manager manager = new Manager("李四", 30000, "技术部", 10);
Programmer programmer = new Programmer("王五", 20000, "技术部", "Java");
manager.work();
System.out.println("经理奖金:" + manager.calculateBonus());
programmer.work();
System.out.println("程序员奖金:" + programmer.calculateBonus());
// 测试接口
System.out.println("\n=== 动物测试 ===");
Dog dog = new Dog("旺财", "金毛");
Bird bird = new Bird("小叽");
dog.eat();
dog.bark();
bird.fly();
// 测试抽象类
System.out.println("\n=== 形状测试 ===");
Circle circle = new Circle("红色", 5);
Rectangle rect = new Rectangle("蓝色", 4, 6);
circle.displayInfo();
System.out.println("---");
rect.displayInfo();
// 测试单例
System.out.println("\n=== 单例测试 ===");
Singleton singleton = Singleton.getInstance();
singleton.showMessage();
}
}
类定义的关键要点
-
访问修饰符:
public:公共访问private:私有访问(封装)protected:受保护访问
-
封装原则:
- 属性私有化 (private)
- 提供公共的getter/setter方法
-
构造方法:
- 与类名相同
- 无返回值
- 可以有多个(重载)
-
方法定义:
访问修饰符 + 返回类型 + 方法名 + 参数
-
this关键字:
- 区分成员变量和局部变量
- 调用其他构造方法
这些案例涵盖了Java类定义的核心内容,你可以根据实际需求选择合适的模式进行开发。