这是一个简单的问题,但是我的AP Comp Sci书并没有解释得那么好,你们总是很有用。
在Java中创建自定义类并在该类中创建方法,然后调用这些方法的基本方法是什么。我知道它很简单但我无法在任何地方找到一个好的解释
答案 0 :(得分:0)
您似乎需要更好地了解OOP。所以,让我们创建一个类和一个测试客户端来帮助你。我们将创建一个类和一个测试客户端(或驱动程序)来计算电势。请注意这个例子来自Robert Sedgewick和Kevin Wayne的Introduction to Programming in Java(我强烈推荐的一本优秀的书)。
首先我们创建一个Charge
类:
/*
Separate classes must be in the same directory as main method or must invoke
classpath
*/
public class Charge {
// first declare instance variables which are usually private
private final double rx;
private final double ry;
private final double q;
/* A class contains constructors that are invoked to create objects from a
class blueprint. Constructor declarations look like method declarations
-except that they use the name of the class and have no return type.
Constructors must use the exact name of the class, case sensitive.
Classes and Constructors are capitalized - methods are camelCase.
*/
// Constructor
public Charge(double x0, double y0, double q0) {
rx = x0;
ry = y0;
q = q0;
}
/*
The method to compute electrical potential which is defined by the equation
V = kq/r
*/
public double potentialAt(double x, double y) {
double k = 8.99e09; // Electrostatic Constant that k=8.99 X 10^9 Nm^2/C^2 (N = Newtons, m = meters, C = Coloumbs)
// r = delta x - delta y
double dx = x - rx; // delta x for distance
double dy = y - ry; // delta y for distance
return k*q/Math.sqrt(dx*dx + dy*dy); // Computation using distance formula
}
}
这将是使用此类的API。 Java编程中的一个重要概念 是你不需要知道如何实现数据类型才能使用它。
public class Charge
这是构造函数:
Charge(double x0, double y0, double q0)
这些是实例方法。变量之间最重要的区别 引用类型与原始类型是您可以使用引用类型变量 调用实现数据类型操作的方法。
double potentialAt(double x, double y)
String toString()
使用这个课程的两个部分是:
1. Create an object
ClassName object = new ClassName (invoke Constructor)
--------- ------ --- --------- -----------------
Charge c = new Charge (2.2, 3.4, 7.2)
2. Use instance methods on object
c.potentialAt(2.3, 4.2)
这将是可以与此类一起使用的客户端(或驱动程序):
import java.util.*;
public class ChargeClient {
public static void main(String[] args) {
// Using a scanner object to get values
System.out.println("Please enter an X Value");
Scanner in = new Scanner(System.in);
double x = in.nextDouble();
System.out.println("Please enter a Y Value");
double y = in.nextDouble();
/*
1. Instantiate objects c1 and c2
ClassName object = new ClassName (invoke Constructor)
--------- ------ --- --------- -----------------
Charge c = new Charge (2.2, 3.4, 7.2)
2. We are invoking constructor from API
Charge(double x0, double y0, double q0)
*/
Charge c1 = new Charge(.51, .63, 21.3);
Charge c2 = new Charge(.13, .94, 81.9);
// print out charge so we know what we are dealing with
System.out.println(c1);
System.out.println(c2);
/*
Here we create variables to hold the return from our potential method
which is enacted on our c1 and c2 objects.
1. We call a method on an object by:
objectName.methodName(appropriate parameters)
*/
double v1 = c1.potentialAt(x, y);
double v2 = c2.potentialAt(x, y);
// Concatenate results and print them out.
System.out.println(v1 + v2);
System.out.println("This is the printf statement:");
System.out.printf("%.2E\n", v1 + v2);
}
}