在Java中在另一个对象内实例化对象(组合 vs 继承)。

huangapple go评论75阅读模式
英文:

Instantiating an object within another object in Java (Composition vs inheritance)

问题

我正在尝试理解Java中的组合工作原理,但遇到了困难。我有一个程序,它实例化一个Customer对象,该对象具有Computer对象作为实例变量。
在我的程序中,有两种类型的Computer对象,一个是Desktop子类,另一个是Laptop子类,所以我让我的Desktop类扩展Computer,我的Laptop类也扩展Computer。我将Computer类设置为抽象类,但我知道无法实例化抽象对象。我不确定如何在Customer类中实例化Computer对象。任何见解都将不胜感激!
示例代码:

public class Customer
{
private String name;
private Computer computer;

public Customer(String n)
{
setName(n);
this.computer = new Computer(); //无法这样做,因为Computer是抽象的
}

//为姓名设置器和访问器
public Computer getComputer()
{
return new Computer(this.computer);
}

public void setComputer(Computer computer)
{
this.computer = new Computer(computer);
}

}

public abstract class Computer{
}

public class Desktop extends Computer{
}

public class Laptop extends Computer{

}

英文:

I'm trying to understand how composition in Java works and I'm getting stuck. I have a program that instantiates a Customer object who has a Computer object as an instance variable.
There are two types of Computer objects in my program, a Desktop and a Laptop subclass, so I have my Desktop class extending Computer and my Laptop class also extending Computer. I have my Computer class as an abstract class but I know that you can't instantiate an abstract object. I'm not sure how to instantiate the Computer object in my Customer class. Any insight is appreciated !
Example code:

public class Customer
{
   private String name;
   private Computer computer;

   public Customer(String n)
   {
      setName(n);
      this.computer = new Computer(); //Can't do this b/c Computer is abstract
   }

   //Setters and getters for name
   public Computer getComputer()
   {
      return new Computer(this.computer);
   }

   public void setComputer(Computer computer)
   {
      this.computer = new Computer(computer);
   }
   
}
public abstract class Computer{
}

public class Desktop extends Computer{
}

public class Laptop extends Computer{

}

答案1

得分: 0

是的,你说得对。你不能实例化抽象类Computer()。
相反,你可以这样做:

this.computer = new Laptop()
this.computer = new Desktop()

取决于它们具有哪些特点。

英文:

Yes you are right. You cannot instantiate the abstract class Computer().
Instead, you can do

this.computer = new Laptop() 
this.computer = new Desktop()

depending on which they have.

答案2

得分: 0

解决这个问题的方式是,你的构造函数还应该接受一个计算机参数。

public Customer(String n, Computer c)
{
    setName(n);
    this.computer = c;
}

但是有许多理由更喜欢只具有空构造函数的POJO(普通的旧Java对象),而不是在构造类型时要求组合元素的类。更多信息请参见:https://stackoverflow.com/questions/14172621/whats-the-advantage-of-pojo

英文:

The way you solve this problem is that your Constructor should also take a Computer argument

   public Customer(String n, Computer c)
   {
      setName(n);
      this.computer = c;
   }

But there are a lot of reasons to prefer POJOs (Plain Old Java Objects) with only a null constructor over classes that demand the composition elements at construct-type. For more information, see: https://stackoverflow.com/questions/14172621/whats-the-advantage-of-pojo

huangapple
  • 本文由 发表于 2020年5月4日 08:11:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61583276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定