Tag: oop

使用OOP结构添加和减去复数

我这里有一个代码,应该打印两个复数的和和差。 给出的指示是: 使方法add , subtract和print void 使用构造函数的对象进行测试。 public class Complex { /** * @param args */ public double real; public double imag; public String output = “”; public Complex(double real, double imag){ this.real += real; this.imag += imag; } public Complex(){ real = 0; imag = 0; } public double getReal(){ return real; } public […]

Java程序不会向ArrayList添加对象

我正在尝试将不同的对象添加到ArrayList ,但它无法正常工作。 我不确定是不是因为我正在错误地添加对象或者是否有其他错误。 这是我的代码。 import java.util.*; import java.io.*; public class QuizBowl implements Quiz { private Player player; // player object private String file; // name of file private int qNum; // number of questions player wants to answer private int qNumFile; // number of questions in file private ArrayList questionsArr; // holds Question objects private […]

Java OOP基础知识

我有一个问题,一直阻碍我进一步推进,这个错误在我看来根本不符合逻辑,我从一本书中学习,代码来自那里。 这是代码: package test_area; public class clzzz { class SimpleCircle{ double radius; SimpleCircle() { radius = 1; } SimpleCircle(double newRadius) { radius = newRadius; } double getArea() { return radius*radius*Math.PI; } double getPerimeter() { return 2*radius*Math.PI; } void setRadius(double newRadius) { radius = newRadius; } } public static void main(String[] args) { SimpleCircle circle1 = […]

OOAD设计问题

我有两个表: tblCustomer , tblProduct : tblCustomer: Id: Integer, auto-increament Name: Varchar(30) …. tblProduct Id: Integer, auto-increament Name: Varchar(50) customerId: Integer …. 还有两个类: Customer , Product : public class Product { private int id; private int name; /* Other stuffs */ } public class Customer { private int id; private String name; private String phoneNumber; /* […]

动态方法调度

在互联网上有很多关于动态调度的信息,我觉得自己像个鸡,因为我无法实现它。 请帮我。 这是我想要做的。 ClassA{ public void createReq(){ } public String postReq(){ } } ClassB{ @Test public void myTest(){ Class A = new ClassA(); a.createReq(); String test = a.getResponse(); /* Not sure how do i do this part */ } 所以,我在myTest方法中得到一个字符串’test’ 。 我想创建一个扩展ClassB的ClassC ,并编写一个方法,用于在步骤( a.getResponse() )之后很快validationmyTest返回的字符串。 如果没有实施ClassC ,我只想简单地结束测试。 如果只存在ClassC并实现validation方法,我希望validation完成。 我该怎么做呢? 请帮忙。 谢谢。

在TDD中模拟值

在GOOS书中。 它被告知不要嘲笑价值观 ,这让我感到困惑。 这是否意味着价值观没有任何行为? 我不太了解价值对象,但AFAIK价值对象是那些不可变的。 在决定何时创建值对象时是否有任何启发式方法?

如何使用枚举中的每种环境类型的值列表来表示键?

我有两个环境PROD和STAGING 。 在prod环境中,我们有三个数据中心ABC , DEF和PQR并且暂存有一个数据中心CORP 。 每个数据中心都有很少的机器,我为它们定义了常量,如下所示: // NOTE: I can have more machines in each dc in future public static final ImmutableList ABC_SERVERS = ImmutableList.of(“tcp://machineA:8081”, “tcp://machineA:8082”); public static final ImmutableList DEF_SERVERS = ImmutableList.of(“tcp://machineB:8081”, “tcp://machineB:8082”); public static final ImmutableList PQR_SERVERS = ImmutableList.of(“tcp://machineC:8081”, “tcp://machineC:8082”); public static final ImmutableList STAGING_SERVERS = ImmutableList.of(“tcp://machineJ:8087″,”tcp://machineJ:8088”); 现在我在同一个类中定义了另一个常量,它按DC分组到每个环境类型的机器列表。 public static final ImmutableMap<Datacenter, […]

在线程“main”中获取exceptionjava.lang.StackOverflowError

我是Java和OOP的新手,这是我的问题。 当我运行以下代码时,我得到了 线程“main”java.lang.StackOverflowError中的exception。 我在代码中遇到的问题是我创建了一个JavaApplication1的对象。类App2没有出现问题。 如果在run方法中创建了对象ja则代码可以正常工作。 你能解释一下为什么吗? package javaapplication1; public class JavaApplication1 { int i, k, j; class App2 { int i = 23; int j = 12; } App2 a2 = new App2(); JavaApplication1 ja = new JavaApplication1(); public void run() { ja.i = 10; a2.i = 26; a2.j = 18; System.out.println(i + “,” + […]

为什么这个Java转换会抛出错误?

我想知道为什么在obj = w;之后引用“w” obj = w; 会抛出错误。 你是不是只是通过说obj = w来创建另一个指向该w实例的指针? 也就是说,为什么说String s = “hi”; String w = s;类String s = “hi”; String w = s;东西是不同的String s = “hi”; String w = s; String s = “hi”; String w = s; 谢谢! public class Casting { public static void main(String[] args) { // casting doesn’t change […]

Java – 从列表/数组中的每个对象获取单个属性的最简单方法?

假设我有一个人物对象,其name , hair color和eye color等属性。 我有以下数组Person[] people ,其中包含人物对象的实例。 我知道我可以获得一个Person对象的name属性 // create a new instance of Person Person george = new Person(‘george’,’brown’,’blue’); // <<>> // access the name property String georgesName = people[0].name; 但是如果我想在不使用索引的情况下访问每个人的name属性呢? 例如,要创建一个只是名称或头发颜色的数组或列表? 我是否必须手动遍历我的peoplearrays? 或者像String[] peopleNames = people.name这样的Java有什么String[] peopleNames = people.name吗?