This article will build a project in IDE and write corresponding code through the class diagram shown below. In practice, we will recognize the inheritance, aggregation, synthesis (combination), association, dependency of Java. After you finish this project, you will have a deep understanding of these relationships. If someone else gives you a class diagram in the future, you will know how to do it, so class diagram drawing will naturally not be there.Next.
inherit
This inheritance is the most, divided into inheritance classes and interfaces. First, animals, birds, geese, ducks, penguins, flight interfaces, speaker interfaces are created, and the inheritance relationship is realized one by one.The class conversion relationship between the class diagram and the code is as follows:
Existing properties and methods are implemented, and methods related to other relationships in each class are temporarily not implemented.It needs to be said that the bird itself has egg laying behavior (method), inheriting the egg laying behavior (method) from its geese, ducks and penguins is actually a rewrite of the egg laying behavior of the parent bird.
Rewriting polymorphisms about inheritance, also known as method overrides, is often confusing by distinguishing them from method overloads.
Overload: In the same class, methods have the same name (a method with the same name appears), and the parameter list distinguishes which method is called specifically.
Override: In a class hierarchy, a method in a subclass is called a method override if it has the same method name, the same return value type, and a parameter list of the same number and type as the method in the parent class.When an override method is called through a parent reference, Java determines which version of the method to execute based on the type of object being referenced.
Reference parent class members as super keywords to reference:
Super.member variable or super.member method (parameter)
For construction methods, the reference is super (parameter)
class SuperClass { int a; SuperClass(){ a = 10; } public void printA() { System.out.println("In parent class a ="+a);} } class SubClass extends SuperClass { int a; SubClass(int a) { this.a = a; } public void printA() { System.out.println("In subclasses a = "+a); } } public class OverrideDemo { public static void main(String args[]) { SubClass s1 = new SubClass(10); s1.printA(); } }
The result of the code above calls the printA method of the subclass.
If the method in the subclass only has the same method name and return type as the parent method, but the parameter list is different, then the calculation method in the subclass is overloaded, not overridden.
When overriding: Note that methods inherited from the parent can only operate on member variables of the parent when a member variable with the same name as the parent in a subclass.A member variable in a subclass has the same name as a member variable defined in the parent class. When a subclass references this variable, it defaults to referring to its own defined member variable, which "overrides" member variables inherited from the parent class, meaning to hide members of the parent class with the same name.
I have these summaries about polymorphism considerations:
1. No polymorphism in member variables
2. Subclass and parent objects can complete type conversion
3. When looking at polymorphism from the perspective of subclass references, it is mainly to understand how member variables and member methods accomplish coverage.
4. If you look at polymorphism from the perspective of parent reference invoking subclass object, you will find that even if parent reference refers to subclass object, parent reference and member methods of parent class can only directly manipulate parent class's own member variables.Parent references must call subclass methods to indirectly manipulate subclass member variables (where methods must be subclasses that override the methods of the parent, not new methods added to the subclasses).One important usage is the dynamic binding method.Finally, note the keyword instanceof.
More about Java polymorphisms here, polymorphisms are not reflected in this example from the perspective of parent-referenced invoked subclass objects, so less is said, only mentioned in the last summary paragraph.Continue with this example.
Dependency
As the name implies, dependency A requires B to make xx, such as in the example here where animal metabolism requires oxygen and water, is a borrowing relationship, becomes A dependent B, and may also be required to change A when B changes.Expressed in code, a method of a dependent class takes a dependent class as its parameter, or a method of class A creates an instance of class B, or a call to a static method of class B.
Then the dependency of this example can be achieved (of course, I am just one way, I think, there are many other ways, if there are new ideas I can think about)
Animal.java public class Animal { public char living; //Is there life public void metabolism(Oxygen oxygen,Water water) { //The new supersedes the old System.out.print("Metabolism "); oxygen.needOxygen(); water.needWater(); } public void reproduce() { System.out.print("I can reproduce!"); //Reproduction } Oxygen.java package edu.hut.software.zqh; class Oxygen{ //Dependency Air public void needOxygen() { System.out.print("Dependent oxygen-like gases "); } } class Water{ //Dependency Water public void needWater() { System.out.print("Dependent Water-like "); } }
Relevance Relationships
Relevance reflects a strong dependency on the semantic level between two classes, or between classes and interfaces. It makes one class know the properties and methods of another class. This relationship is stronger than dependency. It is not accidental or temporary, but a long-term, relatively equal relationship, which is shown at the code level, and takes the form of class attributes for the class B being related.The expression appears in class A, or it may be that the associated class A references a global variable of the associated class B.If the penguins in this example are related to the climate, then there should be a class attribute of the climate class in the penguins. They are an "owning" relationship.
The implementation code is:
Climate.java package edu.hut.software.zqh; public class Climate { public void changeClimate() { System.out.print("Association "Weather""); } } Penguin.java package edu.hut.software.zqh; public class Penguin extends Bird{ private Climate climate; //Relevance Relationships public Penguin(Climate climate) { this.climate=climate; } public void layEggs() { System.out.print("I'm a penguin so I make penguin eggs "); climate.changeClimate(); } }
Relevance Relationships
Aggregation is a special case of association and a strong association. Aggregation is the whole and individual relationship. At this time, the whole and the part can be separated. They have their own life cycle, some of which can belong to multiple objects or can be shared by multiple objects. For example, computer and CPU, the relationship between company and employees. Aggregation and Association are consistent at the code level and can only be semantically based.To distinguish.
Aggregation relationships are also implemented using instance variables, and there is no distinction between association and aggregation in java syntax. Classes in association relationships come from one level, while aggregation is evident at two distinct levels.
In this example, the geese group and the geese, several individual geese form a geese group, and the geese leave the geese group, both of which can exist independently.
Implementation code:
WideGoose.java package edu.hut.software.zqh; public class WideGoose extends Bird implements Fly{ public void layEggs() { System.out.print("I'm a goose so I make goose eggs"); } public void fly() { System.out.print("I can fly"); } } WideGooseAggregate.java package edu.hut.software.zqh; public class WideGooseAggregate { private WideGoose[] arrayWideGoose; //I don't know how to assign those attributes for the moment. It doesn't feel very useful, so I don't assign them anymore. public void horizontalFly() { System.out.printf("Flying Queue of Goose Groups"); } public void vFiy() { System.out.printf("Flying Queue of Goose Groups"); } }
The code implemented through Java does not really distinguish between associations, but the two classes are not at the same level.
Composite (combinatorial) relationship
Combination is also a special case of association, stronger than aggregation, and a strong aggregation relationship.It also reflects the relationship between the whole and the part, but at this time the whole and the part are inseparable, and the end of the whole life cycle also means the end of the part life cycle, and vice versa.If the brain and human beings, as well as the birds and wings in this case, it's really terrifying that a bird has no wings.
The implementation code is as follows:
Wing.java package edu.hut.software.zqh; public class Wing { public void wing() { System.out.print("Composite relationship "with wings""); } } Animal.java package edu.hut.software.zqh; public class Bird extends Animal{ public int feather; //Are there feathers public int mouse; //Is the mouth keratinous without teeth private Wing wing; public Bird() { wing=new Wing(); wing.wing(); } public void layEggs() { //Laying eggs System.out.print("Different kinds of birds lay different kinds of eggs!"); } }
And nothing is different in code from associations
Last
I've given most of the code in the process, leaving the classes that implement inheritance as follows:
package edu.hut.software.zqh; public class DonaldDuck extends Duck implements Speak{ public void speak() { System.out.print("Hello, I am Donald Duck. Unlike other ducks, I inherit the speaker interface and talk."); } } package edu.hut.software.zqh; public class Duck extends Bird{ public void layEggs() { System.out.print("I am a duck so I lay duck eggs"); } } package edu.hut.software.zqh; public interface Speak { public abstract void speak(); } interface Fly{ public abstract void fly(); } public static void main(String args[]) { //Inherited methods are invoked and related statements are output to verify that relationships are implemented Penguin penguin=new Penguin(new Climate()); System.out.print(" "); penguin.layEggs(); System.out.print(" "); penguin.metabolism(new Oxygen(), new Water()); System.out.print(" "); penguin.reproduce(); System.out.println(); DonaldDuck donaldduck=new DonaldDuck(); System.out.print(" "); donaldduck.layEggs(); System.out.print(" "); donaldduck.metabolism(new Oxygen(), new Water()); System.out.print(" "); donaldduck.reproduce(); System.out.print(" "); donaldduck.speak(); System.out.println(); WideGoose widegoose=new WideGoose(); System.out.print(" "); widegoose.layEggs(); System.out.print(" "); widegoose.metabolism(new Oxygen(), new Water()); System.out.print(" "); widegoose.reproduce(); System.out.print(" "); widegoose.fly(); } }
All code has been uploaded and the execution order can be seen after running