子类继承的字段从未被使用

huangapple go评论58阅读模式
英文:

Subclass inherited field is never used

问题

可以在知道某些字段永远不会被使用的情况下继承一个类吗?

假设我有一个父类 Parent 及其子类 Child:

public class Parent {
    private String a;
    private String b;
    private String c;
...
}

public class Child extends Parent {
    private String d;
...
}

我可以继承这样的一个类吗?我知道在我的 Child 类中我只会使用字段 "a"、"b" 和 "d",而字段 "c" 将永远不会被使用。

附:我不是在问编译器是否会报错。我在询问更好的设计实践。如果这是不好的做法,如何修正?

英文:

Can I inherit from a class knowing that some of its fields will never be used?

Let's say I have a Parent class and its subclass Child:

public class Parent {
    private String a;
    private String b;
    private String c;
...
}

public class Child extends Parent {
    private String d;
...
}

Am I allowed to inherit from a class like that knowing that in my Child class I will only use fields "a", "b", "d" and field "c" will never be used?

P.S. Not asking if compiler is going to complain or not. Asking for a better design practice. If this is bad practice, then how to fix it?

答案1

得分: 2

你正在提出错误的问题。可能存在问题,也可能不存在问题,但是专注于类中包含的数据是错误的。你必须专注于它们的行为。

在她的主题演讲《数据抽象和层次结构》中,Barbara Liskov介绍了通常被称为里氏替换原则的概念,该概念在Liskov和Jeannette Wing撰写的论文《子类型的行为概念》中得到了拓展。其核心思想是,如果一个子类型可以在程序中替换掉父类型而不影响程序的正确性(即不会使程序中断),那么这个子类型就是父类型的一个子类。

如果你的程序中所有的Parent实例都被Child实例替换,会发生什么?子类能否完成父类的所有任务?它支持相同的操作吗?它会产生相同的结果吗?

如果答案是肯定的,那么ChildParent的一个子类型。如果答案是否定的,那么它就不是,并且不应该是它的一个子类。字段c是否相关完全取决于它的使用方式。

英文:

You are asking the wrong question. There may or may not be a problem, but focusing on the data contained in the classes is wrong. You must focus on their behavior.

In her keynote address "Data Abstraction and Hierarchy" Barbara Liskov introduced what is commonly known as the Liskov Substitution Principle, expanded on in the paper "A Behavioral Notion of Subtyping" by Liskov and Jeannette Wing. The idea is that one type is a subtype of another if the subtype could replace the supertype everywhere in a program without impacting its correctness; that is, without breaking it.

What would happen if all Parent instances in your program were replaced with Child instances? Can the child do everything the parent does? Does it support the same operations? Will it produce the same results?

If the answer to that is yes, then Child is a subtype of Parent. If the answer to that is no, then it is not and it should not be a subclass of it. Whether field c is relevant depends entirely on how it is used.

答案2

得分: 1

如果您的父类允许被扩展,而子类不需要其父类提供的所有信息,那么很可能您的父类做了过多的事情,可以将其拆分为不同的类。

在进行了这次重构之后,您可以扩展更适合所需行为的类。

英文:

If your Parent class allows being extended and the Child class does not require all the information that its Parent provides, then it is likely your Parent class does too much and it can be separated into different classes.

After you made this refactor, you can extend the class that fits better the behavior you want.

答案3

得分: 1

以下是一个分离 Parent 类的示例。

class AB {
    Object a;
    Object b;
}
class ABC extends AB {
    Object c;
}
class ABD extends AB {
    Object d;
}

继承是一个不太好的代码重用工具,如果这些类没有共享的行为,你可以考虑使用组合。

class AB {
    Object a;
    Object b;
}
class ABC {
    AB ab;
    Object c;
}
class ABD {
    AB ab;
    Object d;
}
英文:

Here is an example of separating the Parent class.

class AB {
    Object a;
    Object b;
}
class ABC extends AB {
    Object c;
}
class ABD extends AB {
    Object d;
}

Inheritance is a poor tool for code reuse, so you may consider composition instead, if these classes have no shared behavior.

class AB {
    Object a;
    Object b;
}
class ABC {
    AB ab;
    Object c;
}
class ABD {
    AB ab;
    Object d;
}

huangapple
  • 本文由 发表于 2020年9月2日 22:35:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63707843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定