简体   繁体   English

我如何同时表示应该是2个不同类的对象

[英]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: 问题的关键是:

  1. we should use one interface. 我们应该使用一个接口。
  2. Ninja class should have some methods (not attributes). 忍者类应该有一些方法(不是属性)。

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 ) moveNinja1moveNinja2

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.

相关问题 我如何在 Java 的不同类中使用相同的 object - How can i use same object in different classes in Java 如何从不同的类访问同一个对象 - How can I have access to the same object from different classes 我如何指定对象列表中来自不同类的许多不同对象的数组中的哪个类来自 - How can I specify which class an object in an array list of many different objects from different classes comes from 如何在两个不同的类中访问同一个堆栈? - How can i access the same Stack in two different Classes? 如何在两个不同的类中设置相同的属性? - How can I set same Property in two different classes? 如何在不同的类型ArrayList中使用相同的方法? - How can I use same methods in different classes type ArrayList? 如何在不同的类中引用相同的类变量? - How can I reference the same class variable in different classes? 通过不同的类同时处理对象列表的最佳方法是什么 - What is the best way to process a object list at the same time by different classes 如何消除/删除在数组中重复大量时间的 object 数组元素的重复。 (返回相同的数组类型) - How can I eliminate/remove a duplication of an object array element which is duplicated a ton of time in the array. (Return same array type) 如何同时显示 2 个不同的面板? - How can I display 2 different panels at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM