一个只有静态数据和访问这些数据的方法的类.如何正确实施? [英] A class that only has static data and methods to access these data. How to implement that properly?

查看:62
本文介绍了一个只有静态数据和访问这些数据的方法的类.如何正确实施?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是初学者的问题.我是 Java 新手,也是一般编程的新手.

I know that is a beginner's question. I'm new to java and and also to programming in general.

假设我有一个只有静态数据的类,例如:

Say I got a class that has only static data, example:

class Foo {
private static int x;  }

我想在不实例化任何对象的情况下使用该类.所以我希望能够做到:

I want to use the class without instantiating any object. So I want to be able to do:

Foo.setX(5);
Foo.getX();

实现这个类的最佳方法是什么,我现在对接口和其他东西有点困惑.

What is the best way to implement this class, I'm a little bit confused about interfaces and other stuff for now.

谢谢.

推荐答案

为什么不直接定义两个修改/返回静态字段的静态方法?

Why don't you just define two static methods that modify/return the static field?

Java 中的静态方法

这篇关于一个只有静态数据和访问这些数据的方法的类.如何正确实施?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆