在Java中创建类的基础 [英] Basics to creating a class in java
问题描述
这是一个简单的问题,但我的AP Comp Sci书籍没有解释得很好,你们总是有用的。
This is a simple question, but my AP Comp Sci book just doesn't explain it well enough and you guys are always useful.
什么是基本方法在Java中创建自定义类并在该类中创建方法,然后调用这些方法。我知道它的简单,但我找不到一个很好的解释任何地方
What is the basic way of creating a custom class in Java and creating methods within that class, then later calling those methods. I know its simple but I can't find a good explanation anywhere
推荐答案
看起来你需要更好地了解OOP。所以,让我们创建一个类和一个测试客户端来帮助你。我们将创建一个类和一个测试客户端(或驱动程序)来计算电势。请注意,此示例来自Robert Sedgewick和Kevin Wayne的 Java编程简介
It seems like you need a better understanding of OOP. So, let's create a class and a test client to help you. We will create a class and a test client (or driver) to compute electrical potential. Please note this example is from Introduction to Programming in Java by Robert Sedgewick and Kevin Wayne (an excellent book I highly recommend).
首先我们创建一个费用
课程:
First we create a Charge
class:
/*
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
}
}
使用这个类。在Java编程中一个重要的概念
是,你不需要知道如何实现数据类型来使用它。
This would be the API to use this class. An important concept in Java programming is that you do not need to know how a data type is implemented to be able to use it.
这是构造函数:
Charge(double x0, double y0, double q0)
。
引用类型与原始类型的变量之间最重要的区别是,您可以使用引用类型变量
来调用实现数据类型操作的方法。
These are instance methods. The most important difference between a variable of reference type vs primitive type is that you can use reference type variables to invoke methods that implement data type operations.
double potentialAt(double x, double y)
String toString()
$ b b
使用这个类的两个部分是:
The two parts of using this class would be:
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)
或驱动程序),可以与此类一起使用:
This would be the client (or driver) that could be used with this class:
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);
}
}
这篇关于在Java中创建类的基础的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!