【问题标题】:Basics to creating a class in java [closed]在java中创建类的基础[关闭]
【发布时间】:2014-10-31 13:43:48
【问题描述】:

这是一个简单的问题,但我的 AP Comp Sci 书解释得不够好,你们总是很有用的。

在 Java 中创建自定义类并在该类中创建方法,然后调用这些方法的基本方法是什么。我知道它很简单,但我在任何地方都找不到好的解释

【问题讨论】:

标签: java class oop


【解决方案1】:

看来您需要更好地了解 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);
    }
}

【讨论】:

  • 抱歉,我生病了,没能查看您的评论。这对我有很大帮助,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-10
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多