Maintaining a two way relationship between classes

前端 未结 6 613
萌比男神i
萌比男神i 2021-02-02 00:34

It is pretty common, especially in applications with an ORM, to have a two way mapping between classes. Like this:

public class Product
{
    private List

        
6条回答
  •  暖寄归人
    2021-02-02 01:17

    My first thought is, in your function/property used to add prices, add a line of code like so:

    public void addPrice(Price p) {
         //code to add price goes here
         p.Product = this;
    }
    

提交回复
热议问题