如何在 Java 中仅扩展函数而不是整个类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7415430/
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
How can i extend function only instead of whole class in Java?
提问by duffymo
How can i extend functions only in Java? (not class extends)
如何仅在 Java 中扩展功能?(不是类扩展)
/*Example:
* year 1990 Alex Kumar
* year 1990 Jhon Ya'ay
* made this: do not touch modify here */
public static void OldMade()
{
Connect("+1800PIZZA");
Say("MYID:69");
Request("PIZZACITROEN_BLACK");
}
/*Example:
* year 2011 Sunil Williams
* applied which extends 1990 */
public static void MadeInIndia extends this.OldMade
{
//hidden include Connect("+1800PIZZA");
//hidden include Say("MYID:69");
//hidden include Request("PIZZACITROEN_BLACK");
Send("CorrectionPandit");
}
private static void main(Strig args[])
{
// Try 1 call and get old + new
MadeInIndia();
//execute Connect("+1800PIZZA");
//execute Say("MYID:69");
//execute Request("PIZZACITROEN_BLACK");
//execute Send("CorrectionPandit");
}
回答by duffymo
Can't be done. Java's object-oriented; you only inherit and extend from classes and interfaces.
做不到。Java的面向对象;您只能从类和接口继承和扩展。
You don't "extend" static methods. In the following arrangement, class B's static version of foo will overshadow that from A. If B needs to call A's static method, it can do so explicitly:
您不会“扩展”静态方法。在下面的安排中,类 B 的 foo 的静态版本将掩盖来自 A 的那个。如果 B 需要调用 A 的静态方法,它可以显式这样做:
public class A {
public static void foo() { System.out("foo for A classes"); }
}
public class B extends A {
public static void foo() { System.out("foo for B classes"); }
}
回答by Andrey
You can't extend a function. Why not just to write:
你不能扩展一个函数。为什么不只是写:
public static void MadeInIndia extends this.
{
OldMade();
Send("CorrectionPandit");
}
回答by Matteo
To extendstatic methods (as from your example) you just call the original method:
要扩展静态方法(如您的示例),您只需调用原始方法:
public static void MadeInIndia {
OldMade();
Send("CorrectionPandit");
}
回答by Sanjay Jain
Just because Inheritance defined in this way only, So you cant override method. Concept of super class is to put the general behavior at super level type. And your sub class is of the same type of that super class. And if you need some specific behavior in sub class then override super class method and change it according sub class requirement.
仅仅因为继承仅以这种方式定义,所以您不能覆盖方法。超类的概念是将一般行为放在超类中。并且您的子类与该超类属于同一类型。如果您需要子类中的某些特定行为,则覆盖超类方法并根据子类要求更改它。
回答by anirvan
If you consider inheritance
- its primary purpose is to allow child
classes to add upon the parent
class' attributes and behaviour. Besides adding on, or extending, the child
class might also choose to override. Since overriding doesn't make much sense for attributes, it's largely used for methods. Also note, when you override methods, you pretty much re-write the entire logic, or, invoke super.method()
and then do something after.
如果您考虑inheritance
- 它的主要目的是允许child
类添加parent
类的属性和行为。除了添加或扩展之外,child
该类还可以选择覆盖。由于覆盖对属性没有多大意义,它主要用于方法。另请注意,当您覆盖方法时,您几乎会重新编写整个逻辑,或者调用super.method()
然后执行某些操作。
Now, if you were to tryand apply the same concept to methods - you'll see that it really doesn't make much sense in case of an object oriented language. Methods always encapsulate behaviouronly. Attributes are not associated with methods. So by extending, you'd only either end up re-writing the entire logic, or, do something along the lines of super.method()
.
现在,如果您尝试将相同的概念应用于方法 - 您会发现它在面向对象语言的情况下确实没有多大意义。方法总是只封装行为。属性与方法无关。因此,通过扩展,您要么最终重写整个逻辑,要么按照super.method()
.
However, all this makes sense in case of a functional programming languagelike Haskell, Scala or Javascript. This is because these languages consider functions
are first class members - meaning, you can write your program using only functions, without any classes. In such languages, you'll find that function inheritance is available in some or the other form.
然而,对于函数式编程语言,如 Haskell、Scala 或 Javascript ,所有这些都是有意义的。这是因为这些语言认为functions
是一流的成员 - 这意味着您可以仅使用函数编写程序,而无需使用任何类。在这些语言中,您会发现函数继承以某种或另一种形式可用。
回答by Bhabani Sankar Mishra
There is no way you can extend a method; it's against OOP concepts.
您无法扩展方法;它违反了面向对象的概念。
You can implement the functionality in the following two ways -
您可以通过以下两种方式实现该功能 -
public static void OldMade()
{
Connect("+1800PIZZA");
Say("MYID:69");
Request("PIZZACITROEN_BLACK");
}
public static void MadeInIndia extends this.OldMade
{
OldMade();
Send("CorrectionPandit");
}
private static void main(Strig args[])
{
MadeInIndia();
}
Or
或者
public static void OldMade()
{
Connect("+1800PIZZA");
Say("MYID:69");
Request("PIZZACITROEN_BLACK");
}
/*public static void MadeInIndia extends this.OldMade
{
OldMade();
Send("CorrectionPandit");
}*/
private static void main(Strig args[])
{
//MadeInIndia();
OldMade();
Send("CorrectionPandit");
}