[英]How can I express object which should be 2 different classes at the same time
Today I had test in OOP and I was given the following task to code: 今天,我在OOP中进行了测试,并得到了以下编写代码的任务:
Imagine you have two classes: Employee (which represents being an employee) and Ninja (which represents being a Ninja). 想象一下,您有两个类:Employee(代表雇员)和Ninja(代表忍者)。 An Employee has both state and behaviour; 雇员既有状态又有行为; a Ninja has only behavior. 忍者只有行为。 You need to represent an employee who is also a ninja (a common problem in the real world). 您需要代表也是忍者(真实世界中的常见问题)的员工。 By creating only one interface and only one class (NinjaEmployee), show how you can do this without having to copy method implementation code from either of the original classes. 通过仅创建一个接口和仅一个类(NinjaEmployee),说明如何执行此操作而不必从任何一个原始类中复制方法实现代码。 Test your code in main method 在主要方法中测试您的代码
I did not really understand the problem well, but this is the solution I came with (I know it's not what was asked): 我不是很了解问题,但这是我附带的解决方案(我知道这不是要问的问题):
I created 4 classes except main. 除了main外,我创建了4个类。 As Employee has state and behaviour I came up with this code: 由于Employee具有状态和行为,因此我想到了以下代码:
public class Employee {
private int ID;
private String Name;
private double salary;
public Employee(int ID, String Name, double salary) {
this.ID = ID;
this.Name = Name;
this.salary = salary;
}
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void startWorking() {
System.out.println("Started Working");
}
}
Class ninja has only behaviour: 忍者职业只有以下行为:
public class Ninja {
public Ninja(){}
public void moveNinja(){
System.out.println("Ninja moved");
}
}
Class NinjaEmployee: 忍者职业:
public class NinjaEmployee extends Employee implements MyInterface {
public NinjaEmployee(int ID, String Name, double salary) {
super(ID, Name, salary);
}
public void moveNinja() {
System.out.println("Ninja Moved");
}
}
Interface which does not make sense in my code: 在我的代码中没有意义的接口:
public interface MyInterface {
public void moveNinja();
public void startWorking();
}
Main class: 主班:
public static void main(String[] args){
MyInterface em = new NinjaEmployee(123,"Sandro",1000000);
em.moveNinja();
em.startWorking();
}
My question is following: 我的问题如下:
1) Specifically/Technically what was asked in test? 1)具体/技术上测试要求什么?
2) What would be correct approach/code for given problem? 2)对于给定的问题,正确的方法/代码是什么?
I don't know correct answer (task is kinda not very strictly defined, there is some unclear moments), but i would do something like this: 我不知道正确的答案(任务的定义不是很严格,有一些不清楚的时刻),但是我会做这样的事情:
public interface IAmNinja {
public void moveNinja();
}
public interface IAmEmployer {
public void startWorking();
}
public class NinjaEmployee implements IAmNinja, IAmEmployer {
private Ninja _ninja;
private Employer _employer;
public NinjaEmployee(int ID, String Name, double salary) {
_employer = new Employer(ID, Name, salary);
_ninja = new Ninja();
}
public void moveNinja() {
_ninja.moveNinja();
}
public void startWorking() {
_employer.startWorking();
}
}
Nice question. 好问题。
The key point of the question is: 问题的关键是:
So we should try to use these key point. 因此,我们应该尝试使用这些关键点。
I provide a class diagram below: 我在下面提供了一个类图:
First of all: We have Employee
class and implement it like other simple classes. 首先:我们有Employee
类,并像其他简单类一样实现它。 It has some implemented attributes and classes. 它具有一些已实现的属性和类。
Secondly : We have an Interface named Ninja_Interface
that have some method declarations about ninja. 其次 :我们有一个名为Ninja_Interface
的接口,其中包含一些有关忍者的方法声明。 ( moveNinja1
and moveNinja2
) ( moveNinja1
和moveNinja2
)
Thirdly : Ninja
Class that implemented (or Realized) Nijna_Interface
and have some implementation of any method declarations in Ninja_Interface
. 第三 :实现(或实现) Nijna_Interface
并在Ninja_Interface
实现任何方法声明的某种实现的Ninja
类。
Fourthly : the NinjaEmployee
class. 第四 : NinjaEmployee
类。 It inherited from Employee
. 它继承自Employee
。 So it has all Employee
's attributes and methods. 因此,它具有所有Employee
的属性和方法。 Also it implements Ninja_Interface
. 它还实现Ninja_Interface
。 So it should implements all Ninja_Interface
methods declarations. 因此,它应该实现所有Ninja_Interface
方法声明。 On the other hand, NinjaEmployee
have an instance of Ninja
(notice that Ninja
class implements all Ninja_Interface
methods). 另一方面, NinjaEmployee
有一个Ninja
实例(注意Ninja
类实现了所有Ninja_Interface
方法)。
So , In Ninja_Employee
class, in implementation of Ninja_Interface
methods, we can use Ninja
instance methods to call. 因此 ,在Ninja_Employee
类中,在实现Ninja_Interface
方法时,我们可以使用Ninja
实例方法进行调用。
For example some parts of NinjaEmployee
is like below code: 例如, NinjaEmployee
某些部分类似于以下代码:
private Ninja ninja=new Ninja();
public void moveNinja1()
{
ninja.moveNinja1();
}
public void moveNinja2()
{
ninja.moveNinja2();
}
Main question is: why Ninja
class should have only some methods ? 主要问题是:为什么Ninja
类应该只具有某些方法 ?
It is because of Ninja
class is just the implementations of Ninja_Interface
methods and there no need to have attributes. 因为Ninja
类只是Ninja_Interface
方法的实现,因此不需要属性。 So instances of Ninja
class are the same . 因此Ninja
类的实例是相同的 。 So we can declare Ninja
attribute in NinjaEmployee
as static attribute . 因此,我们可以将NinjaEmployee
Ninja
属性声明为静态属性 。
Finally : we can add some attributes of ninja into NinjaEmployee
class too. 最后 :我们也可以将ninja的某些属性添加到NinjaEmployee
类中。
You cant create 1 object of 2 class es You can extend class so whenever child class is instantiated it calls parent class constructor Then You can create object of another class in that constructor 您不能创建2个类的1个对象。您可以扩展类,以便在实例化子类时调用父类的构造函数,然后可以在该构造函数中创建另一个类的对象
Add employees in array and add option to add employee in ninja? 在数组中添加员工并添加选项以在忍者中添加员工? 1.yes or 2.no? 1.是或2.否? if yes , add to ninja..then in main method print names of ninja using for loop one by one 如果是,则添加到ninja..then,然后在主方法中使用for循环逐一打印忍者的名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.