简体   繁体   中英

How would an implementation for this UML class diagram look like?

UML图

abstract class Activity extend Measurement extends Location{

private DateTime timestamp;
private int heartRate;

private double latitude;
private double longitude;
private double elevation; 
}

I wonder if maybe it should look like this inside, with classes repeated:

public class Measurement{
private DateTime timestamp;
private int heartRate;
}

public class Location{
private double latitude;
private double longitude;
private double elevation;
}

Should it be only all fields or classes inside as well?

You might have misunderstood the black filled diamond, which is not an inheritance but a composition. Your Activity class has no any extends but it contains one instance of Measurement, which has to be instanciated inside Activity.

public class Activity {
    private Measurement measurement = new Measurement();
}

It should look like this I guess (since I see no constructor in your diagram), although I don't know if measurement should be private or public from your UML diagram.

A better practice of this is to instanciate measurement in Activity's constructor, so I'll just leave this link here which might help you to understand how to do it.

EDIT : I forgot to mention, your other classes look correct, but don't forget the association relationship between Measurement and Location : Measurement has to know that Location exists. You'll surely have to keep this in mind when adding methods to your current classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM