Java object orientation: the ultimate training of interfaces

Problem description

There are four roles as follows. The attributes and behaviors of these roles are

Stock investors: name, age, study stock speculation, speak English

Fund Investor: name, age, study and speculation

Stock Manager: name, age, teaching stock trading, speaking English

Fund manager: name, age, teaching and speculation base

Please use your knowledge to think about which classes and interfaces you design?

analysis

For beginners of object-oriented comrades may think, define these four roles into four classes, write their properties and methods respectively, and then create these four objects in the test class, and then call their respective methods. After learning inheritance, you can extract the same attributes and methods and write them into a base class. As a derived class, the four role classes do not need to write these attributes anymore; After learning the interface, you can extract different behaviors of specific processes into the interface, and unify the specifications of these behaviors in the interface. Ben Reno believes that the most intuitive and clear idea at present is as follows

  1. There are four roles here. Define four classes first. Don't worry about writing members
  2. It is found that these roles have common attributes: name, age, define a Human class, extract the common attributes and write them into the Human class, and let the four roles inherit Human
  3. Investors have the behavior of learning, while managers have the behavior of teaching, but the specific content of teaching and learning is different. Define two interfaces, Teach and Learn, abstract the behavior of teaching and learning into the interface of Teach and Learn, and let all managers and investors implement the teaching / learning methods inside respectively
  4. Stock investors and managers have the behavior of speaking English. Define the speakeenglish interface, abstract the English speaking method to this interface, and let stock investors and managers realize the English speaking method inside
  5. The test class creates objects of four roles and calls their respective methods

Comrades who have studied UML can have a look at this class diagram (gray lines plus triangles represent implementation relationships)

Implementation code

//Human.java
public class Human {
    String name;
    int age;

    public Human() {}

    public Human(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
//Teach.java
public interface Teach {
    void teach();
}
//Learn.java
public interface Learn {
    void learn();
}
//SpeakEnglish.java
public interface SpeakEnglish {
    void speak();
}
//FundManager.java
public class FundManager extends Human implements Teach {
    public FundManager() {}

    public FundManager(String name, int age) {
        super(name, age);
    }

    @Override
    public void teach() {
        System.out.println("I am a fund manager" + name + ", Teaching fried chicken");
    }
}
//FundInvester.java
public class FundInvester extends Human implements Learn {
    public FundInvester() {}

    public FundInvester(String name, int age) {
        super(name, age);
    }

    @Override
    public void learn() {
        System.out.println("I am a fund investor" + name + ", Learning to stir fry");
    }
}
//SharesInvester.java
public class SharesInvester extends Human implements Learn, SpeakEnglish {
    public SharesInvester() {}

    public SharesInvester(String name, int age) {
        super(name, age);
    }

    @Override
    public void learn() {
        System.out.println("I am a stock investor" + name + ", Learning to speculate in stocks");
    }

    @Override
    public void speak() {
        System.out.println("I could speak English, and you?");
    }
}
//SharesManager.java
public class SharesManager extends Human implements Teach, SpeakEnglish {
    public SharesManager() {
    }

    public SharesManager(String name, int age) {
        super(name, age);
    }

    @Override
    public void teach() {
        System.out.println("I'm a stock manager" + name + ", Teaching stock trading");
    }

    @Override
    public void speak() {
        System.out.println("I can speak English, and you?");
    }
}
//HumanTester.java
public class HumanTester {
    public static void main(String[] args) {
        FundManager rain = new FundManager("Rain", 37);
        FundInvester mary = new FundInvester("Mary", 24);
        SharesManager alice = new SharesManager("Alice", 34);
        SharesInvester reno = new SharesInvester("Reno", 21);
        rain.teach();
        mary.learn();
        alice.teach();
        alice.speak();
        reno.learn();
        reno.speak();
    }
}


 

Write it at the back

There may be multiple solutions to the same problem. No one is right or wrong. The key lies in your understanding of the problem structure and the focus of analyzing the problem structure. As for this problem, Reno can also be simulated in C++. Those who are interested can leave a message in the comment area.

Tags: Java JavaSE programming language

Posted by ballhogjoni on Tue, 09 Aug 2022 21:47:12 +0300