由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - Re: Java Tip: Constant
相关主题
求助,using enum in eclipse[合集] 大家都用什么平台使用JAVA?
[转载] help please on Java请教大家一些问题
interface可不可以有variable?So weird.
final static constants easy get cached?我google了java进入了它的主页,然后提示下载java
Google Java coding standard现在做JAVA开发的用什么系统可靠点啊?
一个基本问题Windows 下 Java console application 的问题
[转帖]如何才算掌握Java(J2SE篇)Why java.lang.Iterable depends on java.util.Iterator
What's new in Java 6?Core Java2 Notes (3)
相关话题的讨论汇总
话题: interface话题: public话题: constant话题: constants话题: java
进入Java版参与讨论
1 (共1页)
xt
发帖数: 17532
1

Yes, this works, but it is not a good idea.
First, it does not make sense to implement a constant interface.
Interface means ADT, and what does a constant interface mean?
Second, implementing such an interface gives rise to naming
clashes. For example:
public interface A {
public static final int a = 1;
}
public interface B {
public static final int a = 2;
}
If you implement both interfaces, there you have to think how
the a is evaluated. If you want to be specific, A.a, B.a is better
w
xt
发帖数: 17532
2
Just some personal follow up ideas:
It does not kill you if you type a little more to make the program
more readable and better in style. We have been using vi editor
to write everything (excluding GUI design sometimes, but some
people actually write PowerJ scripts with vi instead of using
the PowerJ GUI to design frames as well. Even my co-op student
knows how to do it). Implementing interfaces with nothing but
constants is bad idea in terms of software engineering.
More typing may cost you tim

【在 xt 的大作中提到】
:
: Yes, this works, but it is not a good idea.
: First, it does not make sense to implement a constant interface.
: Interface means ADT, and what does a constant interface mean?
: Second, implementing such an interface gives rise to naming
: clashes. For example:
: public interface A {
: public static final int a = 1;
: }
: public interface B {

m******t
发帖数: 2416
3
I am with xt on this one. The basic point is, think about it
in OO way, that is, constants are also part of your data, rather
than just some macros in C. And you need to organize and encapsulate
your data into classes.
If you have either a good memory(which I don't have), or an IDE
supporting abbreviation/auto completion(which I do), typing long
variable names is never a big problem. 8-)
m******t
发帖数: 2416
4
Forte Community Edition is a commercial wrap-up for Netbeans.
Netbeans is open-source, but sponsored by Sun(that is, Sun is
cutting the pay checks)
Sun is planning on releasing the Prof. and Ent. Edition. But
won't be free.
I'd suggest you to go to www.netbeans.org. and download
Netbeans 3.0. It's a full-fledged Java IDE.
m*****e
发帖数: 126
5
I do not agree with some of your points.
1. Sometimes it is good practice to use interface to define constants.
A good example in JDK is the SwingConstants interface. It defined
many constants which are used elsewhere in javax.swing package.
2. About the naming confusion, I think the programer should be responsible
for this. If a programmer always names his variables as things like
'a' or 'b', surely he will have trouble from time to time.
3. If we name the constants properly, there
m******t
发帖数: 2416
6

First of all, while we all agree the Java core classes can be used
as classic examples for certain patterns or feature usage, not every
single bit of them is so "holy" and indisputable.
In the case of SwingConstants (and also some IO classes mentioned
in some previous post), the purpose of this separate interface is
to define constants that are used in so many swing classes that it is
inappropriate to define them repeatedly in all those classes. However,
the designer didn't put *all* constants

【在 m*****e 的大作中提到】
: I do not agree with some of your points.
: 1. Sometimes it is good practice to use interface to define constants.
: A good example in JDK is the SwingConstants interface. It defined
: many constants which are used elsewhere in javax.swing package.
: 2. About the naming confusion, I think the programer should be responsible
: for this. If a programmer always names his variables as things like
: 'a' or 'b', surely he will have trouble from time to time.
: 3. If we name the constants properly, there

xt
发帖数: 17532
7

Yes, there can be data members in interfaces. If the
interface is not
for constant definations only, there should be some methods
or semantic
features. In this case, the data members should be related
to the features
otherwise it does not make sense to put them there.
Considering following example, do you think it makes sense:
public interface MyConstants1 {
public static final int a = 1;
}
public interface MyConstants2 {
public static final int a = 2;
public static final int b = 3;
1 (共1页)
进入Java版参与讨论
相关主题
Core Java2 Notes (3)Google Java coding standard
编译原理,正则表示请教一个基本问题
Calendar的日期问题[转帖]如何才算掌握Java(J2SE篇)
Eclipse不运行appletWhat's new in Java 6?
求助,using enum in eclipse[合集] 大家都用什么平台使用JAVA?
[转载] help please on Java请教大家一些问题
interface可不可以有variable?So weird.
final static constants easy get cached?我google了java进入了它的主页,然后提示下载java
相关话题的讨论汇总
话题: interface话题: public话题: constant话题: constants话题: java