简体   繁体   English

摘要 Class getter 和 setter 用法 [V2]

[英]Abstract Class getter and setter usage [V2]

Good Morning,早上好,

This is an add-on to a question I recently asked about the proper usage of abstract classes.这是我最近询问的关于正确使用抽象类的问题的附加内容。

I currently have a abstract class and 2 sub classes see illustration for example:我目前有一个抽象的 class 和 2 个子类,例如参见插图:

public abstract class Vehicle {

  String type;
  String color;
  
  ...getter and setters
 

 }

Inherits getter and setter from parent, plus one unique property从父级继承 getter 和 setter,外加一个独特的属性

  public class Truck extends Vehicle{


   String pickupBed;

 
  public setPickupBed(String pickupBed){
   this.pickupBed = pickupBed;
  }
  public getPickupBed(String pickupBed){
   return pickupBed;
  }


 .....Parent class getter and setters
  }

Inherits getter and setter from parent从父级继承 getter 和 setter

  public class Car extends Vehicle{

   .....Parent class getter and setters
  }

The situation I am having is when I create a Method in my DAO class, for example例如,当我在 DAO class 中创建一个方法时,我遇到的情况是

 public Vehicle selectAllRecords(){
    ...DO database stuff
 return VehicleRowMapperWithData();
 }

And if I call Vehicle veh = selectAllRecords() I can not do this veh.getPickupBed() I understand thats not how it works, but how can I accomplish this?如果我调用Vehicle veh = selectAllRecords()我不能这样做veh.getPickupBed()我明白这不是它的工作原理,但我该如何做到这一点? I tried casting but it didnt work.我尝试铸造,但没有奏效。

Thanks in advance提前致谢

There's no easy answer.没有简单的答案。

Use your DB tooling使用您的数据库工具

Some DBs support 'subclassing';一些数据库支持“子类化”; you can make a table that extends another.您可以制作一个扩展另一个表格的表格。 Postgres can do this, for example.例如,Postgres 可以做到这一点。 Many DB abstraction layers do not support it, psql doesn't support it all that well (it works and is maintained, but not really recommended).许多 DB 抽象层不支持它,psql 也不能很好地支持它(它可以工作并得到维护,但不推荐)。 As long as the DB more or less perfectly reflects how your java code works, then there's at least a chance that the tooling you use will just figure out what to do as if by magic.只要 DB 或多或少完美地反映了您的 java 代码的工作方式,那么您使用的工具至少有机会像魔术一样弄清楚该做什么。

Write code编写代码

Forget java for a moment.暂时忘记 java。 Just look at your DB tables.看看你的数据库表。 How do you know a certain vehicle is, in fact, a truck?你怎么知道某辆车实际上是一辆卡车? Is there a separate table called 'truck'?是否有一个单独的表称为“卡车”? Is there a column 'vehicleType', and all the custom fields for each kind of vehicle is a column in your DB table, with each row that isn't that kind of vehicle holding NULL or some dummy value for that column?是否有“vehicleType”列,每种车辆的所有自定义字段都是数据库表中的一列,每一行不是那种车辆,每行都持有 NULL 或该列的一些虚拟值? In that case, you'd write a big if/elseif block that checks the vehicle type column and then calls the right new Car(...) , or new Truck(...) constructor.在这种情况下,您将编写一个大的 if/elseif 块来检查车辆类型列,然后调用正确的new Car(...)new Truck(...)构造函数。

More generally you now have a mismatch between your java object model and your DB model.更一般地说,您现在的 java object model 和您的 DB Z20F35E6305F44DBFA4ZC3F6 之间不匹配。

This is usually a good thing, though, JPA and friends work best when these 2 match precisely, but given that java is OO and likes hierarchy, and DBs really don't.不过,这通常是一件好事,JPA 和朋友在这两个精确匹配时效果最好,但考虑到 java 是 OO 并且喜欢层次结构,而 DB 真的不这样做。 that's not neccessarily a good idea, However: once your java's class structures do not match up with how the DB stores things (example, In your DB, it's one table with a 'type' column, in java, it's a hierarchy of classes and there is no 'type' field, instead, there are instances of Car, instances of Truck. etc) - that means automated tools can't just magically turn one into the other.这不一定是一个好主意,但是:一旦您的 java 的 class 结构与数据库存储事物的方式不匹配(例如,在您的数据库中,它是一个带有“类型”列的表,在 java 中,它是类的层次结构和没有“类型”字段,而是有 Car 的实例、Truck 的实例等)——这意味着自动化工具不能只是神奇地把一个变成另一个。 You write code that does this.您编写执行此操作的代码。

Match java to the DB将 java 与 DB 匹配

Instead of trying to make sure the DB's table definitions match your java code exactly, which is hard (psql) or impossible (most other db systems that do not have subtabling features), well, as they say, if you can't get the stone up the mountain, maybe bring the mountain to the stone: Forget about your type hierachy in java.而不是试图确保数据库的表定义与您的 java 代码完全匹配,这很难(psql)或不可能(大多数其他没有子表功能的数据库系统),正如他们所说,如果你不能得到石头上山,也许把山带到石头上:忘记你在 java 中的类型层次。 Just have one class Vehicle, and an enum VehicleType.只需拥有一辆 class 车辆和一个枚举 VehicleType。 This is a bit nasty, as it breaks all sorts of idiomatic java rules;这有点讨厌,因为它违反了各种惯用的 java 规则; now an instance of Vehicle representing a roadbike has a getPickupBed() method which is weird.现在代表公路自行车的 Vehicle 实例有一个很奇怪的getPickupBed()方法。

This brings us back to the simple truth of things:这让我们回到事物的简单真相:

  • Idiomatic (as in, most tutorials recommend it, tooling of all stripes work best if you do it that way, it's a supported path in most DBs and any issues in that workflow are found early and fixed fast because most folks working with this software work just like you do, that's what 'idiomatic' means) DB design is important.惯用的(正如大多数教程推荐的那样,如果你这样做,所有条纹的工具效果最好,它是大多数数据库中受支持的路径,并且该工作流程中的任何问题都可以及早发现并快速修复,因为大多数使用此软件的人都可以工作就像你一样,这就是“惯用”的意思)数据库设计很重要。
  • Idiomatic code is important.惯用代码很重要。
  • The same concept represented as idiomatic DB design often doesn't match idiomatic code design.以惯用 DB 设计表示的相同概念通常与惯用代码设计不匹配。
  • We like automated conversion.我们喜欢自动转换。

These things aren't fully compatible with each other.这些东西彼此并不完全兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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