op -java的居民

发布于 2025-02-13 07:01:19 字数 413 浏览 0 评论 0原文

我在2天前就发现了Java的OOP,现在我有一个我无法独自重新熟食的问题。 我有4个公司为一家公司,在第一类中,我们在第二堂3级是:

public class Profession {
    public enum Position {
        Developer, Mechanic, Director
    }
}"

那么我如何在第一堂课中使用这些枚举来设置工资? 我的意思是类似的东西

"if (position is Developer{
salary = 5000
}else if (position is Mechanic){
salary = 3000
}"

,当我回到主要的时候写下类似的东西: 囊泡。 (通过使用枚举来捷径进行捷径。

I found out the OOP in JAVA just 2 days ago, and now I have a problem that I can not reslove alone.
I have 4 classes for a company, in the 1st class we have the characteristics of an employee(name, salary, exepirience) in the 2nd I made a list and a method to add them in the class with the main using the method, the 3rd class is :

"

public class Profession {
    public enum Position {
        Developer, Mechanic, Director
    }
}"

So how can I use those Enums in the first class to set the salary?
I mean something like

"if (position is Developer{
salary = 5000
}else if (position is Mechanic){
salary = 3000
}"

and when I go back in the main and write something like:
veselin. (to have the shortcut to setProfession by using the ENUMS.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的那抹悲凉 2025-02-20 07:01:19

您必须有一个枚举实例,目前,您只有它的“蓝图”。创建您的位置字段(我将其重命名为位置类型)并使用它。另外,作为个人喜好,我不喜欢将我的枚举嵌入课堂上,因为您每次使用时都必须在类名称上前缀。

一个示例:

import java.util.*;
import java.lang.*;
import java.io.*;

class Profession {
    public enum PositionTypes {
        Developer, Mechanic, Director
    }

    private PositionTypes _position;

    public void setPosition(PositionTypes position) {
        _position = position;
    }

    public PositionTypes getPosition() {
        return _position;
    }
}

class Main {
    public static void main(String[] args) {
        Profession p1 = new Profession();
        System.out.println(p1.getPosition());
    
        p1.setPosition(Profession.PositionTypes.Director);
        System.out.println(p1.getPosition());
    
        if (p1.getPosition() == Profession.PositionTypes.Director)
            System.out.println("We made a check!");
    }
}

此输出:

null
Director
We made a check!

You have to have an instance of your enum, currently you only have the "blueprint" of it. Create a field of your Position (I've renamed it PositionTypes) and use that. Also, as a personal preference, I don't like having my enums embedded in classes, since you'd have to prefix the class name every time you use it.

An example:

import java.util.*;
import java.lang.*;
import java.io.*;

class Profession {
    public enum PositionTypes {
        Developer, Mechanic, Director
    }

    private PositionTypes _position;

    public void setPosition(PositionTypes position) {
        _position = position;
    }

    public PositionTypes getPosition() {
        return _position;
    }
}

class Main {
    public static void main(String[] args) {
        Profession p1 = new Profession();
        System.out.println(p1.getPosition());
    
        p1.setPosition(Profession.PositionTypes.Director);
        System.out.println(p1.getPosition());
    
        if (p1.getPosition() == Profession.PositionTypes.Director)
            System.out.println("We made a check!");
    }
}

This outputs:

null
Director
We made a check!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文