有人可以在OOP上下文中提供方法与函数的简单解释吗?
在此代码中,当我在main方法中创建一个Object 然后调用该对象方法:( ff.twentyDivCount(i)运行在16010毫秒)时,它运行速度比使用此注释调用它快得多:( twentyDivCount(i)运行在59516毫秒).当然,当我在不创建对象的情况下运行它时,我将方法设为静态,因此可以在main中调用它.
public class ProblemFive {
// Counts the number of numbers that the entry is evenly divisible by, as max is 20
int twentyDivCount(int a) { // Change to static int.... when using it directly
int count = 0;
for (int i = 1; i<21; i++) {
if (a % i == 0) {
count++;
}
}
return count;
}
public static void main(String[] args) {
long startT = System.currentTimeMillis();;
int start = …Run Code Online (Sandbox Code Playgroud) 我现在正在编写一本教科书,它将纯方法定义为:
"一种静态方法,仅取决于其参数而不依赖于其他数据"
如果一个实例方法不是静态的(只要它不修改参数并且没有像打印那样的"副作用"),那么它是不可能的?
我知道纯方法是纯粹的,返回值只取决于参数而不依赖于任何其他状态,所以可能调用实例方法意味着从调用方法的对象中获取的变量不算作参数但作为另一个"状态"?
除此之外,我无法想到为什么非静态方法不能成为纯方法.
这是一个例子:
public class Rational {
private int numer;
private int denom;
public Rational() {
this.numer = 0;
this.denom = 1;
}
public Rational(int numer, int denom) {
this.numer = numer;
this.denom = denom;
}
}
Run Code Online (Sandbox Code Playgroud)
以上定义了一个Rational类
然后,您可以在Rational类中编写一个方法,该方法通过下面的"方法一"或"方法二" 返回一个Rational对象double.
方法一:
public double toDouble() {
double x = this.numer;
double y = this.denom;
double fprat = x / y;
return fprat;
}
Run Code Online (Sandbox Code Playgroud)
方法二:
public static double toDouble(Rational …Run Code Online (Sandbox Code Playgroud) 我有一个变长的Java类.当我通过代码质量工具运行它时,我会被标记为类中的行数.
这是一个较低的层类,由上层使用Spring @Autowired.该类有许多非静态的私有实例方法.它们不使用任何实例字段,仅适用于方法参数.
我可以像public static在某个单独的实用程序类中一样安全地移动这些方 有什么缺点?
所以我的编码器朋友讨厌使用static编码.然而,我的Java程序充满了它在类之间的链接,我有很多它们!
是否值得重写整个代码来删除静态方法?
使用一个优于另一个是否有任何优势?
关于静态的东西:
类似的问题:
我很困惑:
我知道在OOP中,instance = object.所以,如果我们有这样的类:
public class something
{
public static void main(String args[])
{
}
}
Run Code Online (Sandbox Code Playgroud)
main方法中的这一行会创建新对象instance吗?
something instance=new something();
Run Code Online (Sandbox Code Playgroud)
我的第二个问题是类似的:如果我们创建Thread对象 - Thread t1=new Thread();它是否真的意味着我们已经创建了一个类的实例Thread,我们可以静态地调用方法?(例如sleep()).
在我的应用程序中,有许多类包含处理数据的方法,这些方法可以是计算和数据丰富.
我的问题是 - 如果类没有任何类级变量,我可以使类中的所有方法都是静态的吗?
我想线程不会有任何问题.
有什么后果吗?是否有任何性能优势,因为我没有实例化类?
样本类:
Class A{
public Object findTheCar(String id){
Car car= new Car();
//do something
return car;
}
}
Run Code Online (Sandbox Code Playgroud)
我打算将上面的内容改为静态.
有一个用于捕获异常的短方法的类是否很好?
class ContractUtils{
public static String getCode(Contract contract) throws MyException{
try{
return contract.getInfo().getCode(); //throws ContractException and LogicException
}catch(Exception e){
throw new MyException("error during code reading:"+e.getMessage, e);
}
}
//other methods like above...
}
Run Code Online (Sandbox Code Playgroud) ES5的哪些问题是ES6中应该处理的静态类方法?
Babel文档在其关于ES6类的部分中有以下示例,但它实际上并未说明此模式实现的内容.
类支持基于原型的继承,超级调用,实例和静态方法以及构造函数
class SkinnedMesh extends THREE.Mesh {
constructor(geometry, materials) {
super(geometry, materials);
this.idMatrix = SkinnedMesh.defaultMatrix();
this.bones = [];
this.boneMatrices = [];
//...
}
update(camera) {
//...
super.update();
}
static defaultMatrix() {
return new THREE.Matrix4();
}
}
Run Code Online (Sandbox Code Playgroud) java ×8
static ×5
methods ×3
instance ×2
object ×2
class ×1
ecmascript-6 ×1
function ×1
javascript ×1
oop ×1
performance ×1
semantics ×1
short ×1
spring ×1
terminology ×1
try-catch ×1