在java中创建类的基础知识
这是一个简单的问题,但我的AP Comp Sci书并没有很好地解释它,你们总是很有用的在java中创建类的基础知识,java,class,oop,Java,Class,Oop,这是一个简单的问题,但我的AP Comp Sci书并没有很好地解释它,你们总是很有用的 在Java中创建自定义类并在该类中创建方法,然后调用这些方法的基本方法是什么。我知道这很简单,但我在任何地方都找不到一个好的解释似乎你需要更好地理解OOP。因此,让我们创建一个类和一个测试客户机来帮助您。我们将创建一个类和一个测试客户机(或驱动程序)来计算电势。请注意,这个例子来自罗伯特·塞吉威克和凯文·韦恩(我极力推荐一本优秀的书) 首先,我们创建一个费用类: /* Separate classes mus
在Java中创建自定义类并在该类中创建方法,然后调用这些方法的基本方法是什么。我知道这很简单,但我在任何地方都找不到一个好的解释似乎你需要更好地理解OOP。因此,让我们创建一个类和一个测试客户机来帮助您。我们将创建一个类和一个测试客户机(或驱动程序)来计算电势。请注意,这个例子来自罗伯特·塞吉威克和凯文·韦恩(我极力推荐一本优秀的书) 首先,我们创建一个
费用类:
/*
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编程中的一个重要概念
您不需要知道如何实现数据类型就可以使用它
公共课收费
这是构造器:
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);
}
}
. 当你在那里提问时,请始终包括你的尝试,并向我们展示你的尝试。看起来你甚至都没有搜索,因为Oracle官方文档几乎是google上所有java搜索的第一个结果。这对我帮助很大,非常感谢。