java 什么是子类化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5072780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:17:08  来源:igfitidea点击:

What is subclassing?

javaxmlclonesubclassing

提问by codenamejupiterx

I am new to java and I am trying to create an XML document and clone a specific node (minus the textnode) of this document over and over again. Someone answered me and said that I should subclass the node and override the cloning. So my question is what is sub-classing?

我是 Java 新手,我正在尝试创建一个 XML 文档并一遍又一遍地克隆该文档的特定节点(减去文本节点)。有人回答我说我应该对节点进行子类化并覆盖克隆。所以我的问题是什么是子分类?

采纳答案by Stephen C

@Charlie Martin has explained what subclassing means.

@Charlie Martin 解释了子类化的含义。

However, it is not clear that you've been given good advice. If you are creating the XML document by assembling a DOM in memory, a better approach would be to create a helper class with static methods that perform the sequence of DOM node operations that you need to do.

但是,目前尚不清楚您是否得到了好的建议。如果您通过在内存中组装 DOM 来创建 XML 文档,更好的方法是创建一个带有静态方法的辅助类,这些方法执行您需要执行的 DOM 节点操作序列。

回答by Charlie Martin

Subclassing means to define a new class that has the properties of an old class (the "superclass") with some changes.

子类化意味着定义一个新类,该类具有旧类(“超类”)的属性并进行了一些更改。

In this case, your original responder is saying something like this:

在这种情况下,您的原始回复者是这样说的:

Say you have a base class Basewhich has a method getTwolike so:

假设你有一个基类Base,它有一个getTwo像这样的方法:

class Base {
   public int getTwo(){ return 2;}
}

You decide you want a new class that still have a method getTwobut that returns the string"two" instead of the number 2. You could define it as

您决定需要一个仍然有方法getTwo但返回字符串“two”而不是数字 2的新类。您可以将其定义为

class Subclass extends Base {
   public String getTwo() { return "two"; }
}

We say Subclassis a subclass of -- or more commonly, "is a kind of" -- Base.

我们说Subclass是的子类 - 或者更常见的是,“是一种” - Base

Beyond that, you'd be best off to read a book on object-oriented programming with Java. I'm fond of Thinking in Java, which has the added advantage that it's available freely on line.

除此之外,您最好阅读一本关于使用 Java 进行面向对象编程的书。我喜欢Thinking in Java,它还有一个额外的优势,即它可以在线免费获得。

回答by user1207965

In short Answer : A Superclass can be Subclassed - That means for a specific class we can find/create a subclass that extend it.

简而言之:一个超类可以被子类化——这意味着对于一个特定的类,我们可以找到/创建一个扩展它的子类。

回答by ahmednabil88

Subclass represents is arelationship in Object-Oriented Programming (Inheritance).

子类表示is a面向对象编程(继承)中的关系。

For example
The Circleis aShap.
So we can say:
The Circle classis a subclass of Shape class.

例如
The Circle is aShap
因此,我们可以说:
Circle类是的子类Shape类