栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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类,对于类的使用后面需要不断的练习熟悉

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/581790.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号