简体   繁体   English

以下哪一项是 java 中更好的编程实践来调用用户定义的 class 中存在的方法?

[英]Which one of the below is a better programming practice in java to call methods present in a user defined class?

Stack newStack = new Stack();
newStack.push(0);

OR或者

Stack.push(0);

Note: Here Stack is a user defined class to implement concept of stack manually in java and not the predefined one.注意:这里的 Stack 是用户定义的 class 以在 java 中手动实现堆栈的概念,而不是预定义的。

This allows having multiple stacks for different purposes.这允许有多个堆栈用于不同的目的。

Stack newStack = new Stack();
newStack.push(0);

We can have only one stack when using this.使用它时我们只能有一个堆栈。

Stack.push(0);

I prefer the first one as it allows initializing multiple stacks at the same time which the second way can't do.我更喜欢第一种方法,因为它允许同时初始化多个堆栈,而第二种方法无法做到。

In the first example, push is a non-static method of the Stack class.在第一个示例中, pushStack class 的非静态方法。 In order to use the push method, you have to create an instance of the Stack class.为了使用push方法,您必须创建Stack class 的实例。

Stack newStack = new Stack();
newStack.push(0);

The equivalent code with an anonymous instance would be:具有匿名实例的等效代码将是:

new Stack().push(0);

which would be kind of useless in this case, since you're eventually going to want to pop the stack you created.在这种情况下这将是无用的,因为您最终会想要pop您创建的堆栈。

In the second example, push is a static method of the Stack class.在第二个示例中, pushStack class 的 static 方法。

Stack.push(0);

Nandu Raj is correct in his answer in that you can only have one Stack in your application, rather than more than one with the non-static push method. Nandu Raj 的回答是正确的,因为您的应用程序中只能有一个Stack ,而不是使用非静态push方法的多个 Stack。

Creating and using non-static methods is generally a better programming practice, although there are cases, like the Java Math class, where static methods work better.创建和使用非静态方法通常是一种更好的编程实践,尽管在某些情况下,例如 Java Math class,其中 static 方法效果更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 编程用户自定义 class - Java programming User defined class 哪种更好的Java编程实践:堆叠枚举和枚举构造函数或子类化? - Which is better Java programming practice: stacking enums and enum constructors, or subclassing? 哪种更好的做法,在类中使用私有变量或公共方法? - Which is better practice, using private variables or public methods within the class? 在Java中这是更好的做法 - Which is a better practice in java 在 Java 中,多个 select 查询或连接,哪一个是更好的做法? - In Java, mutiple select queries or join, which one would be a better practice? 我如何使用“对象类”在java中调用用户定义类的方法 - how can I Use of "Object Class" to call methods of user-defined classes in java Java 8:更好地扩展静态方法类或更好地直接调用静态方法? - Java 8: Better to extend class of static methods or better to call static methods directly? 如何在将用户定义的类对象作为类成员的 Java 中创建用户定义的不可变类? - How to create user defined immutable class in Java which is having user defined class object as a class member? 单元测试的最佳实践 class 主要负责调用依赖项的方法,但也包含逻辑 - Best practice for Unit Testing class which is mostly responsible to call methods of dependencies, but contains logic as well 在Java的父类中已经定义的子类中重写相同的静态方法是一种不好的做法吗? - Is it a bad practice to override same static method in child class which is already defined in parent class in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM