Java面向对象6----几个简单的类实践案例

Java面向对象6----几个简单的类实践案例,第1张

Java面向对象6----几个简单的类实践案例

前面学习了Java中的类的相关知识,这里通过几个简单的案例来进一步熟悉内容分

案例1:Dog类

属性:颜色,品种,姓名,年龄
方法:咬人,吃饭

class Dog {
    private String name; // 狗的名字
    private String color; // 狗的颜色

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    private int age; // 狗的年龄
    private String type; // 狗的品种

    // 构造方法
    public Dog() {}
    public Dog(String name, String color, int age, String type) {
        this.name = name;
        this.age = age;
        this.color = color;
        this.type = type;
    }

    // setter gette 方法
    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    // 咬人
    public void bark() {
        System.out.println(this.color + "色的" + this.name + "咬人了!!!");
    }

    // 吃饭
    public void eat() {
        System.out.println(this.color + "色的" + this.name + "开始吃狗粮了!!!");
    }
}

public class Demo {
    public static void main(String[] args) {
        Dog dog1 = new Dog("小花", "斑点", 2, "中华田园犬");
        Dog dog2 = new Dog("小黑", "黑", 4, "阿拉斯加");
        dog1.bark();
        dog2.eat();
    }
}

案例3:Book类

属性:名字,编号,售价,库存
方法:显示书本的库存

class Book {
    private String bookName; // 书名
    private long bookNo; // 编号
    private double price; // 价格
    private static int count = 0; // 库存

    public Book(String bookName, long bookNo, double price) {
        this.bookName = bookName;
        this.bookNo = bookNo;
        this.price = price;
        Book.count++;
    }

    // setter, getter 方法省略

    // 获的库存
    public static int getCount() {
        return Book.count;
    }
}

public class Demo {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            new Book("book"+i, (long)(100*i), 19.99);
        }
        System.out.println("书籍的库存为:" + Book.getCount());
    }
}

案例2:员工(Employee)类

定义一个Employee类
属性:编号、姓名、年纪,固定薪水(5000)、固定增长率(0.2),年资, 实际增长率,实际薪资。
包含方法

  • 计算实际增长率(固定正增长率+0.15*年资)
  • 计算当前工资增长额度
  • 计算增长后的工资总额,并更新薪资
class Employee {
    private long empno; // 编号
    private String name; // 姓名
    private int age; // 年龄
    private int hireYear = 0; // 年资
    private static double baseSalary = 5000.00; // 固定薪水
    private static double baseRate = 0.2; // 固定增长率
    private double realRate; // 实际增长率=0.04*hireYear + baseRate
    private double realSalary; // 实际薪资 = baseSalary * (1 + realRate)

    // 构造方法
    public Employee(String name, long empno, int age, int hireYear) {
        this.name = name;
        this.empno = empno;
        this.age = age;
        this.hireYear = hireYear;
    }

    public Employee(String name, long empno, int age) {
        this(name, empno, age, 0); // 调用构造方法
    }

    // setter, getter 方法省略

    // 计算实际增长率
    public double getRealRate() {
        this.realRate = Employee.baseRate + 0.15 * hireYear; // 更新实际增长率
        return this.realRate;
    }

    // 计算当前工资增长额度
    public double getSalaryIncr() {
        return Employee.baseSalary * this.getRealRate();
    }

    // 计算实际工资
    public double getRealSalary() {
        this.realSalary = Employee.baseSalary + this.getSalaryIncr();
        return this.realSalary;
    }

    // 获得信息
    public String getInfo() {
        return "name: " + this.name + "tage: " + this.age +
                "tempno: " + this.empno + "thireYear: " + this.hireYear +
                "trealSalary: " + this.getRealSalary() + "trealRate" + this.realRate;
    }
}

public class Demo {
    public static void main(String[] args) {
        Employee emp1 = new Employee("emp1", 10000L, 40, 5);
        System.out.println(emp1.getInfo());
        Employee emp2 = new Employee("emp2", 100002L, 20);
        System.out.println(emp2.getInfo());
    }
}


这里只展示了几个简单的Java类,对于类的使用后面需要不断的练习熟悉

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5573004.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存