我有一个由机器人标记的Java评估.每当我上传我的作业时,它都会显示一个这样的屏幕.
A good object-oriented design places each method into the most appropriate class. The most appropriate class for a method should be the same class as the data fields that that method needs to access. If you don't place a method into the right class, then this will most likely increase the amount of communication that is required between your classes. The score below measures the amount of communication between your classes. A lower score is better. 19 method invocations between classes 7 arguments passed between classes 15 results returned between classes Amount of communication = invocations + 2*inputs + 2*outputs = 63
现在,"类之间的方法调用","类之间传递的参数"和"类之间返回的结果"究竟是什么意思?
类之间的方法调用
当你的类包含自己的方法,如果你想从另一个类调用方法,你必须使用一个实例本的类.
例如 :
class A{ public void methodA(){ } } class B{ public void methodB(){ } }
如果我想methodA()
从课堂打电话,B
我必须使用这个:
public void methodB(){ A a = new A(); a.methodA(); // method invocation between classes }
类之间传递的参数
这个时间methodA()
需要一个参数,并且可以B
作为一个可以用作参数的字段.
class A{ public void methodA(int argument){ } } class B{ private int fieldB = 42; public void methodB(){ } }
methodA()
从B
你那里打电话会把一个论点从一个类传递给另一个.
public void methodB(){ A a= new A(); a.methodA(fieldB); //Argument passed between classes }
在类之间返回结果
现在methodA()
返回一个结果,这是代码.
class A{ public int methodA(){ return 42; } } class B{ private int fieldB; public void methodB(){ } }
要使用/处理返回的值methodA()
从类 B
你必须这样做:
public void methodB(){ A a= new A(); fieldB = a.methodA(); //Result returned between classes }